diff --git a/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap b/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap index 1d01ced146..8b9a70a4cf 100644 --- a/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap +++ b/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap @@ -80,6 +80,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = ` ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", diff --git a/packages/core/src/plugins/swc.ts b/packages/core/src/plugins/swc.ts index a27384ad55..860e9e7476 100644 --- a/packages/core/src/plugins/swc.ts +++ b/packages/core/src/plugins/swc.ts @@ -15,6 +15,7 @@ import type { Polyfill, RsbuildContext, RsbuildPlugin, + RsbuildTarget, RspackChain, TransformImport, } from '../types'; @@ -23,42 +24,36 @@ const builtinSwcLoaderName = 'builtin:swc-loader'; function applyScriptCondition({ rule, - chain, + isDev, config, context, - includes, - excludes, + rsbuildTarget, }: { rule: RspackChain.Rule; - chain: RspackChain; + isDev: boolean; config: NormalizedEnvironmentConfig; context: RsbuildContext; - includes: (string | RegExp)[]; - excludes: (string | RegExp)[]; + rsbuildTarget: RsbuildTarget; }): void { - // compile all folders in app directory, exclude node_modules - // which can be removed next version of rspack + // compile all modules in the app directory, exclude node_modules rule.include.add({ and: [context.rootPath, { not: NODE_MODULES_REGEX }], }); - // Always compile TS and JSX files. - // Otherwise, it will lead to compilation errors and incorrect output. + // always compile TS and JSX files. + // otherwise, it may cause compilation errors and incorrect output rule.include.add(/\.(?:ts|tsx|jsx|mts|cts)$/); - // The Rsbuild runtime code is es2017 by default, - // transform the runtime code if user target < es2017 - const target = castArray(chain.get('target')); - const legacyTarget = ['es5', 'es6', 'es2015', 'es2016']; - if (legacyTarget.some((item) => target.includes(item))) { + // transform the Rsbuild runtime code to support legacy browsers + if (rsbuildTarget === 'web' && isDev) { rule.include.add(/[\\/]@rsbuild[\\/]core[\\/]dist[\\/]/); } - for (const condition of [...includes, ...(config.source.include || [])]) { + for (const condition of config.source.include || []) { rule.include.add(condition); } - for (const condition of [...excludes, ...(config.source.exclude || [])]) { + for (const condition of config.source.exclude || []) { rule.exclude.add(condition); } } @@ -95,7 +90,7 @@ export const pluginSwc = (): RsbuildPlugin => ({ setup(api) { api.modifyBundlerChain({ order: 'pre', - handler: async (chain, { CHAIN_ID, target, environment }) => { + handler: async (chain, { CHAIN_ID, isDev, target, environment }) => { const { config, browserslist } = environment; const cacheRoot = path.join(api.context.cachePath, '.swc'); @@ -112,11 +107,10 @@ export const pluginSwc = (): RsbuildPlugin => ({ applyScriptCondition({ rule, - chain, + isDev, config, context: api.context, - includes: [], - excludes: [], + rsbuildTarget: target, }); // Rspack builtin SWC is not suitable for webpack diff --git a/packages/core/tests/__snapshots__/builder.test.ts.snap b/packages/core/tests/__snapshots__/builder.test.ts.snap index 80266aeafb..c8f439e194 100644 --- a/packages/core/tests/__snapshots__/builder.test.ts.snap +++ b/packages/core/tests/__snapshots__/builder.test.ts.snap @@ -78,6 +78,7 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = ` ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", diff --git a/packages/core/tests/__snapshots__/default.test.ts.snap b/packages/core/tests/__snapshots__/default.test.ts.snap index 2b358376bb..0198b7185f 100644 --- a/packages/core/tests/__snapshots__/default.test.ts.snap +++ b/packages/core/tests/__snapshots__/default.test.ts.snap @@ -78,6 +78,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = ` ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", @@ -1240,6 +1241,7 @@ exports[`tools.rspack > should match snapshot 1`] = ` ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap index d11f31f7ac..3811ad6bd7 100644 --- a/packages/core/tests/__snapshots__/environments.test.ts.snap +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -1468,6 +1468,7 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", diff --git a/packages/plugin-react/tests/__snapshots__/index.test.ts.snap b/packages/plugin-react/tests/__snapshots__/index.test.ts.snap index 05f25963fc..97a6ac20fc 100644 --- a/packages/plugin-react/tests/__snapshots__/index.test.ts.snap +++ b/packages/plugin-react/tests/__snapshots__/index.test.ts.snap @@ -19,6 +19,7 @@ exports[`plugins/react > should configuring \`tools.swc\` to override react runt ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto", @@ -97,6 +98,7 @@ exports[`plugins/react > should work with swc-loader 1`] = ` ], }, /\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/, + /\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/, ], "test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/, "type": "javascript/auto",