|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
1 | 3 | import { defineConfig } from '@rslib/core'; |
| 4 | +import type { Configuration } from '@rspack/core'; |
| 5 | +import pkgJson from './package.json'; |
| 6 | +import prebundleConfig from './prebundle.config.mjs'; |
| 7 | +import type { RsbuildPlugin } from './src'; |
| 8 | + |
| 9 | +const define = { |
| 10 | + RSBUILD_VERSION: JSON.stringify(pkgJson.version), |
| 11 | +}; |
| 12 | + |
| 13 | +const regexpMap: Record<string, RegExp> = {}; |
| 14 | + |
| 15 | +for (const item of prebundleConfig.dependencies) { |
| 16 | + const depName = typeof item === 'string' ? item : item.name; |
| 17 | + regexpMap[depName] = new RegExp(`compiled[\\/]${depName}(?:[\\/]|$)`); |
| 18 | +} |
| 19 | + |
| 20 | +const externals: Configuration['externals'] = [ |
| 21 | + 'webpack', |
| 22 | + '@rspack/core', |
| 23 | + '@rsbuild/core', |
| 24 | + '@rsbuild/core/client/hmr', |
| 25 | + '@rsbuild/core/client/overlay', |
| 26 | + ({ request }, callback) => { |
| 27 | + const entries = Object.entries(regexpMap); |
| 28 | + if (request) { |
| 29 | + for (const [name, test] of entries) { |
| 30 | + if (request === name) { |
| 31 | + throw new Error( |
| 32 | + `"${name}" is not allowed to be imported, use "../compiled/${name}/index.js" instead.`, |
| 33 | + ); |
| 34 | + } |
| 35 | + if (test.test(request)) { |
| 36 | + return callback(undefined, `../compiled/${name}/index.js`); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + callback(); |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +const pluginFixDtsTypes: RsbuildPlugin = { |
| 45 | + name: 'fix-dts-types', |
| 46 | + setup(api) { |
| 47 | + api.onAfterBuild(() => { |
| 48 | + const typesDir = path.join(process.cwd(), 'dist-types'); |
| 49 | + const pkgPath = path.join(typesDir, 'package.json'); |
| 50 | + if (!fs.existsSync(typesDir)) { |
| 51 | + fs.mkdirSync(typesDir); |
| 52 | + } |
| 53 | + fs.writeFileSync( |
| 54 | + pkgPath, |
| 55 | + JSON.stringify({ |
| 56 | + '//': 'This file is for making TypeScript work with moduleResolution node16+.', |
| 57 | + version: '1.0.0', |
| 58 | + }), |
| 59 | + 'utf8', |
| 60 | + ); |
| 61 | + }); |
| 62 | + }, |
| 63 | +}; |
2 | 64 |
|
3 | 65 | export default defineConfig({ |
| 66 | + source: { |
| 67 | + define, |
| 68 | + }, |
| 69 | + output: { |
| 70 | + target: 'node', |
| 71 | + externals, |
| 72 | + }, |
4 | 73 | lib: [ |
| 74 | + // Node / ESM |
| 75 | + { |
| 76 | + format: 'esm', |
| 77 | + syntax: 'es2021', |
| 78 | + shims: { |
| 79 | + esm: { |
| 80 | + __filename: true, |
| 81 | + __dirname: true, |
| 82 | + }, |
| 83 | + }, |
| 84 | + plugins: [pluginFixDtsTypes], |
| 85 | + }, |
| 86 | + // Node / CJS |
| 87 | + { |
| 88 | + format: 'cjs', |
| 89 | + syntax: 'es2021', |
| 90 | + source: { |
| 91 | + entry: { |
| 92 | + index: './src/index.ts', |
| 93 | + ignoreCssLoader: './src/loader/ignoreCssLoader.ts', |
| 94 | + transformLoader: './src/loader/transformLoader.ts', |
| 95 | + transformRawLoader: './src/loader/transformRawLoader.ts', |
| 96 | + }, |
| 97 | + }, |
| 98 | + footer: { |
| 99 | + // TODO https://github.com/web-infra-dev/rslib/issues/351 |
| 100 | + js: `// Annotate the CommonJS export names for ESM import in node: |
| 101 | +0 && (module.exports = { |
| 102 | + PLUGIN_CSS_NAME, |
| 103 | + PLUGIN_SWC_NAME, |
| 104 | + __internalHelper, |
| 105 | + createRsbuild, |
| 106 | + defineConfig, |
| 107 | + ensureAssetPrefix, |
| 108 | + loadConfig, |
| 109 | + loadEnv, |
| 110 | + logger, |
| 111 | + mergeRsbuildConfig, |
| 112 | + rspack, |
| 113 | + version |
| 114 | +});`, |
| 115 | + }, |
| 116 | + }, |
5 | 117 | // Client / ESM |
6 | 118 | { |
7 | 119 | format: 'esm', |
|
0 commit comments