Skip to content

Commit

Permalink
fix: should resolve esnext browserslist
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Sep 11, 2024
1 parent 63bb9ed commit d613c6a
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 159 deletions.
53 changes: 22 additions & 31 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
Format,
LibConfig,
PkgJson,
RsbuildConfigOutputTarget,
RslibConfig,
RslibConfigAsyncFn,
RslibConfigExport,
Expand All @@ -39,7 +40,10 @@ import {
readPackageJson,
} from './utils/helper';
import { logger } from './utils/logger';
import { transformSyntaxToBrowserslist } from './utils/syntax';
import {
ESX_TO_BROWSERSLIST,
transformSyntaxToBrowserslist,
} from './utils/syntax';
import { loadTsconfig } from './utils/tsconfig';

/**
Expand Down Expand Up @@ -551,7 +555,7 @@ const composeAutoExtensionConfig = (

const composeSyntaxConfig = (
syntax?: Syntax,
target?: string,
target?: RsbuildConfigOutputTarget,
): RsbuildConfig => {
// Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.

Expand All @@ -567,24 +571,11 @@ const composeSyntaxConfig = (
},
},
output: {
overrideBrowserslist: transformSyntaxToBrowserslist(syntax),
overrideBrowserslist: transformSyntaxToBrowserslist(syntax, target),
},
};
}

// If `syntax` is not defined, Rslib will try to determine by the `target`, with the last version of the target.
const lastTargetVersions = {
node: ['last 1 node versions'],
web: [
'last 1 Chrome versions',
'last 1 Firefox versions',
'last 1 Edge versions',
'last 1 Safari versions',
'last 1 ios_saf versions',
'not dead',
],
};

return {
tools: {
rspack: (config) => {
Expand All @@ -593,12 +584,8 @@ const composeSyntaxConfig = (
},
},
output: {
overrideBrowserslist:
target === 'web'
? lastTargetVersions.web
: target === 'node'
? lastTargetVersions.node
: [...lastTargetVersions.node, ...lastTargetVersions.web],
// If `syntax` is not defined, Rslib will try to determine by the `target`, with the last version of the target.
overrideBrowserslist: ESX_TO_BROWSERSLIST.esnext(target),
},
};
};
Expand Down Expand Up @@ -695,6 +682,7 @@ const composeBundleConfig = (
? request.replace(/\.[^.]+$/, jsExtension)
: `${request}${jsExtension}`;
}

return callback(null, request);
}
callback();
Expand Down Expand Up @@ -728,7 +716,9 @@ const composeDtsConfig = async (
};
};

const composeTargetConfig = (target = 'web'): RsbuildConfig => {
const composeTargetConfig = (
target: RsbuildConfigOutputTarget = 'web',
): RsbuildConfig => {
switch (target) {
case 'web':
return {
Expand Down Expand Up @@ -756,14 +746,15 @@ const composeTargetConfig = (target = 'web'): RsbuildConfig => {
target: 'node',
},
};
case 'neutral':
return {
tools: {
rspack: {
target: ['web', 'node'],
},
},
};
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
// case 'neutral':
// return {
// tools: {
// rspack: {
// target: ['web', 'node'],
// },
// },
// };
default:
throw new Error(`Unsupported platform: ${target}`);
}
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { RsbuildConfig } from '@rsbuild/core';

export type Format = 'esm' | 'cjs' | 'umd';

export type EcmaScriptVersion =
| 'esnext'
export type FixedEcmaVersions =
| 'es5'
| 'es6'
| 'es2015'
Expand All @@ -14,8 +13,13 @@ export type EcmaScriptVersion =
| 'es2020'
| 'es2021'
| 'es2022'
| 'es2023'
| 'es2024';
| 'es2023';
export type LatestEcmaVersions = 'es2024' | 'esnext';
export type EcmaScriptVersion = FixedEcmaVersions | LatestEcmaVersions;

export type RsbuildConfigOutputTarget = NonNullable<
RsbuildConfig['output']
>['target'];

export type Syntax =
// ECMAScript versions as an common used addition to browserslist query
Expand Down
Loading

0 comments on commit d613c6a

Please sign in to comment.