Skip to content

Commit 782a29b

Browse files
committed
fix
1 parent 7b811c7 commit 782a29b

File tree

14 files changed

+151
-110
lines changed

14 files changed

+151
-110
lines changed

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
},
6262
"dependencies": {
6363
"@rsbuild/shared": "workspace:*",
64-
"@rspack/core": "0.4.5-canary-45f100a-20240104044121",
64+
"@rspack/core": "0.4.5-canary-be59383-20240105051840",
65+
"@swc/helpers": "0.5.3",
6566
"core-js": "~3.32.2",
6667
"html-webpack-plugin": "npm:[email protected]",
6768
"postcss": "8.4.31"

packages/core/src/provider/plugins/swc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ export const pluginSwc = (): RsbuildPlugin => ({
7979

8080
applyDecorator(swcConfig, config.output.enableLatestDecorators);
8181

82+
if (swcConfig.jsc?.externalHelpers) {
83+
chain.resolve.alias.set(
84+
'@swc/helpers',
85+
path.dirname(require.resolve('@swc/helpers/package.json')),
86+
);
87+
}
88+
8289
// apply polyfill
8390
if (isWebTarget(target)) {
8491
const polyfillMode = config.output.polyfill;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
1212
},
1313
},
1414
"context": "<ROOT>",
15-
"devServer": {
16-
"hot": true,
17-
},
1815
"devtool": "cheap-module-source-map",
1916
"entry": {
2017
"index": [
@@ -666,9 +663,13 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
666663
RemoveCssSourcemapPlugin {
667664
"name": "RemoveCssSourcemapPlugin",
668665
},
666+
HotModuleReplacementPlugin {
667+
"name": "HotModuleReplacementPlugin",
668+
},
669669
],
670670
"resolve": {
671671
"alias": {
672+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
672673
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
673674
},
674675
"extensions": [

packages/core/tests/rspack-provider/plugins/__snapshots__/default.test.ts.snap

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
1212
},
1313
},
1414
"context": "<ROOT>",
15-
"devServer": {
16-
"hot": true,
17-
},
1815
"devtool": "cheap-module-source-map",
1916
"entry": {
2017
"index": [
@@ -666,9 +663,13 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
666663
RemoveCssSourcemapPlugin {
667664
"name": "RemoveCssSourcemapPlugin",
668665
},
666+
HotModuleReplacementPlugin {
667+
"name": "HotModuleReplacementPlugin",
668+
},
669669
],
670670
"resolve": {
671671
"alias": {
672+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
672673
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
673674
},
674675
"extensions": [
@@ -699,9 +700,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
699700
},
700701
},
701702
"context": "<ROOT>",
702-
"devServer": {
703-
"hot": false,
704-
},
705703
"devtool": false,
706704
"entry": {
707705
"index": [
@@ -1277,7 +1275,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
12771275
"_options": {
12781276
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
12791277
"exclude": undefined,
1280-
"extractComments": "true",
1278+
"extractComments": {
1279+
"condition": "@preserve|@lic|@cc_on|^/**!",
1280+
},
12811281
"format": "{"comments":false,"asciiOnly":true}",
12821282
"include": undefined,
12831283
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
@@ -1404,6 +1404,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
14041404
],
14051405
"resolve": {
14061406
"alias": {
1407+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
14071408
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
14081409
},
14091410
"extensions": [
@@ -1434,9 +1435,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
14341435
},
14351436
},
14361437
"context": "<ROOT>",
1437-
"devServer": {
1438-
"hot": false,
1439-
},
14401438
"devtool": "cheap-module-source-map",
14411439
"entry": {
14421440
"index": [
@@ -1837,6 +1835,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
18371835
},
18381836
],
18391837
"resolve": {
1838+
"alias": {
1839+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
1840+
},
18401841
"conditionNames": [
18411842
"require",
18421843
"node",
@@ -1866,9 +1867,6 @@ exports[`tools.rspack > should match snapshot 1`] = `
18661867
},
18671868
},
18681869
"context": "<ROOT>",
1869-
"devServer": {
1870-
"hot": true,
1871-
},
18721870
"devtool": "cheap-module-source-map",
18731871
"entry": {
18741872
"index": [
@@ -2531,9 +2529,13 @@ exports[`tools.rspack > should match snapshot 1`] = `
25312529
RemoveCssSourcemapPlugin {
25322530
"name": "RemoveCssSourcemapPlugin",
25332531
},
2532+
HotModuleReplacementPlugin {
2533+
"name": "HotModuleReplacementPlugin",
2534+
},
25342535
],
25352536
"resolve": {
25362537
"alias": {
2538+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
25372539
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
25382540
},
25392541
"extensions": [

packages/core/tests/rspack-provider/plugins/__snapshots__/swc.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ exports[`plugin-swc > should add antd pluginImport 1`] = `
102102
},
103103
"resolve": {
104104
"alias": {
105+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
105106
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
106107
},
107108
},
@@ -205,6 +206,7 @@ exports[`plugin-swc > should add browserslist 1`] = `
205206
},
206207
"resolve": {
207208
"alias": {
209+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
208210
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
209211
},
210212
},
@@ -309,6 +311,7 @@ exports[`plugin-swc > should add browserslist 2`] = `
309311
},
310312
"resolve": {
311313
"alias": {
314+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
312315
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
313316
},
314317
},
@@ -433,6 +436,7 @@ exports[`plugin-swc > should add pluginImport 1`] = `
433436
},
434437
"resolve": {
435438
"alias": {
439+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
436440
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
437441
},
438442
},
@@ -542,6 +546,7 @@ exports[`plugin-swc > should disable all pluginImport 1`] = `
542546
},
543547
"resolve": {
544548
"alias": {
549+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
545550
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
546551
},
547552
},
@@ -637,6 +642,11 @@ exports[`plugin-swc > should disable preset_env in target other than web 1`] = `
637642
},
638643
],
639644
},
645+
"resolve": {
646+
"alias": {
647+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
648+
},
649+
},
640650
},
641651
]
642652
`;
@@ -738,6 +748,11 @@ exports[`plugin-swc > should disable preset_env mode 1`] = `
738748
},
739749
],
740750
},
751+
"resolve": {
752+
"alias": {
753+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
754+
},
755+
},
741756
},
742757
]
743758
`;
@@ -844,6 +859,7 @@ exports[`plugin-swc > should enable entry mode preset_env 1`] = `
844859
},
845860
"resolve": {
846861
"alias": {
862+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
847863
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
848864
},
849865
},
@@ -954,6 +970,7 @@ exports[`plugin-swc > should enable usage mode preset_env 1`] = `
954970
},
955971
"resolve": {
956972
"alias": {
973+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
957974
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
958975
},
959976
},
@@ -1063,6 +1080,7 @@ exports[`plugin-swc > should has correct core-js 1`] = `
10631080
},
10641081
"resolve": {
10651082
"alias": {
1083+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
10661084
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
10671085
},
10681086
},
@@ -1159,6 +1177,11 @@ exports[`plugin-swc > should has correct core-js 2`] = `
11591177
},
11601178
],
11611179
},
1180+
"resolve": {
1181+
"alias": {
1182+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
1183+
},
1184+
},
11621185
},
11631186
]
11641187
`;
@@ -1266,6 +1289,7 @@ exports[`plugin-swc > should'n override browserslist when target platform is not
12661289
},
12671290
"resolve": {
12681291
"alias": {
1292+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
12691293
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
12701294
},
12711295
},

packages/core/tests/rspack-provider/plugins/minimize.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ describe('plugin-minimize', () => {
3535
"_options": {
3636
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
3737
"exclude": undefined,
38-
"extractComments": "true",
38+
"extractComments": {
39+
"condition": "@preserve|@lic|@cc_on|^/**!",
40+
},
3941
"format": "{"comments":false,"asciiOnly":true}",
4042
"include": undefined,
4143
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
@@ -97,7 +99,9 @@ describe('plugin-minimize', () => {
9799
"_options": {
98100
"compress": "{"passes":1,"pure_funcs":[],"drop_console":true}",
99101
"exclude": undefined,
100-
"extractComments": "true",
102+
"extractComments": {
103+
"condition": "@preserve|@lic|@cc_on|^/**!",
104+
},
101105
"format": "{"comments":false,"asciiOnly":true}",
102106
"include": undefined,
103107
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
@@ -140,7 +144,9 @@ describe('plugin-minimize', () => {
140144
"_options": {
141145
"compress": "{"passes":1,"pure_funcs":["console.log","console.warn"],"drop_console":false}",
142146
"exclude": undefined,
143-
"extractComments": "true",
147+
"extractComments": {
148+
"condition": "@preserve|@lic|@cc_on|^/**!",
149+
},
144150
"format": "{"comments":false,"asciiOnly":true}",
145151
"include": undefined,
146152
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
@@ -183,7 +189,9 @@ describe('plugin-minimize', () => {
183189
"_options": {
184190
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
185191
"exclude": undefined,
186-
"extractComments": "true",
192+
"extractComments": {
193+
"condition": "@preserve|@lic|@cc_on|^/**!",
194+
},
187195
"format": "{"comments":false,"asciiOnly":false}",
188196
"include": undefined,
189197
"mangle": "{"keep_classnames":false,"keep_fnames":false}",

packages/plugin-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"dependencies": {
2929
"@rsbuild/shared": "workspace:*",
30-
"@rspack/plugin-react-refresh": "0.4.5-canary-45f100a-20240104044121",
30+
"@rspack/plugin-react-refresh": "0.4.5-canary-be59383-20240105051840",
3131
"react-refresh": "^0.14.0"
3232
},
3333
"devDependencies": {

packages/plugin-react/src/react.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
1-
import { isUsingHMR, isClientCompiler, isProd } from '@rsbuild/shared';
2-
import type { Rspack, RsbuildPluginAPI } from '@rsbuild/core';
1+
import path from 'node:path';
2+
import { isUsingHMR } from '@rsbuild/shared';
3+
import type { RsbuildPluginAPI } from '@rsbuild/core';
34

4-
function getReactRefreshEntry(compiler: Rspack.Compiler) {
5-
const hot = compiler.options.devServer?.hot ?? true;
6-
const refresh = compiler.options.builtins?.react?.refresh ?? true;
7-
8-
if (hot && refresh) {
9-
const reactRefreshEntryPath = require.resolve(
10-
'@rspack/plugin-react-refresh/react-refresh-entry',
11-
);
12-
return reactRefreshEntryPath;
13-
}
14-
15-
return null;
16-
}
17-
18-
const setupCompiler = (compiler: Rspack.Compiler) => {
19-
if (!isClientCompiler(compiler)) {
20-
return;
21-
}
22-
23-
const reactRefreshEntry = getReactRefreshEntry(compiler);
24-
if (!reactRefreshEntry) {
25-
return;
26-
}
27-
28-
new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntry, {
29-
name: undefined,
30-
}).apply(compiler);
31-
};
5+
export const REACT_REFRESH_PATH = require.resolve('react-refresh');
6+
const REACT_REFRESH_DIR_PATH = path.dirname(REACT_REFRESH_PATH);
327

338
export const applyBasicReactSupport = (api: RsbuildPluginAPI) => {
34-
api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
35-
if (isProd()) {
36-
return;
37-
}
38-
39-
if ((multiCompiler as Rspack.MultiCompiler).compilers) {
40-
(multiCompiler as Rspack.MultiCompiler).compilers.forEach(setupCompiler);
41-
} else {
42-
setupCompiler(multiCompiler as Rspack.Compiler);
43-
}
44-
});
45-
469
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd, target }) => {
4710
const config = api.getNormalizedConfig();
4811
const usingHMR = isUsingHMR(config, { isProd, target });
@@ -77,6 +40,8 @@ export const applyBasicReactSupport = (api: RsbuildPluginAPI) => {
7740
return;
7841
}
7942

43+
chain.resolve.alias.set('react-refresh', REACT_REFRESH_DIR_PATH);
44+
8045
const { default: ReactRefreshRspackPlugin } = await import(
8146
'@rspack/plugin-react-refresh'
8247
);

packages/plugin-react/tests/__snapshots__/index.test.ts.snap

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ exports[`plugins/react > should work with swc-loader 1`] = `
2929
},
3030
},
3131
"context": "<ROOT>",
32-
"devServer": {
33-
"hot": true,
34-
},
3532
"devtool": "cheap-module-source-map",
3633
"entry": {
3734
"index": [
@@ -704,6 +701,9 @@ exports[`plugins/react > should work with swc-loader 1`] = `
704701
RemoveCssSourcemapPlugin {
705702
"name": "RemoveCssSourcemapPlugin",
706703
},
704+
HotModuleReplacementPlugin {
705+
"name": "HotModuleReplacementPlugin",
706+
},
707707
ReactRefreshRspackPlugin {
708708
"options": {
709709
"exclude": /node_modules/i,
@@ -713,7 +713,9 @@ exports[`plugins/react > should work with swc-loader 1`] = `
713713
],
714714
"resolve": {
715715
"alias": {
716+
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
716717
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
718+
"react-refresh": "<ROOT>/node_modules/<PNPM_INNER>/react-refresh",
717719
},
718720
"extensions": [
719721
".ts",

0 commit comments

Comments
 (0)