Skip to content

Commit

Permalink
Put back ESM helpers in a folder where we can use .js (babel#12919)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Mar 1, 2021
1 parent a653b9c commit 9844eee
Show file tree
Hide file tree
Showing 38 changed files with 1,355 additions and 1,365 deletions.
22 changes: 8 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@ package-lock.json
/packages/babel-compat-data/build

/packages/babel-runtime/helpers/*.js
/packages/babel-runtime/helpers/*/*.js
/packages/babel-runtime/helpers/*/*.mjs
!/packages/babel-runtime/helpers/toArray/*
!/packages/babel-runtime/helpers/iterableToArray/*
!/packages/babel-runtime/helpers/temporalRef/*
!/packages/babel-runtime/helpers/typeof/*
!/packages/babel-runtime/helpers/toArray.js
!/packages/babel-runtime/helpers/iterableToArray.js
!/packages/babel-runtime/helpers/temporalRef.js
!/packages/babel-runtime/helpers/typeof.js
/packages/babel-runtime/helpers/esm/*.js
!/packages/babel-runtime/helpers/esm/toArray.js
!/packages/babel-runtime/helpers/esm/iterableToArray.js
!/packages/babel-runtime/helpers/esm/temporalRef.js

/packages/babel-runtime-corejs2/helpers/*.js
/packages/babel-runtime-corejs2/helpers/*/*.js
/packages/babel-runtime-corejs2/helpers/*/*.mjs
!/packages/babel-runtime-corejs2/helpers/toArray/*
!/packages/babel-runtime-corejs2/helpers/iterableToArray/*
!/packages/babel-runtime-corejs2/helpers/temporalRef/*
!/packages/babel-runtime-corejs2/helpers/typeof/*
!/packages/babel-runtime-corejs2/helpers/toArray.js
!/packages/babel-runtime-corejs2/helpers/iterableToArray.js
!/packages/babel-runtime-corejs2/helpers/temporalRef.js
!/packages/babel-runtime-corejs2/helpers/typeof.js
/packages/babel-runtime-corejs2/helpers/esm/*.js
!/packages/babel-runtime-corejs2/helpers/esm/toArray.js
!/packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
Expand All @@ -52,8 +48,6 @@ package-lock.json
!/packages/babel-runtime-corejs2/core-js/map.js

/packages/babel-runtime-corejs3/helpers/*.js
/packages/babel-runtime-corejs3/helpers/*/*.js
/packages/babel-runtime-corejs3/helpers/*/*.mjs
/packages/babel-runtime-corejs3/helpers/esm/*.js
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js
Expand Down
31 changes: 5 additions & 26 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ function writeHelperFile(
helperName,
{ esm, corejs }
) {
const filePath = path.join(helperPath, esm ? "_index.mjs" : "index.js");
const fileName = `${helperName}.js`;
const filePath = esm
? path.join("helpers", "esm", fileName)
: path.join("helpers", fileName);
const fullPath = path.join(pkgDirname, filePath);

outputFile(
Expand All @@ -127,12 +130,6 @@ function writeHelperFile(
return `./${filePath}`;
}

function writeHelperLegacyESMFile(pkgDirname, helperName) {
const fullPath = path.join(pkgDirname, "helpers", "esm", `${helperName}.js`);

outputFile(fullPath, `export { default } from "../${helperName}/_index.mjs"`);
}

function writeHelpers(runtimeName, { corejs } = {}) {
const pkgDirname = getRuntimeRoot(runtimeName);
const helperSubExports = {};
Expand Down Expand Up @@ -173,8 +170,6 @@ function writeHelpers(runtimeName, { corejs } = {}) {
];
// For backward compatibility. We can remove this in Babel 8.
helperSubExports[`./${path.join("helpers", "esm", helperName)}`] = esm;

writeHelperLegacyESMFile(pkgDirname, helperName);
}

writeHelperExports(runtimeName, helperSubExports);
Expand Down Expand Up @@ -251,7 +246,6 @@ function buildHelper(
[transformRuntime, { corejs, version: runtimeVersion }],
buildRuntimeRewritePlugin(runtimeName, helperName),
esm ? null : addDefaultCJSExport,
esm ? useRelativeImports : null,
].filter(Boolean),
overrides: [
{
Expand All @@ -272,7 +266,7 @@ function buildRuntimeRewritePlugin(runtimeName, helperName) {
*/
function adjustImportPath(node) {
if (helpers.list.includes(node.value)) {
node.value = `${runtimeName}/helpers/${node.value}`;
node.value = `./${node.value}.js`;
}
}

Expand Down Expand Up @@ -322,18 +316,3 @@ 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"
);
},
},
};
}
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { default } from "../iterableToArray/_index.mjs"
import _Symbol from "@babel/runtime-corejs2/core-js/symbol";
import _isIterable from "@babel/runtime-corejs2/core-js/is-iterable";
import _Array$from from "@babel/runtime-corejs2/core-js/array/from";
export default function _iterableToArray(iter) {
if (typeof _Symbol !== "undefined" && _isIterable(Object(iter))) return _Array$from(iter);
}
6 changes: 5 additions & 1 deletion packages/babel-runtime-corejs2/helpers/esm/temporalRef.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { default } from "../temporalRef/_index.mjs"
import undef from "./temporalUndefined.js";
import err from "./tdz.js";
export default function _temporalRef(val, name) {
return val === undef ? err(name) : val;
}
8 changes: 7 additions & 1 deletion packages/babel-runtime-corejs2/helpers/esm/toArray.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { default } from "../toArray/_index.mjs"
import arrayWithHoles from "./arrayWithHoles.js";
import iterableToArray from "./iterableToArray.js";
import unsupportedIterableToArray from "./unsupportedIterableToArray.js";
import nonIterableRest from "./nonIterableRest.js";
export default function _toArray(arr) {
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var temporalUndefined = require("@babel/runtime/helpers/temporalUndefined");
var temporalUndefined = require("./temporalUndefined.js");

var tdz = require("@babel/runtime/helpers/tdz");
var tdz = require("./tdz.js");

function _temporalRef(val, name) {
return val === temporalUndefined ? tdz(name) : val;
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-runtime-corejs2/helpers/temporalRef/_index.mjs

This file was deleted.

14 changes: 14 additions & 0 deletions packages/babel-runtime-corejs2/helpers/toArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var arrayWithHoles = require("./arrayWithHoles.js");

var iterableToArray = require("./iterableToArray.js");

var unsupportedIterableToArray = require("./unsupportedIterableToArray.js");

var nonIterableRest = require("./nonIterableRest.js");

function _toArray(arr) {
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
}

module.exports = _toArray;
module.exports["default"] = module.exports, module.exports.__esModule = true;
7 changes: 0 additions & 7 deletions packages/babel-runtime-corejs2/helpers/toArray/_index.mjs

This file was deleted.

14 changes: 0 additions & 14 deletions packages/babel-runtime-corejs2/helpers/toArray/index.js

This file was deleted.

17 changes: 0 additions & 17 deletions packages/babel-runtime-corejs2/helpers/typeof/_index.mjs

This file was deleted.

Loading

0 comments on commit 9844eee

Please sign in to comment.