diff --git a/packages/babel-plugin-transform-runtime/src/index.js b/packages/babel-plugin-transform-runtime/src/index.js index f53fd16a0b..788432dbad 100644 --- a/packages/babel-plugin-transform-runtime/src/index.js +++ b/packages/babel-plugin-transform-runtime/src/index.js @@ -25,6 +25,10 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) { } } +function supportsStaticESM(caller) { + return !!(caller && caller.supportsStaticESM); +} + export default declare((api, options, dirname) => { api.assertVersion(7); @@ -45,9 +49,9 @@ export default declare((api, options, dirname) => { if (typeof useRuntimeHelpers !== "boolean") { throw new Error("The 'helpers' option must be undefined, or a boolean."); } - if (typeof useESModules !== "boolean") { + if (typeof useESModules !== "boolean" && useESModules !== "auto") { throw new Error( - "The 'useESModules' option must be undefined, or a boolean.", + "The 'useESModules' option must be undefined, or a boolean, or 'auto'.", ); } if ( @@ -107,6 +111,9 @@ export default declare((api, options, dirname) => { ); } + const esModules = + useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules; + const injectCoreJS2 = `${corejsVersion}` === "2"; const moduleName = injectCoreJS2 ? "@babel/runtime-corejs2" @@ -145,7 +152,7 @@ export default declare((api, options, dirname) => { isInteropHelper && !isModule(file.path) ? 4 : undefined; const helpersDir = - useESModules && file.path.node.sourceType === "module" + esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers"; diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/input.js new file mode 100644 index 0000000000..dcde0c7bf9 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/input.js @@ -0,0 +1 @@ +class Foo extends Bar {} diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/options.json new file mode 100644 index 0000000000..68379236ea --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/options.json @@ -0,0 +1,7 @@ +{ + "caller": { + "name": "babel-test", + "supportsStaticESM": true + }, + "plugins": [["transform-runtime", { "useESModules": "auto" }], "transform-classes"] +} diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/output.js new file mode 100644 index 0000000000..39882dcd46 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs-auto/output.js @@ -0,0 +1,23 @@ +var _classCallCheck = require("@babel/runtime/helpers/classCallCheck"); + +var _possibleConstructorReturn = require("@babel/runtime/helpers/possibleConstructorReturn"); + +var _getPrototypeOf = require("@babel/runtime/helpers/getPrototypeOf"); + +var _inherits = require("@babel/runtime/helpers/inherits"); + +let Foo = +/*#__PURE__*/ +function (_Bar) { + "use strict"; + + _inherits(Foo, _Bar); + + function Foo() { + _classCallCheck(this, Foo); + + return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments)); + } + + return Foo; +}(Bar); diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json index 92d77833b5..5f363a2640 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json @@ -1,3 +1,7 @@ { + "caller": { + "name": "babel-test", + "supportsStaticESM": true + }, "plugins": [["transform-runtime", { "useESModules": true }], "transform-classes"] } diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/input.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/input.mjs new file mode 100644 index 0000000000..dcde0c7bf9 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/input.mjs @@ -0,0 +1 @@ +class Foo extends Bar {} diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/options.json new file mode 100644 index 0000000000..68379236ea --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/options.json @@ -0,0 +1,7 @@ +{ + "caller": { + "name": "babel-test", + "supportsStaticESM": true + }, + "plugins": [["transform-runtime", { "useESModules": "auto" }], "transform-classes"] +} diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/output.mjs new file mode 100644 index 0000000000..0247fa0b55 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs-auto/output.mjs @@ -0,0 +1,18 @@ +import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; +import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; +import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; +import _inherits from "@babel/runtime/helpers/esm/inherits"; + +let Foo = +/*#__PURE__*/ +function (_Bar) { + _inherits(Foo, _Bar); + + function Foo() { + _classCallCheck(this, Foo); + + return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments)); + } + + return Foo; +}(Bar); diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/options.json index 92d77833b5..5f363a2640 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/options.json +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/options.json @@ -1,3 +1,7 @@ { + "caller": { + "name": "babel-test", + "supportsStaticESM": true + }, "plugins": [["transform-runtime", { "useESModules": true }], "transform-classes"] }