Expose caller.supportsStaticESM as a flag to change preset-env behavior.
This commit is contained in:
parent
90bebe7186
commit
d60c5e1736
@ -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 }]);
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
export default {
|
||||
auto: "transform-modules-commonjs",
|
||||
amd: "transform-modules-amd",
|
||||
commonjs: "transform-modules-commonjs",
|
||||
cjs: "transform-modules-commonjs",
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -15,6 +15,7 @@ export const TopLevelOptions = {
|
||||
|
||||
export const ModulesOption = {
|
||||
false: false,
|
||||
auto: "auto",
|
||||
amd: "amd",
|
||||
commonjs: "commonjs",
|
||||
cjs: "cjs",
|
||||
|
||||
@ -5,7 +5,7 @@ Using targets:
|
||||
"android": "4"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "android":"4" }
|
||||
|
||||
@ -5,7 +5,7 @@ Using targets:
|
||||
"node": "6"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-function-name { "node":"6" }
|
||||
|
||||
@ -7,7 +7,7 @@ Using targets:
|
||||
"node": "6"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "ie":"10" }
|
||||
|
||||
@ -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" }
|
||||
|
||||
@ -13,7 +13,7 @@ Using targets:
|
||||
"node": "7.4"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-literals { "firefox":"52" }
|
||||
|
||||
@ -5,7 +5,7 @@ Using targets:
|
||||
"chrome": "60"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-dotall-regex { "chrome":"60" }
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Using targets:
|
||||
{}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals {}
|
||||
|
||||
@ -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" }
|
||||
|
||||
@ -7,7 +7,7 @@ Using targets:
|
||||
"ie": "11"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "ie":"11" }
|
||||
|
||||
@ -5,7 +5,7 @@ Using targets:
|
||||
"chrome": "55"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-dotall-regex { "chrome":"55" }
|
||||
|
||||
@ -7,7 +7,7 @@ Using targets:
|
||||
"ie": "11"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "ie":"11" }
|
||||
|
||||
@ -16,7 +16,7 @@ Using targets:
|
||||
"node": "6.1"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "ie":"10" }
|
||||
|
||||
@ -7,7 +7,7 @@ Using targets:
|
||||
"node": "6.10"
|
||||
}
|
||||
|
||||
Using modules transform: commonjs
|
||||
Using modules transform: auto
|
||||
|
||||
Using plugins:
|
||||
transform-template-literals { "ie":"10" }
|
||||
|
||||
2
packages/babel-preset-env/test/fixtures/modules/auto-cjs/input.mjs
vendored
Normal file
2
packages/babel-preset-env/test/fixtures/modules/auto-cjs/input.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import mod from "mod";
|
||||
console.log(mod);
|
||||
9
packages/babel-preset-env/test/fixtures/modules/auto-cjs/options.json
vendored
Normal file
9
packages/babel-preset-env/test/fixtures/modules/auto-cjs/options.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"caller": {
|
||||
"name": "test-fixture",
|
||||
"supportsStaticESM": false
|
||||
},
|
||||
"presets": [
|
||||
"env"
|
||||
]
|
||||
}
|
||||
7
packages/babel-preset-env/test/fixtures/modules/auto-cjs/output.js
vendored
Normal file
7
packages/babel-preset-env/test/fixtures/modules/auto-cjs/output.js
vendored
Normal 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);
|
||||
2
packages/babel-preset-env/test/fixtures/modules/auto-esm/input.mjs
vendored
Normal file
2
packages/babel-preset-env/test/fixtures/modules/auto-esm/input.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import mod from "mod";
|
||||
console.log(mod);
|
||||
9
packages/babel-preset-env/test/fixtures/modules/auto-esm/options.json
vendored
Normal file
9
packages/babel-preset-env/test/fixtures/modules/auto-esm/options.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"caller": {
|
||||
"name": "test-fixture",
|
||||
"supportsStaticESM": true
|
||||
},
|
||||
"presets": [
|
||||
"env"
|
||||
]
|
||||
}
|
||||
2
packages/babel-preset-env/test/fixtures/modules/auto-esm/output.mjs
vendored
Normal file
2
packages/babel-preset-env/test/fixtures/modules/auto-esm/output.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import mod from "mod";
|
||||
console.log(mod);
|
||||
2
packages/babel-preset-env/test/fixtures/modules/auto-unknown/input.mjs
vendored
Normal file
2
packages/babel-preset-env/test/fixtures/modules/auto-unknown/input.mjs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import mod from "mod";
|
||||
console.log(mod);
|
||||
5
packages/babel-preset-env/test/fixtures/modules/auto-unknown/options.json
vendored
Normal file
5
packages/babel-preset-env/test/fixtures/modules/auto-unknown/options.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"presets": [
|
||||
"env"
|
||||
]
|
||||
}
|
||||
7
packages/babel-preset-env/test/fixtures/modules/auto-unknown/output.js
vendored
Normal file
7
packages/babel-preset-env/test/fixtures/modules/auto-unknown/output.js
vendored
Normal 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);
|
||||
@ -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", () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user