Skip to content

Commit

Permalink
feat: add export-namespace-from to preset-env
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 17, 2020
1 parent c8d602d commit 9609cfe
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/babel-preset-env/package.json
Expand Up @@ -22,6 +22,7 @@
"@babel/plugin-proposal-async-generator-functions": "^7.10.4",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-dynamic-import": "^7.10.4",
"@babel/plugin-proposal-export-namespace-from": "^7.10.4",
"@babel/plugin-proposal-json-strings": "^7.10.4",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-proposal-numeric-separator": "^7.10.4",
Expand All @@ -33,6 +34,7 @@
"@babel/plugin-syntax-async-generators": "^7.8.0",
"@babel/plugin-syntax-class-properties": "^7.10.4",
"@babel/plugin-syntax-dynamic-import": "^7.8.0",
"@babel/plugin-syntax-export-namespace-from": "^7.8.3",
"@babel/plugin-syntax-json-strings": "^7.8.0",
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.0",
"@babel/plugin-syntax-numeric-separator": "^7.10.4",
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-preset-env/src/available-plugins.js
Expand Up @@ -4,6 +4,7 @@
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator";
Expand All @@ -14,6 +15,7 @@ import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";
import proposalAsyncGeneratorFunctions from "@babel/plugin-proposal-async-generator-functions";
import proposalClassProperties from "@babel/plugin-proposal-class-properties";
import proposalDynamicImport from "@babel/plugin-proposal-dynamic-import";
import proposalExportNamespaceFrom from "@babel/plugin-proposal-export-namespace-from";
import proposalJsonStrings from "@babel/plugin-proposal-json-strings";
import proposalNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator";
import proposalNumericSeparator from "@babel/plugin-proposal-numeric-separator";
Expand Down Expand Up @@ -72,6 +74,7 @@ export default {
"proposal-async-generator-functions": proposalAsyncGeneratorFunctions,
"proposal-class-properties": proposalClassProperties,
"proposal-dynamic-import": proposalDynamicImport,
"proposal-export-namespace-from": proposalExportNamespaceFrom,
"proposal-json-strings": proposalJsonStrings,
"proposal-nullish-coalescing-operator": proposalNullishCoalescingOperator,
"proposal-numeric-separator": proposalNumericSeparator,
Expand All @@ -83,6 +86,7 @@ export default {
"syntax-async-generators": syntaxAsyncGenerators,
"syntax-class-properties": syntaxClassProperties,
"syntax-dynamic-import": syntaxDynamicImport,
"syntax-export-namespace-from": syntaxExportNamespaceFrom,
"syntax-json-strings": syntaxJsonStrings,
"syntax-nullish-coalescing-operator": syntaxNullishCoalescingOperator,
"syntax-numeric-separator": syntaxNumericSeparator,
Expand Down
28 changes: 25 additions & 3 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -99,14 +99,16 @@ export const getModulesPluginNames = ({
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
shouldParseTopLevelAwait,
}: {
}: {|
modules: ModuleOption,
transformations: ModuleTransformationsType,
shouldTransformESM: boolean,
shouldTransformDynamicImport: boolean,
shouldTransformExportNamespaceFrom: boolean,
shouldParseTopLevelAwait: boolean,
}) => {
|}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
Expand All @@ -132,6 +134,12 @@ export const getModulesPluginNames = ({
modulesPluginNames.push("syntax-dynamic-import");
}

if (shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("proposal-export-namespace-from");
} else {
modulesPluginNames.push("syntax-export-namespace-from");
}

if (shouldParseTopLevelAwait) {
modulesPluginNames.push("syntax-top-level-await");
}
Expand Down Expand Up @@ -206,6 +214,10 @@ function supportsDynamicImport(caller) {
return !!caller?.supportsDynamicImport;
}

function supportsExportNamespaceFrom(caller) {
return !!caller?.supportsExportNamespaceFrom;
}

function supportsTopLevelAwait(caller) {
return !!caller?.supportsTopLevelAwait;
}
Expand Down Expand Up @@ -265,6 +277,7 @@ export default declare((api, opts) => {

const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;

const compatData = getPluginList(shippedProposals, bugfixes);
const modulesPluginNames = getModulesPluginNames({
modules,
transformations: moduleTransformations,
Expand All @@ -273,11 +286,20 @@ export default declare((api, opts) => {
shouldTransformESM: modules !== "auto" || !api.caller?.(supportsStaticESM),
shouldTransformDynamicImport:
modules !== "auto" || !api.caller?.(supportsDynamicImport),
shouldTransformExportNamespaceFrom: !(
(modules === "auto" && api.caller?.(supportsExportNamespaceFrom)) ||
(modules === false &&
!isRequired("proposal-export-namespace-from", transformTargets, {
compatData,
includes: include.plugins,
excludes: exclude.plugins,
}))
),
shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait),
});

const pluginNames = filterItems(
getPluginList(shippedProposals, bugfixes),
compatData,
include.plugins,
exclude.plugins,
transformTargets,
Expand Down
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";
@@ -0,0 +1,10 @@
{
"validateLogs": true,
"caller": {
"name": "test-fixture",
"supportsStaticESM": false,
"supportsDynamicImport": false,
"supportsExportNamespaceFrom": false
},
"presets": ["env"]
}
@@ -0,0 +1,12 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.foo = void 0;

var _foo = _interopRequireDefault(require("./foo.mjs"));

exports.foo = _foo;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";
@@ -0,0 +1,9 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
"supportsDynamicImport": true,
"supportsExportNamespaceFrom": false
},
"presets": ["env"]
}
@@ -0,0 +1,2 @@
import * as _foo from "./foo.mjs";
export { _foo as foo };
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";
@@ -0,0 +1,7 @@
{
"caller": {
"name": "test-fixture",
"supportsExportNamespaceFrom": true
},
"presets": [["env", { "modules": false, "targets": "chrome 71" }]]
}
@@ -0,0 +1,2 @@
import * as _foo from "./foo.mjs";
export { _foo as foo };
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";
@@ -0,0 +1,7 @@
{
"caller": {
"name": "test-fixture",
"supportsExportNamespaceFrom": false
},
"presets": [["env", { "modules": false, "targets": "chrome 71" }]]
}
@@ -0,0 +1,2 @@
import * as _foo from "./foo.mjs";
export { _foo as foo };
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";
@@ -0,0 +1,7 @@
{
"caller": {
"name": "test-fixture",
"supportsExportNamespaceFrom": false
},
"presets": [["env", { "modules": false, "targets": "chrome 72" }]]
}
@@ -0,0 +1 @@
export * as foo from "./foo.mjs";

0 comments on commit 9609cfe

Please sign in to comment.