Skip to content

Commit

Permalink
chore(deps): switch to browserslist-to-es-version (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jul 2, 2024
1 parent bb2a11d commit e0a1f8d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 170 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/node": "18.x",
"@types/on-finished": "2.3.4",
"@types/ws": "^8.5.10",
"browserslist-to-es-version": "^1.0.0",
"commander": "^12.1.0",
"connect": "3.7.0",
"connect-history-api-fallback": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/target.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { browserslistToESVersion } from '@rsbuild/shared';
import * as toESVersion from 'browserslist-to-es-version';
import type { RsbuildPlugin } from '../types';

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

const { browserslist } = environment;
const esVersion = browserslistToESVersion(browserslist);
const esVersion = toESVersion.browserslistToESVersion(browserslist);

if (target === 'web-worker') {
chain.target(['webworker', `es${esVersion}`]);
Expand Down
1 change: 1 addition & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"rspack-manifest-plugin": ["./compiled/rspack-manifest-plugin"],
"webpack-dev-middleware": ["./compiled/webpack-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
1 change: 1 addition & 0 deletions packages/plugin-check-syntax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@rsbuild/shared": "workspace:*",
"acorn": "^8.12.0",
"browserslist-to-es-version": "^1.0.0",
"htmlparser2": "9.1.0",
"source-map": "^0.7.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-check-syntax/src/CheckSyntaxPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'node:fs';
import { resolve } from 'node:path';
import type { Rspack } from '@rsbuild/core';
import { browserslistToESVersion } from '@rsbuild/shared';
import { parse } from 'acorn';
import { browserslistToESVersion } from 'browserslist-to-es-version';
import {
checkIsExcludeSource,
generateError,
Expand Down
72 changes: 0 additions & 72 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browserslist from '../compiled/browserslist/index.js';
import deepmerge from '../compiled/deepmerge/index.js';
import color from '../compiled/picocolors/index.js';
import RspackChain from '../compiled/rspack-chain/index.js';
Expand All @@ -9,77 +8,6 @@ export * from './types';
// RegExp
export const SCRIPT_REGEX: RegExp = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;

enum ESVersion {
es5 = 5,
es2015 = 2015,
es2016 = 2016,
es2017 = 2017,
es2018 = 2018,
}

// the minimal version for [es2015, es2016, es2017, es2018]
const ES_VERSIONS_MAP: Record<string, number[]> = {
chrome: [51, 52, 57, 64],
edge: [15, 15, 15, 79],
safari: [10, 10.3, 11, 16.4],
firefox: [54, 54, 54, 78],
opera: [38, 39, 44, 51],
samsung: [5, 6.2, 6.2, 8.2],
};

const renameBrowser = (name: string) => {
return name === 'ios_saf' ? 'safari' : name;
};

export function browserslistToESVersion(browsers: string[]): ESVersion {
const projectBrowsers = browserslist(browsers, {
ignoreUnknownVersions: true,
});

let esVersion: ESVersion = ESVersion.es2018;

for (const item of projectBrowsers) {
const pairs = item.split(' ');

// skip invalid item
if (pairs.length < 2) {
continue;
}

const browser = renameBrowser(pairs[0]);
const version = Number(pairs[1].split('-')[0]);

// ignore unknown version
if (Number.isNaN(version)) {
continue;
}

// IE / Android 4.x ~ 5.x only supports es5
if (browser === 'ie' || (browser === 'android' && version < 6)) {
esVersion = ESVersion.es5;
break;
}

// skip unknown browsers
const versions = ES_VERSIONS_MAP[browser];
if (!versions) {
continue;
}

if (version < versions[0]) {
esVersion = Math.min(ESVersion.es5, esVersion);
} else if (version < versions[1]) {
esVersion = Math.min(ESVersion.es2015, esVersion);
} else if (version < versions[2]) {
esVersion = Math.min(ESVersion.es2016, esVersion);
} else if (version < versions[3]) {
esVersion = Math.min(ESVersion.es2017, esVersion);
}
}

return esVersion;
}

export { color, deepmerge };

export type Colors = Omit<
Expand Down
56 changes: 0 additions & 56 deletions packages/shared/tests/getBrowserslist.test.ts

This file was deleted.

Loading

0 comments on commit e0a1f8d

Please sign in to comment.