Skip to content

Commit

Permalink
Define fallback exports for @babel/runtime on old Node (#12877)
Browse files Browse the repository at this point in the history
* Define fallback `exports` for `@babel/runtime` on old Node

* Update .github/workflows/ci.yml
  • Loading branch information
nicolo-ribaudo committed Feb 23, 2021
1 parent 4acb734 commit ac7ac54
Show file tree
Hide file tree
Showing 4 changed files with 1,975 additions and 1,234 deletions.
50 changes: 31 additions & 19 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Expand Up @@ -138,25 +138,37 @@ function writeHelpers(runtimeName, { corejs } = {}) {
const helperSubExports = {};
for (const helperName of helpers.list) {
const helperPath = path.join("helpers", helperName);
helperSubExports[`./${helperPath}`] = {
get node() {
return this.require;
},
require: writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm: false, corejs }
),
default: writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm: true, corejs }
),
};
const cjs = writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm: false, corejs }
);
const esm = writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm: true, corejs }
);

// Node.js versions >=13.0.0, <13.7.0 support the `exports` field but
// not conditional exports (`require`/`node`/`default`)
// We can specify exports with an array of fallbacks:
// - Node.js >=13.7.0 and bundlers will succesfully load the first
// array entry:
// * Node.js will always load the CJS file
// * Bundlers when using require() will load the CJS file
// * Everything else will load the ESM file
// - Node.js <13.7.0 will fail resolving the first array entry, and will
// fallback to the second entry (the CJS file)
// In Babel 8 we can simplify this.
helperSubExports[`./${helperPath}`] = [
{ node: cjs, require: cjs, default: esm },
cjs,
];

writeHelperLegacyESMFile(pkgDirname, helperName);
}

Expand Down

0 comments on commit ac7ac54

Please sign in to comment.