Skip to content

Commit

Permalink
fix: failed to transform Rsbuild runtime code
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Oct 8, 2024
1 parent aca1290 commit c70d5ad
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 15 additions & 21 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
Polyfill,
RsbuildContext,
RsbuildPlugin,
RsbuildTarget,
RspackChain,
TransformImport,
} from '../types';
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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');

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-react/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit c70d5ad

Please sign in to comment.