Expose caller.supportsStaticESM as a flag to change preset-env behavior.

This commit is contained in:
Logan Smyth 2018-08-16 16:59:49 -07:00
parent 90bebe7186
commit d60c5e1736
27 changed files with 100 additions and 36 deletions

View File

@ -155,6 +155,10 @@ const filterItems = (
return result;
};
function supportsStaticESM(caller) {
return !!(caller && caller.supportsStaticESM);
}
export default declare((api, opts) => {
api.assertVersion(7);
@ -232,9 +236,15 @@ export default declare((api, opts) => {
const plugins = [];
const pluginUseBuiltIns = useBuiltIns !== false;
// NOTE: not giving spec here yet to avoid compatibility issues when
// transform-modules-commonjs gets its spec mode
if (modules !== false && moduleTransformations[modules]) {
if (
modules !== false &&
moduleTransformations[modules] &&
// TODO: Remove the 'api.caller' check eventually. Just here to prevent
// unnecessary breakage in the short term for users on older betas/RCs.
(modules !== "auto" || !api.caller || !api.caller(supportsStaticESM))
) {
// NOTE: not giving spec here yet to avoid compatibility issues when
// transform-modules-commonjs gets its spec mode
plugins.push([getPlugin(moduleTransformations[modules]), { loose }]);
}

View File

@ -1,4 +1,5 @@
export default {
auto: "transform-modules-commonjs",
amd: "transform-modules-amd",
commonjs: "transform-modules-commonjs",
cjs: "transform-modules-commonjs",

View File

@ -128,13 +128,16 @@ export const validateIgnoreBrowserslistConfig = (
);
export const validateModulesOption = (
modulesOpt: ModuleOption = ModulesOption.commonjs,
modulesOpt: ModuleOption = ModulesOption.auto,
) => {
invariant(
ModulesOption[modulesOpt] ||
ModulesOption[modulesOpt] === ModulesOption.false,
`Invalid Option: The 'modules' option must be either 'false' to indicate no modules, or a
module type which can be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'.`,
`Invalid Option: The 'modules' option must be one of \n` +
` - 'false' to indicate no module processing\n` +
` - a specific module type: 'commonjs', 'amd', 'umd', 'systemjs'` +
` - 'auto' (default) which will automatically select 'false' if the current\n` +
` process is known to support ES module syntax, or "commonjs" otherwise\n`,
);
return modulesOpt;

View File

@ -15,6 +15,7 @@ export const TopLevelOptions = {
export const ModulesOption = {
false: false,
auto: "auto",
amd: "amd",
commonjs: "commonjs",
cjs: "cjs",

View File

@ -5,7 +5,7 @@ Using targets:
"android": "4"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "android":"4" }
@ -147,4 +147,4 @@ Using polyfills with `entry` option:
web.timers { "android":"4" }
web.immediate { "android":"4" }
web.dom.iterable { "android":"4" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -5,7 +5,7 @@ Using targets:
"node": "6"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-function-name { "node":"6" }
@ -21,4 +21,4 @@ Using plugins:
Using polyfills with `entry` option:
[<CWD>/src/in.js] `import '@babel/polyfill'` was not found.
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -7,7 +7,7 @@ Using targets:
"node": "6"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"10" }
@ -163,4 +163,4 @@ Using polyfills with `entry` option:
web.timers { "chrome":"54", "ie":"10", "node":"6" }
web.immediate { "chrome":"54", "ie":"10", "node":"6" }
web.dom.iterable { "chrome":"54", "ie":"10", "node":"6" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -12,7 +12,7 @@ Using targets:
"electron": "0.36"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-function-name { "electron":"0.36" }
@ -123,4 +123,4 @@ Using polyfills with `entry` option:
web.timers { "electron":"0.36" }
web.immediate { "electron":"0.36" }
web.dom.iterable { "electron":"0.36" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -13,7 +13,7 @@ Using targets:
"node": "7.4"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-literals { "firefox":"52" }
@ -26,4 +26,4 @@ Using plugins:
proposal-unicode-property-regex { "firefox":"52", "node":"7.4" }
Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -5,7 +5,7 @@ Using targets:
"chrome": "60"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-dotall-regex { "chrome":"60" }
@ -27,4 +27,4 @@ Using polyfills with `entry` option:
web.timers { "chrome":"60" }
web.immediate { "chrome":"60" }
web.dom.iterable { "chrome":"60" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -3,7 +3,7 @@
Using targets:
{}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals {}
@ -181,4 +181,4 @@ Using polyfills with `entry` option:
web.timers {}
web.immediate {}
web.dom.iterable {}
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -10,7 +10,7 @@ Using targets:
"safari": "7"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"10", "safari":"7" }
@ -167,4 +167,4 @@ Using polyfills with `entry` option:
web.timers { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
web.immediate { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
web.dom.iterable { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -7,7 +7,7 @@ Using targets:
"ie": "11"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"11" }
@ -41,4 +41,4 @@ Using polyfills with `usage` option:
[<CWD>/src/in.js] Based on your code and targets, none were added.
[<CWD>/src/in2.js] Based on your code and targets, none were added.
Successfully compiled 2 files with Babel.
Successfully compiled 2 files with Babel.

View File

@ -5,7 +5,7 @@ Using targets:
"chrome": "55"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-dotall-regex { "chrome":"55" }
@ -16,4 +16,4 @@ Using plugins:
Using polyfills with `usage` option:
[<CWD>/src/in.js] Based on your code and targets, none were added.
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -7,7 +7,7 @@ Using targets:
"ie": "11"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"11" }
@ -47,4 +47,4 @@ Using polyfills with `usage` option:
[<CWD>/src/in2.js] Added following polyfills:
regenerator-runtime { "chrome":"52", "firefox":"50", "ie":"11" }
web.dom.iterable { "chrome":"52", "firefox":"50", "ie":"11" }
Successfully compiled 2 files with Babel.
Successfully compiled 2 files with Babel.

View File

@ -16,7 +16,7 @@ Using targets:
"node": "6.1"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"10" }
@ -193,4 +193,4 @@ Using polyfills with `entry` option:
web.timers { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
web.immediate { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
web.dom.iterable { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -7,7 +7,7 @@ Using targets:
"node": "6.10"
}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals { "ie":"10" }
@ -163,4 +163,4 @@ Using polyfills with `entry` option:
web.timers { "chrome":"54", "ie":"10", "node":"6.10" }
web.immediate { "chrome":"54", "ie":"10", "node":"6.10" }
web.dom.iterable { "chrome":"54", "ie":"10", "node":"6.10" }
Successfully compiled 1 file with Babel.
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,2 @@
import mod from "mod";
console.log(mod);

View File

@ -0,0 +1,9 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": false
},
"presets": [
"env"
]
}

View File

@ -0,0 +1,7 @@
"use strict";
var _mod = _interopRequireDefault(require("mod"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_mod.default);

View File

@ -0,0 +1,2 @@
import mod from "mod";
console.log(mod);

View File

@ -0,0 +1,9 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true
},
"presets": [
"env"
]
}

View File

@ -0,0 +1,2 @@
import mod from "mod";
console.log(mod);

View File

@ -0,0 +1,2 @@
import mod from "mod";
console.log(mod);

View File

@ -0,0 +1,5 @@
{
"presets": [
"env"
]
}

View File

@ -0,0 +1,7 @@
"use strict";
var _mod = _interopRequireDefault(require("mod"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_mod.default);

View File

@ -134,16 +134,20 @@ describe("normalize-options", () => {
});
describe("validateModulesOption", () => {
it("`undefined` option returns commonjs", () => {
expect(validateModulesOption()).toBe("commonjs");
it("`undefined` option returns auto", () => {
expect(validateModulesOption()).toBe("auto");
});
it("`false` option returns commonjs", () => {
it("`false` option returns false", () => {
expect(validateModulesOption(false)).toBe(false);
});
it("auto option is valid", () => {
expect(validateModulesOption("auto")).toBe("auto");
});
it("commonjs option is valid", () => {
expect(validateModulesOption()).toBe("commonjs");
expect(validateModulesOption("commonjs")).toBe("commonjs");
});
it("systemjs option is valid", () => {