diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 5891b2bb..dcde67d1 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -542,50 +542,17 @@ const composeFormatConfig = (format: Format): RsbuildConfig => { } }; -const resolveShims = (shims?: Shims) => { - const resolvedShims = { +const resolveShims = (shims?: Shims): ResolvedShims => { + return { cjs: { - 'import.meta.url': true, + 'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true, }, esm: { - __filename: true, - __dirname: true, - require: false, + __filename: shims?.esm?.__filename ?? false, + __dirname: shims?.esm?.__dirname ?? false, + require: shims?.esm?.require ?? false, }, }; - - if (!shims) { - return resolvedShims; - } - - if (shims.cjs) { - if (typeof shims.cjs === 'boolean') { - if (shims.cjs === true) { - resolvedShims.cjs['import.meta.url'] = true; - } else { - resolvedShims.cjs['import.meta.url'] = false; - } - } else { - resolvedShims.cjs['import.meta.url'] = - shims.cjs['import.meta.url'] ?? false; - } - } - - if (shims.esm) { - if (typeof shims.esm === 'boolean') { - if (shims.esm === true) { - resolvedShims.esm.__filename = true; - resolvedShims.esm.__dirname = true; - resolvedShims.esm.require = true; - } - } else { - resolvedShims.esm.__filename = shims.esm.__filename ?? false; - resolvedShims.esm.__dirname = shims.esm.__dirname ?? false; - resolvedShims.esm.require = shims.esm.require ?? false; - } - } - - return resolvedShims; }; const composeShimsConfig = ( diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index f6749e85..ee55c7cf 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -49,18 +49,14 @@ export type BannerAndFooter = { }; export type Shims = { - cjs?: - | boolean - | { - 'import.meta.url'?: boolean; - }; - esm?: - | boolean - | { - __filename?: boolean; - __dirname?: boolean; - require?: boolean; - }; + cjs?: { + 'import.meta.url'?: boolean; + }; + esm?: { + __filename?: boolean; + __dirname?: boolean; + require?: boolean; + }; }; export type ResolvedShims = { diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index 9f510038..0d048c13 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -112,8 +112,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, }, "node": { - "__dirname": "node-module", - "__filename": "node-module", + "__dirname": false, + "__filename": false, }, "optimization": { "concatenateModules": true, diff --git a/tests/integration/shims/cjs/rslib.config.ts b/tests/integration/shims/cjs/rslib.config.ts index 18492bfa..2b558cd1 100644 --- a/tests/integration/shims/cjs/rslib.config.ts +++ b/tests/integration/shims/cjs/rslib.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ generateBundleEsmConfig(), generateBundleCjsConfig({ shims: { - cjs: true, + cjs: { 'import.meta.url': true }, }, }), ], diff --git a/tests/integration/shims/esm/rslib.config.ts b/tests/integration/shims/esm/rslib.config.ts index 3ff50b22..5628aa7f 100644 --- a/tests/integration/shims/esm/rslib.config.ts +++ b/tests/integration/shims/esm/rslib.config.ts @@ -4,7 +4,7 @@ import { generateBundleEsmConfig } from 'test-helper'; export default defineConfig({ lib: [ generateBundleEsmConfig({ - shims: { esm: true }, + shims: { esm: { __dirname: true, __filename: true } }, source: { entry: { index: './src/index.ts', @@ -17,7 +17,7 @@ export default defineConfig({ }, }), generateBundleEsmConfig({ - shims: { esm: true }, + shims: { esm: { __dirname: true, __filename: true } }, syntax: 'esnext', source: { entry: { @@ -31,7 +31,7 @@ export default defineConfig({ }, }), generateBundleEsmConfig({ - shims: { esm: true }, + shims: { esm: { __dirname: true, __filename: true } }, syntax: 'esnext', source: { entry: { diff --git a/tests/integration/shims/esm/rslibShimsDisabled.config.ts b/tests/integration/shims/esm/rslibShimsDisabled.config.ts index e036a5cf..85d625a2 100644 --- a/tests/integration/shims/esm/rslibShimsDisabled.config.ts +++ b/tests/integration/shims/esm/rslibShimsDisabled.config.ts @@ -3,7 +3,7 @@ import config from './rslib.config'; export default defineConfig({ ...config, - lib: [config.lib[2]!].map((libConfig) => { + lib: [config.lib[0]!, config.lib[2]!].map((libConfig) => { libConfig.output!.distPath!.root = libConfig.output!.distPath!.root!.replace( './dist/enabled', diff --git a/tests/integration/shims/index.test.ts b/tests/integration/shims/index.test.ts index 6b7e1687..9dfbb06c 100644 --- a/tests/integration/shims/index.test.ts +++ b/tests/integration/shims/index.test.ts @@ -61,8 +61,11 @@ describe('ESM shims disabled', async () => { fixturePath, configPath: './rslibShimsDisabled.config.ts', }); + + expect(entries.esm0).not.toContain('fileURLToPath'); + const context = vm.createContext({}); - const module = new vm.SourceTextModule(entries.esm, { + const module = new vm.SourceTextModule(entries.esm1!, { context, }); diff --git a/website/docs/en/config/lib/shims.mdx b/website/docs/en/config/lib/shims.mdx index 76cd96d9..db43258a 100644 --- a/website/docs/en/config/lib/shims.mdx +++ b/website/docs/en/config/lib/shims.mdx @@ -2,45 +2,39 @@ - **Type:** -```ts -type Shims = { - cjs?: - | boolean - | { - 'import.meta.url'?: boolean; - }; - esm?: - | boolean - | { - __filename?: boolean; - __dirname?: boolean; - require?: boolean; - }; -}; -``` + ```ts + type Shims = { + cjs?: { + 'import.meta.url'?: boolean; + }; + esm?: { + __filename?: boolean; + __dirname?: boolean; + require?: boolean; + }; + }; + ``` - **Default:** -```js -{ - cjs: { - 'import.meta.url': true, - }, - esm: { - __filename: true, - __dirname: true, - require: false, - }, -} -``` + ```js + { + cjs: { + 'import.meta.url': true, + }, + esm: { + __filename: false, + __dirname: false, + require: false, + }, + } + ``` Used to configure the shims for CommonJS and ESM output. ## shims.cjs -- set to `true` to enable all shims for CommonJS output. -- set to `false` to disable all shims for CommonJS output. -- set the fields to `true` to enable the corresponding shims for CommonJS output. +Set the fields to `true` to enable the corresponding shims for CommonJS output. ### shims.cjs['import.meta.url'] @@ -80,9 +74,7 @@ Options: ## shims.esm -- set to `true` to enable all shims for ESM output. -- set to `false` to disable all shims for ESM output. -- set the fields to `true` to enable the corresponding shims for ESM output. +Set the fields to `true` to enable the corresponding shims for ESM output. ### shims.esm.\_\_filename