Skip to content

Commit e4af537

Browse files
committed
feat: improve getEcmaVersion to recognize more environment flags
Map more of webpack's `output.environment` flags to ECMAScript versions and pick the highest matching version when several flags are set: * ES2015 now also includes `methodShorthand` and `templateLiteral` * ES2017 added — triggered by `asyncFunction` * ES2020 now also includes `dynamicImportInWorker`, `globalThis` and `optionalChaining` Previously the function checked ES2015 before ES2020, so an environment that supported both (e.g. webpack production defaults) was downgraded to ES2015 — which left ES2017–ES2020 minification opportunities on the table. Now the highest version wins. Effect: when webpack signals modern features, swcMinify can now drop the unused `catch (e)` binding (ES2019 optional catch binding), which shows up in the updated snapshots.
1 parent 9df66f3 commit e4af537

3 files changed

Lines changed: 92 additions & 11 deletions

File tree

src/utils.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,42 @@
1717
*/
1818

1919
/**
20+
* Map a webpack `output.environment` configuration to the highest
21+
* ECMAScript version that the target is known to support. Returns `5`
22+
* when no ES2015+ features are flagged.
2023
* @param {NonNullable<NonNullable<import("webpack").Configuration["output"]>["environment"]>} environment environment
21-
* @returns {number} ecma version
24+
* @returns {number} ecma version (5, 2015, 2017 or 2020)
2225
*/
2326
function getEcmaVersion(environment) {
24-
// ES 6th
27+
// ES2020 (11th edition)
28+
if (
29+
environment.bigIntLiteral ||
30+
environment.dynamicImport ||
31+
environment.dynamicImportInWorker ||
32+
environment.globalThis ||
33+
environment.optionalChaining
34+
) {
35+
return 2020;
36+
}
37+
38+
// ES2017 (8th edition)
39+
if (environment.asyncFunction) {
40+
return 2017;
41+
}
42+
43+
// ES2015 (6th edition)
2544
if (
2645
environment.arrowFunction ||
2746
environment.const ||
2847
environment.destructuring ||
2948
environment.forOf ||
30-
environment.module
49+
environment.methodShorthand ||
50+
environment.module ||
51+
environment.templateLiteral
3152
) {
3253
return 2015;
3354
}
3455

35-
// ES 11th
36-
if (environment.bigIntLiteral || environment.dynamicImport) {
37-
return 2020;
38-
}
39-
4056
return 5;
4157
}
4258

0 commit comments

Comments
 (0)