diff --git a/packages/babel-plugin-transform-runtime/src/index.js b/packages/babel-plugin-transform-runtime/src/index.js index 757ec333a461..f53fd16a0be7 100644 --- a/packages/babel-plugin-transform-runtime/src/index.js +++ b/packages/babel-plugin-transform-runtime/src/index.js @@ -107,7 +107,6 @@ export default declare((api, options, dirname) => { ); } - const helpersDir = useESModules ? "helpers/esm" : "helpers"; const injectCoreJS2 = `${corejsVersion}` === "2"; const moduleName = injectCoreJS2 ? "@babel/runtime-corejs2" @@ -145,6 +144,11 @@ export default declare((api, options, dirname) => { const blockHoist = isInteropHelper && !isModule(file.path) ? 4 : undefined; + const helpersDir = + useESModules && file.path.node.sourceType === "module" + ? "helpers/esm" + : "helpers"; + return this.addDefaultImport( `${modulePath}/${helpersDir}/${name}`, name, diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/input.mjs similarity index 100% rename from packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/input.js rename to packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/input.mjs diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.js deleted file mode 100644 index fd691a2742a1..000000000000 --- a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.js +++ /dev/null @@ -1,23 +0,0 @@ -var _classCallCheck = require("@babel/runtime-corejs2/helpers/esm/classCallCheck"); - -var _possibleConstructorReturn = require("@babel/runtime-corejs2/helpers/esm/possibleConstructorReturn"); - -var _getPrototypeOf = require("@babel/runtime-corejs2/helpers/esm/getPrototypeOf"); - -var _inherits = require("@babel/runtime-corejs2/helpers/esm/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/corejs-useES6Modules/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.mjs new file mode 100644 index 000000000000..828c9aa787ef --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs-useES6Modules/output.mjs @@ -0,0 +1,18 @@ +import _classCallCheck from "@babel/runtime-corejs2/helpers/esm/classCallCheck"; +import _possibleConstructorReturn from "@babel/runtime-corejs2/helpers/esm/possibleConstructorReturn"; +import _getPrototypeOf from "@babel/runtime-corejs2/helpers/esm/getPrototypeOf"; +import _inherits from "@babel/runtime-corejs2/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/corejs/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/input.mjs similarity index 100% rename from packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/input.js rename to packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/input.mjs diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.js deleted file mode 100644 index 91eb87aa1694..000000000000 --- a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.js +++ /dev/null @@ -1,23 +0,0 @@ -var _classCallCheck = require("@babel/runtime-corejs2/helpers/classCallCheck"); - -var _possibleConstructorReturn = require("@babel/runtime-corejs2/helpers/possibleConstructorReturn"); - -var _getPrototypeOf = require("@babel/runtime-corejs2/helpers/getPrototypeOf"); - -var _inherits = require("@babel/runtime-corejs2/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/corejs/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.mjs new file mode 100644 index 000000000000..44f4d72a0409 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/corejs/output.mjs @@ -0,0 +1,18 @@ +import _classCallCheck from "@babel/runtime-corejs2/helpers/classCallCheck"; +import _possibleConstructorReturn from "@babel/runtime-corejs2/helpers/possibleConstructorReturn"; +import _getPrototypeOf from "@babel/runtime-corejs2/helpers/getPrototypeOf"; +import _inherits from "@babel/runtime-corejs2/helpers/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/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/input.js similarity index 100% rename from packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/input.js rename to packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/input.js diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json similarity index 100% rename from packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/options.json rename to packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/options.json diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/output.js similarity index 56% rename from packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/output.js rename to packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/output.js index d2863837c42b..39882dcd466a 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-cjs/output.js @@ -1,10 +1,10 @@ -var _classCallCheck = require("@babel/runtime/helpers/esm/classCallCheck"); +var _classCallCheck = require("@babel/runtime/helpers/classCallCheck"); -var _possibleConstructorReturn = require("@babel/runtime/helpers/esm/possibleConstructorReturn"); +var _possibleConstructorReturn = require("@babel/runtime/helpers/possibleConstructorReturn"); -var _getPrototypeOf = require("@babel/runtime/helpers/esm/getPrototypeOf"); +var _getPrototypeOf = require("@babel/runtime/helpers/getPrototypeOf"); -var _inherits = require("@babel/runtime/helpers/esm/inherits"); +var _inherits = require("@babel/runtime/helpers/inherits"); let Foo = /*#__PURE__*/ diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/input.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/input.mjs new file mode 100644 index 000000000000..dcde0c7bf9a2 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/input.mjs @@ -0,0 +1 @@ +class Foo extends 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 new file mode 100644 index 000000000000..92d77833b5a7 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["transform-runtime", { "useESModules": true }], "transform-classes"] +} diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/output.mjs new file mode 100644 index 000000000000..0247fa0b5556 --- /dev/null +++ b/packages/babel-plugin-transform-runtime/test/fixtures/use-options/useESModules-mjs/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);