Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -610,6 +618,7 @@ const composeFormatConfig = ({
experiments: {
outputModule: true,
},
plugins,
},
},
};
Expand All @@ -636,6 +645,7 @@ const composeFormatConfig = ({
workerChunkLoading: 'async-node',
wasmLoading: 'async-node',
},
plugins,
},
},
};
Expand Down Expand Up @@ -670,6 +680,7 @@ const composeFormatConfig = ({
optimization: {
nodeEnv: process.env.NODE_ENV,
},
plugins,
},
},
};
Expand Down Expand Up @@ -703,6 +714,7 @@ const composeFormatConfig = ({
optimization: {
nodeEnv: process.env.NODE_ENV,
},
plugins,
},
},
};
Expand Down
44 changes: 44 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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],
{
Expand Down Expand Up @@ -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],
{
Expand Down Expand Up @@ -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],
{
Expand Down Expand Up @@ -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],
{
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/format/api-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "api-plugin-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
23 changes: 23 additions & 0 deletions tests/integration/format/api-plugin/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/format/api-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c = require.cache;
15 changes: 15 additions & 0 deletions tests/integration/format/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;');
});
Loading