Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Jan 5, 2024
1 parent 7b811c7 commit 782a29b
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 110 deletions.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
},
"dependencies": {
"@rsbuild/shared": "workspace:*",
"@rspack/core": "0.4.5-canary-45f100a-20240104044121",
"@rspack/core": "0.4.5-canary-be59383-20240105051840",
"@swc/helpers": "0.5.3",
"core-js": "~3.32.2",
"html-webpack-plugin": "npm:[email protected]",
"postcss": "8.4.31"
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/provider/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export const pluginSwc = (): RsbuildPlugin => ({

applyDecorator(swcConfig, config.output.enableLatestDecorators);

if (swcConfig.jsc?.externalHelpers) {
chain.resolve.alias.set(
'@swc/helpers',
path.dirname(require.resolve('@swc/helpers/package.json')),
);
}

// apply polyfill
if (isWebTarget(target)) {
const polyfillMode = config.output.polyfill;
Expand Down
7 changes: 4 additions & 3 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
},
},
"context": "<ROOT>",
"devServer": {
"hot": true,
},
"devtool": "cheap-module-source-map",
"entry": {
"index": [
Expand Down Expand Up @@ -666,9 +663,13 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
RemoveCssSourcemapPlugin {
"name": "RemoveCssSourcemapPlugin",
},
HotModuleReplacementPlugin {
"name": "HotModuleReplacementPlugin",
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
"extensions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
},
"context": "<ROOT>",
"devServer": {
"hot": true,
},
"devtool": "cheap-module-source-map",
"entry": {
"index": [
Expand Down Expand Up @@ -666,9 +663,13 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
RemoveCssSourcemapPlugin {
"name": "RemoveCssSourcemapPlugin",
},
HotModuleReplacementPlugin {
"name": "HotModuleReplacementPlugin",
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
"extensions": [
Expand Down Expand Up @@ -699,9 +700,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
},
},
"context": "<ROOT>",
"devServer": {
"hot": false,
},
"devtool": false,
"entry": {
"index": [
Expand Down Expand Up @@ -1277,7 +1275,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"_options": {
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
"exclude": undefined,
"extractComments": "true",
"extractComments": {
"condition": "@preserve|@lic|@cc_on|^/**!",
},
"format": "{"comments":false,"asciiOnly":true}",
"include": undefined,
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
Expand Down Expand Up @@ -1404,6 +1404,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
"extensions": [
Expand Down Expand Up @@ -1434,9 +1435,6 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
},
"context": "<ROOT>",
"devServer": {
"hot": false,
},
"devtool": "cheap-module-source-map",
"entry": {
"index": [
Expand Down Expand Up @@ -1837,6 +1835,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"conditionNames": [
"require",
"node",
Expand Down Expand Up @@ -1866,9 +1867,6 @@ exports[`tools.rspack > should match snapshot 1`] = `
},
},
"context": "<ROOT>",
"devServer": {
"hot": true,
},
"devtool": "cheap-module-source-map",
"entry": {
"index": [
Expand Down Expand Up @@ -2531,9 +2529,13 @@ exports[`tools.rspack > should match snapshot 1`] = `
RemoveCssSourcemapPlugin {
"name": "RemoveCssSourcemapPlugin",
},
HotModuleReplacementPlugin {
"name": "HotModuleReplacementPlugin",
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
"extensions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ exports[`plugin-swc > should add antd pluginImport 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -205,6 +206,7 @@ exports[`plugin-swc > should add browserslist 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -309,6 +311,7 @@ exports[`plugin-swc > should add browserslist 2`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -433,6 +436,7 @@ exports[`plugin-swc > should add pluginImport 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -542,6 +546,7 @@ exports[`plugin-swc > should disable all pluginImport 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -637,6 +642,11 @@ exports[`plugin-swc > should disable preset_env in target other than web 1`] = `
},
],
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -738,6 +748,11 @@ exports[`plugin-swc > should disable preset_env mode 1`] = `
},
],
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -844,6 +859,7 @@ exports[`plugin-swc > should enable entry mode preset_env 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -954,6 +970,7 @@ exports[`plugin-swc > should enable usage mode preset_env 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -1063,6 +1080,7 @@ exports[`plugin-swc > should has correct core-js 1`] = `
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down Expand Up @@ -1159,6 +1177,11 @@ exports[`plugin-swc > should has correct core-js 2`] = `
},
],
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1266,6 +1289,7 @@ exports[`plugin-swc > should'n override browserslist when target platform is not
},
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
},
},
Expand Down
16 changes: 12 additions & 4 deletions packages/core/tests/rspack-provider/plugins/minimize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe('plugin-minimize', () => {
"_options": {
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
"exclude": undefined,
"extractComments": "true",
"extractComments": {
"condition": "@preserve|@lic|@cc_on|^/**!",
},
"format": "{"comments":false,"asciiOnly":true}",
"include": undefined,
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
Expand Down Expand Up @@ -97,7 +99,9 @@ describe('plugin-minimize', () => {
"_options": {
"compress": "{"passes":1,"pure_funcs":[],"drop_console":true}",
"exclude": undefined,
"extractComments": "true",
"extractComments": {
"condition": "@preserve|@lic|@cc_on|^/**!",
},
"format": "{"comments":false,"asciiOnly":true}",
"include": undefined,
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
Expand Down Expand Up @@ -140,7 +144,9 @@ describe('plugin-minimize', () => {
"_options": {
"compress": "{"passes":1,"pure_funcs":["console.log","console.warn"],"drop_console":false}",
"exclude": undefined,
"extractComments": "true",
"extractComments": {
"condition": "@preserve|@lic|@cc_on|^/**!",
},
"format": "{"comments":false,"asciiOnly":true}",
"include": undefined,
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
Expand Down Expand Up @@ -183,7 +189,9 @@ describe('plugin-minimize', () => {
"_options": {
"compress": "{"passes":1,"pure_funcs":[],"drop_console":false}",
"exclude": undefined,
"extractComments": "true",
"extractComments": {
"condition": "@preserve|@lic|@cc_on|^/**!",
},
"format": "{"comments":false,"asciiOnly":false}",
"include": undefined,
"mangle": "{"keep_classnames":false,"keep_fnames":false}",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@rsbuild/shared": "workspace:*",
"@rspack/plugin-react-refresh": "0.4.5-canary-45f100a-20240104044121",
"@rspack/plugin-react-refresh": "0.4.5-canary-be59383-20240105051840",
"react-refresh": "^0.14.0"
},
"devDependencies": {
Expand Down
49 changes: 7 additions & 42 deletions packages/plugin-react/src/react.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
import { isUsingHMR, isClientCompiler, isProd } from '@rsbuild/shared';
import type { Rspack, RsbuildPluginAPI } from '@rsbuild/core';
import path from 'node:path';
import { isUsingHMR } from '@rsbuild/shared';
import type { RsbuildPluginAPI } from '@rsbuild/core';

function getReactRefreshEntry(compiler: Rspack.Compiler) {
const hot = compiler.options.devServer?.hot ?? true;
const refresh = compiler.options.builtins?.react?.refresh ?? true;

if (hot && refresh) {
const reactRefreshEntryPath = require.resolve(
'@rspack/plugin-react-refresh/react-refresh-entry',
);
return reactRefreshEntryPath;
}

return null;
}

const setupCompiler = (compiler: Rspack.Compiler) => {
if (!isClientCompiler(compiler)) {
return;
}

const reactRefreshEntry = getReactRefreshEntry(compiler);
if (!reactRefreshEntry) {
return;
}

new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntry, {
name: undefined,
}).apply(compiler);
};
export const REACT_REFRESH_PATH = require.resolve('react-refresh');
const REACT_REFRESH_DIR_PATH = path.dirname(REACT_REFRESH_PATH);

export const applyBasicReactSupport = (api: RsbuildPluginAPI) => {
api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
if (isProd()) {
return;
}

if ((multiCompiler as Rspack.MultiCompiler).compilers) {
(multiCompiler as Rspack.MultiCompiler).compilers.forEach(setupCompiler);
} else {
setupCompiler(multiCompiler as Rspack.Compiler);
}
});

api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd, target }) => {
const config = api.getNormalizedConfig();
const usingHMR = isUsingHMR(config, { isProd, target });
Expand Down Expand Up @@ -77,6 +40,8 @@ export const applyBasicReactSupport = (api: RsbuildPluginAPI) => {
return;
}

chain.resolve.alias.set('react-refresh', REACT_REFRESH_DIR_PATH);

const { default: ReactRefreshRspackPlugin } = await import(
'@rspack/plugin-react-refresh'
);
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-react/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ exports[`plugins/react > should work with swc-loader 1`] = `
},
},
"context": "<ROOT>",
"devServer": {
"hot": true,
},
"devtool": "cheap-module-source-map",
"entry": {
"index": [
Expand Down Expand Up @@ -704,6 +701,9 @@ exports[`plugins/react > should work with swc-loader 1`] = `
RemoveCssSourcemapPlugin {
"name": "RemoveCssSourcemapPlugin",
},
HotModuleReplacementPlugin {
"name": "HotModuleReplacementPlugin",
},
ReactRefreshRspackPlugin {
"options": {
"exclude": /node_modules/i,
Expand All @@ -713,7 +713,9 @@ exports[`plugins/react > should work with swc-loader 1`] = `
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"core-js": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"react-refresh": "<ROOT>/node_modules/<PNPM_INNER>/react-refresh",
},
"extensions": [
".ts",
Expand Down
Loading

0 comments on commit 782a29b

Please sign in to comment.