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;
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
if (modules !== false && moduleTransformations[modules]) {
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" }

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" }

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" }

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" }

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" }

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" }

View File

@ -3,7 +3,7 @@
Using targets:
{}
Using modules transform: commonjs
Using modules transform: auto
Using plugins:
transform-template-literals {}

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" }

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" }

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" }

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" }

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" }

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" }

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", () => {