Skip to content

Commit f08c714

Browse files
committed
feat: use Rslib bulit-in plugin
1 parent 48ed6bc commit f08c714

File tree

7 files changed

+104
-503
lines changed

7 files changed

+104
-503
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"simple-git-hooks"
7676
],
7777
"overrides": {
78+
"@rspack/core": "link:../rspack/packages/rspack",
7879
"zx>@types/node": "-"
7980
}
8081
}

packages/core/src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ const composeFormatConfig = ({
581581
},
582582
} as const;
583583

584+
// The built-in Rslib plugin will apply to all formats except the `mf` format.
585+
// The `mf` format functions more like an application than a library and requires additional webpack runtime.
586+
const plugins = [
587+
new rspack.RslibPlugin({
588+
interceptApiPlugin: true,
589+
}),
590+
];
591+
584592
switch (format) {
585593
case 'esm':
586594
return {
@@ -613,6 +621,7 @@ const composeFormatConfig = ({
613621
experiments: {
614622
outputModule: true,
615623
},
624+
plugins,
616625
},
617626
},
618627
};
@@ -639,6 +648,7 @@ const composeFormatConfig = ({
639648
workerChunkLoading: 'async-node',
640649
wasmLoading: 'async-node',
641650
},
651+
plugins,
642652
},
643653
},
644654
};
@@ -673,6 +683,7 @@ const composeFormatConfig = ({
673683
optimization: {
674684
nodeEnv: process.env.NODE_ENV,
675685
},
686+
plugins,
676687
},
677688
},
678689
};
@@ -706,6 +717,7 @@ const composeFormatConfig = ({
706717
optimization: {
707718
nodeEnv: process.env.NODE_ENV,
708719
},
720+
plugins,
709721
},
710722
},
711723
};

pnpm-lock.yaml

Lines changed: 46 additions & 503 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "api-plugin-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
// ESM
7+
generateBundleEsmConfig({
8+
output: {
9+
distPath: {
10+
root: './dist/bundle-esm',
11+
},
12+
},
13+
}),
14+
// CJS
15+
generateBundleCjsConfig({
16+
output: {
17+
distPath: {
18+
root: './dist/bundle-cjs',
19+
},
20+
},
21+
}),
22+
],
23+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const c = require.cache;

tests/integration/format/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,18 @@ test('throw error when using mf with `bundle: false`', async () => {
111111
`[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.]`,
112112
);
113113
});
114+
115+
test("API plugin's api should be skipped in parser", async () => {
116+
const fixturePath = path.resolve(__dirname, 'api-plugin');
117+
const { entries } = await buildAndGetResults({
118+
fixturePath,
119+
});
120+
121+
expect(entries.esm).toMatchInlineSnapshot(`
122+
"const c = require.cache;
123+
export { c };
124+
"
125+
`);
126+
127+
expect(entries.cjs).toContain('const c = require.cache;');
128+
});

0 commit comments

Comments
 (0)