diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index d137d0219..8ae913792 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -578,6 +578,14 @@ const composeFormatConfig = ({ }, } as const; + // The built-in Rslib plugin will apply to all formats except the `mf` format. + // The `mf` format functions more like an application than a library and requires additional webpack runtime. + const plugins = [ + new rspack.experiments.RslibPlugin({ + interceptApiPlugin: true, + }), + ]; + switch (format) { case 'esm': return { @@ -610,6 +618,7 @@ const composeFormatConfig = ({ experiments: { outputModule: true, }, + plugins, }, }, }; @@ -636,6 +645,7 @@ const composeFormatConfig = ({ workerChunkLoading: 'async-node', wasmLoading: 'async-node', }, + plugins, }, }, }; @@ -670,6 +680,7 @@ const composeFormatConfig = ({ optimization: { nodeEnv: process.env.NODE_ENV, }, + plugins, }, }, }; @@ -703,6 +714,7 @@ const composeFormatConfig = ({ optimization: { nodeEnv: process.env.NODE_ENV, }, + plugins, }, }, }; diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index 0ced864d6..39e693a73 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -209,6 +209,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "wasmLoading": "fetch", "workerChunkLoading": "import", }, + "plugins": [ + RslibPlugin { + "_args": [ + { + "interceptApiPlugin": true, + }, + ], + "affectedHooks": undefined, + "name": "RslibPlugin", + }, + ], }, [Function], { @@ -454,6 +465,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "wasmLoading": "async-node", "workerChunkLoading": "async-node", }, + "plugins": [ + RslibPlugin { + "_args": [ + { + "interceptApiPlugin": true, + }, + ], + "affectedHooks": undefined, + "name": "RslibPlugin", + }, + ], }, [Function], { @@ -672,6 +694,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "type": "umd", }, }, + "plugins": [ + RslibPlugin { + "_args": [ + { + "interceptApiPlugin": true, + }, + ], + "affectedHooks": undefined, + "name": "RslibPlugin", + }, + ], }, [Function], { @@ -891,6 +924,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i "type": "modern-module", }, }, + "plugins": [ + RslibPlugin { + "_args": [ + { + "interceptApiPlugin": true, + }, + ], + "affectedHooks": undefined, + "name": "RslibPlugin", + }, + ], }, [Function], { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 19ab5b462..35141bb34 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -835,6 +835,8 @@ importers: specifier: ^4.17.18 version: 4.17.18 + tests/integration/format/api-plugin: {} + tests/integration/format/cjs-static-export: {} tests/integration/format/default: {} diff --git a/tests/integration/format/api-plugin/package.json b/tests/integration/format/api-plugin/package.json new file mode 100644 index 000000000..bb5846304 --- /dev/null +++ b/tests/integration/format/api-plugin/package.json @@ -0,0 +1,6 @@ +{ + "name": "api-plugin-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/format/api-plugin/rslib.config.ts b/tests/integration/format/api-plugin/rslib.config.ts new file mode 100644 index 000000000..63bbbda43 --- /dev/null +++ b/tests/integration/format/api-plugin/rslib.config.ts @@ -0,0 +1,23 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + // ESM + generateBundleEsmConfig({ + output: { + distPath: { + root: './dist/bundle-esm', + }, + }, + }), + // CJS + generateBundleCjsConfig({ + output: { + distPath: { + root: './dist/bundle-cjs', + }, + }, + }), + ], +}); diff --git a/tests/integration/format/api-plugin/src/index.js b/tests/integration/format/api-plugin/src/index.js new file mode 100644 index 000000000..8fe1b9762 --- /dev/null +++ b/tests/integration/format/api-plugin/src/index.js @@ -0,0 +1 @@ +export const c = require.cache; diff --git a/tests/integration/format/index.test.ts b/tests/integration/format/index.test.ts index 9876f31f4..4092bf1ba 100644 --- a/tests/integration/format/index.test.ts +++ b/tests/integration/format/index.test.ts @@ -111,3 +111,18 @@ test('throw error when using mf with `bundle: false`', async () => { `[Error: When using "mf" format, "bundle" must be set to "true". Since the default value for "bundle" is "true", so you can either explicitly set it to "true" or remove the field entirely.]`, ); }); + +test("API plugin's api should be skipped in parser", async () => { + const fixturePath = path.resolve(__dirname, 'api-plugin'); + const { entries } = await buildAndGetResults({ + fixturePath, + }); + + expect(entries.esm).toMatchInlineSnapshot(` + "const c = require.cache; + export { c }; + " + `); + + expect(entries.cjs).toContain('const c = require.cache;'); +});