Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ship runtime code in es2017 by default #2242

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/compat/plugin-swc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const pluginSwc = (options: PluginSwcOptions = {}): RsbuildPlugin => ({
} else {
applyScriptCondition({
rule: chain.module.rule(CHAIN_ID.RULE.JS),
chain,
config: rsbuildConfig,
context: api.context,
includes: [],
Expand Down
11 changes: 9 additions & 2 deletions packages/core/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default defineConfig({
buildConfig: [
{
...baseBuildConfig.buildConfig,
input: ['src', '!src/client/hmr', '!src/client/overlay.ts'],
input: ['src', '!src/client/hmr', '!src/client/overlay.ts'],
},
{
buildType: 'bundle',
format: 'esm',
target: 'es5',
target: 'es2017',
dts: false,
input: {
hmr: 'src/client/hmr/index.ts',
Expand All @@ -20,6 +20,13 @@ export default defineConfig({
externals: ['./hmr'],
outDir: './dist/client',
autoExtension: true,
externalHelpers: true,
// Skip esbuild transform and only use SWC to transform,
// because esbuild will transform `import.meta`.
esbuildOptions: (options) => {
options.target = undefined;
return options;
},
},
],
});
37 changes: 20 additions & 17 deletions packages/core/src/plugins/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ export const pluginTarget = (): RsbuildPlugin => ({
name: 'rsbuild:target',

setup(api) {
api.modifyBundlerChain(async (chain, { target }) => {
if (target === 'node') {
chain.target('node');
return;
}
api.modifyBundlerChain({
order: 'pre',
handler: async (chain, { target }) => {
if (target === 'node') {
chain.target('node');
return;
}

const config = api.getNormalizedConfig();
const browserslist = await getBrowserslistWithDefault(
api.context.rootPath,
config,
target,
);
const esVersion = browserslistToESVersion(browserslist);
const config = api.getNormalizedConfig();
const browserslist = await getBrowserslistWithDefault(
api.context.rootPath,
config,
target,
);
const esVersion = browserslistToESVersion(browserslist);

if (target === 'web-worker' || target === 'service-worker') {
chain.target(['webworker', `es${esVersion}`]);
return;
}
if (target === 'web-worker' || target === 'service-worker') {
chain.target(['webworker', `es${esVersion}`]);
return;
}

chain.target(['web', `es${esVersion}`]);
chain.target(['web', `es${esVersion}`]);
},
});
},
});
1 change: 1 addition & 0 deletions packages/core/src/provider/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const pluginSwc = (): RsbuildPlugin => ({

applyScriptCondition({
rule,
chain,
config,
context: api.context,
includes: [],
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@ export type ChainIdentifier = typeof CHAIN_ID;

export function applyScriptCondition({
rule,
chain,
config,
context,
includes,
excludes,
}: {
rule: BundlerChainRule;
chain: BundlerChain;
config: NormalizedConfig;
context: RsbuildContext;
includes: (string | RegExp)[];
Expand All @@ -260,6 +262,14 @@ export function applyScriptCondition({
// Otherwise, it will lead to compilation errors and incorrect output.
rule.include.add(TS_AND_JSX_REGEX);

// 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))) {
rule.include.add(/[\\/]@rsbuild[\\/]core[\\/]/);
}

for (const condition of [...includes, ...(config.source.include || [])]) {
rule.include.add(condition);
}
Expand Down
Loading