Skip to content

Commit

Permalink
perf: remove browserslist-to-es-version dependency (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 28, 2024
1 parent b69fa51 commit 13a0b50
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion e2e/cases/browserslist/browserslist-config-mock/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["Chrome >= 49", "Edge >= 79", "Firefox >= 49", "Safari >= 10"]
["Chrome >= 40", "Edge >= 79", "Firefox >= 49", "Safari >= 10"]
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@types/webpack-bundle-analyzer": "4.7.0",
"@types/ws": "^8.5.12",
"browserslist": "4.24.0",
"browserslist-to-es-version": "^1.0.0",
"chokidar": "3.6.0",
"commander": "^12.1.0",
"connect": "3.7.0",
Expand Down
30 changes: 14 additions & 16 deletions packages/core/src/plugins/target.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { DEFAULT_WEB_BROWSERSLIST } from '../constants';
import type { RsbuildPlugin } from '../types';

const getESVersion = async (browserslist: string[]) => {
const { browserslistToESVersion } = await import(
'browserslist-to-es-version'
);

// skip calculation if the browserslist is the default value
if (browserslist.join(',') === DEFAULT_WEB_BROWSERSLIST.join(',')) {
return 2017;
}

return browserslistToESVersion(browserslist);
};

export const pluginTarget = (): RsbuildPlugin => ({
name: 'rsbuild:target',

Expand All @@ -27,14 +14,25 @@ export const pluginTarget = (): RsbuildPlugin => ({
}

const { browserslist } = environment;
const esVersion = await getESVersion(browserslist);

// skip calculation if the browserslist is the default value
const isDefaultBrowserslist =
browserslist.join(',') === DEFAULT_WEB_BROWSERSLIST.join(',');

if (target === 'web-worker') {
chain.target(['webworker', `es${esVersion}`]);
chain.target(
isDefaultBrowserslist
? ['webworker', 'es2017']
: // TODO: Rspack should support `browserslist:` for webworker target
['webworker', 'es5'],
);
return;
}

chain.target(['web', `es${esVersion}`]);
const esQuery = isDefaultBrowserslist
? 'es2017'
: (`browserslist:${browserslist.join(',')}` as const);
chain.target(['web', esQuery]);
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('plugin-target', () => {
},
{
browserslist: ['Chrome 100'],
expected: ['web', 'es2018'],
expected: ['web', 'browserslist:Chrome 100'],
},
{
browserslist: null,
Expand Down
1 change: 0 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"rspack-manifest-plugin": ["./compiled/rspack-manifest-plugin"],
"rsbuild-dev-middleware": ["./compiled/rsbuild-dev-middleware"],
"launch-editor-middleware": ["./compiled/launch-editor-middleware"],
"browserslist-to-es-version": ["./compiled/browserslist-to-es-version"],
"connect-history-api-fallback": [
"./compiled/connect-history-api-fallback"
]
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13a0b50

Please sign in to comment.