Skip to content

Commit 53e832c

Browse files
authored
feat: use Rslib bulit-in plugin (#1080)
1 parent a6b46be commit 53e832c

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

packages/core/src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,14 @@ const composeFormatConfig = ({
578578
},
579579
} as const;
580580

581+
// The built-in Rslib plugin will apply to all formats except the `mf` format.
582+
// The `mf` format functions more like an application than a library and requires additional webpack runtime.
583+
const plugins = [
584+
new rspack.experiments.RslibPlugin({
585+
interceptApiPlugin: true,
586+
}),
587+
];
588+
581589
switch (format) {
582590
case 'esm':
583591
return {
@@ -610,6 +618,7 @@ const composeFormatConfig = ({
610618
experiments: {
611619
outputModule: true,
612620
},
621+
plugins,
613622
},
614623
},
615624
};
@@ -636,6 +645,7 @@ const composeFormatConfig = ({
636645
workerChunkLoading: 'async-node',
637646
wasmLoading: 'async-node',
638647
},
648+
plugins,
639649
},
640650
},
641651
};
@@ -670,6 +680,7 @@ const composeFormatConfig = ({
670680
optimization: {
671681
nodeEnv: process.env.NODE_ENV,
672682
},
683+
plugins,
673684
},
674685
},
675686
};
@@ -703,6 +714,7 @@ const composeFormatConfig = ({
703714
optimization: {
704715
nodeEnv: process.env.NODE_ENV,
705716
},
717+
plugins,
706718
},
707719
},
708720
};

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
209209
"wasmLoading": "fetch",
210210
"workerChunkLoading": "import",
211211
},
212+
"plugins": [
213+
RslibPlugin {
214+
"_args": [
215+
{
216+
"interceptApiPlugin": true,
217+
},
218+
],
219+
"affectedHooks": undefined,
220+
"name": "RslibPlugin",
221+
},
222+
],
212223
},
213224
[Function],
214225
{
@@ -454,6 +465,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
454465
"wasmLoading": "async-node",
455466
"workerChunkLoading": "async-node",
456467
},
468+
"plugins": [
469+
RslibPlugin {
470+
"_args": [
471+
{
472+
"interceptApiPlugin": true,
473+
},
474+
],
475+
"affectedHooks": undefined,
476+
"name": "RslibPlugin",
477+
},
478+
],
457479
},
458480
[Function],
459481
{
@@ -672,6 +694,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
672694
"type": "umd",
673695
},
674696
},
697+
"plugins": [
698+
RslibPlugin {
699+
"_args": [
700+
{
701+
"interceptApiPlugin": true,
702+
},
703+
],
704+
"affectedHooks": undefined,
705+
"name": "RslibPlugin",
706+
},
707+
],
675708
},
676709
[Function],
677710
{
@@ -891,6 +924,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
891924
"type": "modern-module",
892925
},
893926
},
927+
"plugins": [
928+
RslibPlugin {
929+
"_args": [
930+
{
931+
"interceptApiPlugin": true,
932+
},
933+
],
934+
"affectedHooks": undefined,
935+
"name": "RslibPlugin",
936+
},
937+
],
894938
},
895939
[Function],
896940
{

pnpm-lock.yaml

Lines changed: 2 additions & 0 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)