Skip to content

Commit

Permalink
Use relative imports in ESM helpers
Browse files Browse the repository at this point in the history
This makes sure that even if the target bundler doesn't support export
conditions in `package.json`, an ESM helper will only load other ESM
helpers.
  • Loading branch information
nicolo-ribaudo committed Feb 24, 2021
1 parent a2ed7aa commit 8b20b01
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Expand Up @@ -248,6 +248,7 @@ function buildHelper(
[transformRuntime, { corejs, version: runtimeVersion }],
buildRuntimeRewritePlugin(runtimeName, helperName),
esm ? null : addDefaultCJSExport,
esm ? useRelativeImports : null,
].filter(Boolean),
overrides: [
{
Expand Down Expand Up @@ -318,3 +319,18 @@ function addDefaultCJSExport({ template }) {
},
};
}

function useRelativeImports() {
const RE = /^@babel\/runtime(?:-corejs[23])?\/helpers\/(?<name>.+)$/;

return {
visitor: {
ImportDeclaration(path) {
path.node.source.value = path.node.source.value.replace(
RE,
"../$<name>/_index.mjs"
);
},
},
};
}

This file was deleted.

4 changes: 2 additions & 2 deletions packages/babel-runtime-corejs2/helpers/temporalRef/_index.mjs
@@ -1,5 +1,5 @@
import undef from "@babel/runtime-corejs2/helpers/temporalUndefined";
import err from "@babel/runtime-corejs2/helpers/tdz";
import undef from "../temporalUndefined/_index.mjs";
import err from "../tdz/_index.mjs";
export default function _temporalRef(val, name) {
return val === undef ? err(name) : val;
}
8 changes: 4 additions & 4 deletions packages/babel-runtime-corejs2/helpers/toArray/_index.mjs
@@ -1,7 +1,7 @@
import arrayWithHoles from "@babel/runtime-corejs2/helpers/arrayWithHoles";
import iterableToArray from "@babel/runtime-corejs2/helpers/iterableToArray";
import unsupportedIterableToArray from "@babel/runtime-corejs2/helpers/unsupportedIterableToArray";
import nonIterableRest from "@babel/runtime-corejs2/helpers/nonIterableRest";
import arrayWithHoles from "../arrayWithHoles/_index.mjs";
import iterableToArray from "../iterableToArray/_index.mjs";
import unsupportedIterableToArray from "../unsupportedIterableToArray/_index.mjs";
import nonIterableRest from "../nonIterableRest/_index.mjs";
export default function _toArray(arr) {
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
}
4 changes: 2 additions & 2 deletions packages/babel-runtime/helpers/temporalRef/_index.mjs
@@ -1,5 +1,5 @@
import undef from "@babel/runtime/helpers/temporalUndefined";
import err from "@babel/runtime/helpers/tdz";
import undef from "../temporalUndefined/_index.mjs";
import err from "../tdz/_index.mjs";
export default function _temporalRef(val, name) {
return val === undef ? err(name) : val;
}
8 changes: 4 additions & 4 deletions packages/babel-runtime/helpers/toArray/_index.mjs
@@ -1,7 +1,7 @@
import arrayWithHoles from "@babel/runtime/helpers/arrayWithHoles";
import iterableToArray from "@babel/runtime/helpers/iterableToArray";
import unsupportedIterableToArray from "@babel/runtime/helpers/unsupportedIterableToArray";
import nonIterableRest from "@babel/runtime/helpers/nonIterableRest";
import arrayWithHoles from "../arrayWithHoles/_index.mjs";
import iterableToArray from "../iterableToArray/_index.mjs";
import unsupportedIterableToArray from "../unsupportedIterableToArray/_index.mjs";
import nonIterableRest from "../nonIterableRest/_index.mjs";
export default function _toArray(arr) {
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
}

0 comments on commit 8b20b01

Please sign in to comment.