Skip to content

Commit e57d081

Browse files
authored
feat: add import-assertions to shippedProposals (#14556)
* feat: add import-assertions to shippedProposals * fix: import assertions are independent of targets
1 parent 1daded5 commit e57d081

File tree

63 files changed

+94
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+94
-4
lines changed

packages/babel-preset-env/data/shipped-proposals.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
/* eslint sort-keys: "error" */
2-
// These mappings represent the syntax proposals that have been
2+
// These mappings represent the transform plugins that have been
33
// shipped by browsers, and are enabled by the `shippedProposals` option.
44

55
const proposalPlugins = new Set();
66

7+
// proposal syntax plugins enabled by the `shippedProposals` option.
8+
// Unlike proposalPlugins above, they are independent of compiler targets.
9+
const proposalSyntaxPlugins = [
10+
"syntax-import-assertions"
11+
]
12+
713
// use intermediary object to enforce alphabetical key order
814
const pluginSyntaxObject = {
915
"proposal-async-generator-functions": "syntax-async-generators",
@@ -27,4 +33,4 @@ const pluginSyntaxEntries = Object.keys(pluginSyntaxObject).map(function (key) {
2733

2834
const pluginSyntaxMap = new Map(pluginSyntaxEntries);
2935

30-
module.exports = { pluginSyntaxMap, proposalPlugins };
36+
module.exports = { pluginSyntaxMap, proposalPlugins, proposalSyntaxPlugins };

packages/babel-preset-env/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@babel/plugin-syntax-class-static-block": "^7.14.5",
4343
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
4444
"@babel/plugin-syntax-export-namespace-from": "^7.8.3",
45+
"@babel/plugin-syntax-import-assertions": "workspace:^",
4546
"@babel/plugin-syntax-json-strings": "^7.8.3",
4647
"@babel/plugin-syntax-logical-assignment-operators": "^7.10.4",
4748
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3",

packages/babel-preset-env/src/available-plugins.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
55
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";
66
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
77
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
8+
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
89
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
910
import syntaxLogicalAssignmentOperators from "@babel/plugin-syntax-logical-assignment-operators";
1011
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
@@ -104,6 +105,7 @@ export default {
104105
"syntax-class-static-block": () => syntaxClassStaticBlock,
105106
"syntax-dynamic-import": () => syntaxDynamicImport,
106107
"syntax-export-namespace-from": () => syntaxExportNamespaceFrom,
108+
"syntax-import-assertions": () => syntaxImportAssertions,
107109
"syntax-json-strings": () => syntaxJsonStrings,
108110
"syntax-logical-assignment-operators": () => syntaxLogicalAssignmentOperators,
109111
"syntax-nullish-coalescing-operator": () => syntaxNullishCoalescingOperator,

packages/babel-preset-env/src/filter-items.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { minVersions } from "./available-plugins";
44
// $FlowIgnore
55
const has = Function.call.bind(Object.hasOwnProperty);
66

7+
export function addProposalSyntaxPlugins(
8+
items: Set<string>,
9+
proposalSyntaxPlugins: string[],
10+
) {
11+
proposalSyntaxPlugins.forEach(plugin => {
12+
items.add(plugin);
13+
});
14+
}
715
export function removeUnnecessaryItems(
816
items: Set<string>,
917
overlapping: { [name: string]: string[] },

packages/babel-preset-env/src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import { lt } from "semver";
22
import type { SemVer } from "semver";
33
import { logPlugin } from "./debug";
44
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
5-
import { removeUnnecessaryItems, removeUnsupportedItems } from "./filter-items";
5+
import {
6+
addProposalSyntaxPlugins,
7+
removeUnnecessaryItems,
8+
removeUnsupportedItems,
9+
} from "./filter-items";
610
import moduleTransformations from "./module-transformations";
711
import normalizeOptions from "./normalize-options";
8-
import { proposalPlugins, pluginSyntaxMap } from "../data/shipped-proposals";
12+
import {
13+
pluginSyntaxMap,
14+
proposalPlugins,
15+
proposalSyntaxPlugins,
16+
} from "../data/shipped-proposals";
917
import {
1018
plugins as pluginsList,
1119
pluginsBugfixes as pluginsBugfixesList,
@@ -375,6 +383,9 @@ option \`forceAllTransforms: true\` instead.
375383
);
376384
removeUnnecessaryItems(pluginNames, overlappingPlugins);
377385
removeUnsupportedItems(pluginNames, api.version);
386+
if (shippedProposals) {
387+
addProposalSyntaxPlugins(pluginNames, proposalSyntaxPlugins);
388+
}
378389

379390
const polyfillPlugins = getPolyfillPlugins({
380391
useBuiltIns,

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs2-shippedProposals-chrome-71/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Using plugins:
2323
proposal-export-namespace-from { chrome < 72 }
2424
transform-modules-commonjs
2525
proposal-dynamic-import
26+
syntax-import-assertions
2627
corejs2: `DEBUG` option
2728

2829
Using targets: {

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs2-shippedProposals/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Using plugins:
5252
proposal-export-namespace-from { ie }
5353
transform-modules-commonjs
5454
proposal-dynamic-import
55+
syntax-import-assertions
5556
corejs2: `DEBUG` option
5657

5758
Using targets: {

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs3-all-chrome-71/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Using plugins:
2323
proposal-export-namespace-from { chrome < 72 }
2424
transform-modules-commonjs
2525
proposal-dynamic-import
26+
syntax-import-assertions
2627
corejs3: `DEBUG` option
2728

2829
Using targets: {

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs3-all/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Using plugins:
5252
proposal-export-namespace-from { ie }
5353
transform-modules-commonjs
5454
proposal-dynamic-import
55+
syntax-import-assertions
5556
corejs3: `DEBUG` option
5657

5758
Using targets: {

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs3-babel-polyfill/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Using plugins:
5252
proposal-export-namespace-from { ie }
5353
transform-modules-commonjs
5454
proposal-dynamic-import
55+
syntax-import-assertions
5556
corejs3: `DEBUG` option
5657

5758
Using targets: {

0 commit comments

Comments
 (0)