Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define fallback exports for @babel/runtime on old Node #12877

Merged
merged 2 commits into from Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -130,7 +130,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 12, 10, 8, 6]
node-version: [14, 13.1, 12, 10, 8, 6]
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
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