Improve @babel/runtime esm stability (#12883)
This commit is contained in:
parent
c9da9650eb
commit
6a471decc3
@ -26,3 +26,6 @@ packages/babel-parser/test/expressions
|
|||||||
eslint/*/lib
|
eslint/*/lib
|
||||||
eslint/*/node_modules
|
eslint/*/node_modules
|
||||||
eslint/*/test/fixtures
|
eslint/*/test/fixtures
|
||||||
|
|
||||||
|
test/runtime-integration/*/output.js
|
||||||
|
test/runtime-integration/*/output-absolute.js
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
<PROJECT_ROOT>/codemods/.*/lib
|
<PROJECT_ROOT>/codemods/.*/lib
|
||||||
<PROJECT_ROOT>/codemods/.*/test
|
<PROJECT_ROOT>/codemods/.*/test
|
||||||
<PROJECT_ROOT>/node_modules/module-deps/
|
<PROJECT_ROOT>/node_modules/module-deps/
|
||||||
|
<PROJECT_ROOT>/node_modules/webpack-cli/
|
||||||
|
<PROJECT_ROOT>/test/runtime-integration/
|
||||||
|
|
||||||
[include]
|
[include]
|
||||||
packages/*/src
|
packages/*/src
|
||||||
|
|||||||
81
.github/workflows/ci.yml
vendored
81
.github/workflows/ci.yml
vendored
@ -269,3 +269,84 @@ jobs:
|
|||||||
run: make test-flow
|
run: make test-flow
|
||||||
- name: Run TypeScript Tests
|
- name: Run TypeScript Tests
|
||||||
run: make test-typescript
|
run: make test-typescript
|
||||||
|
|
||||||
|
runtime-interop:
|
||||||
|
name: Test @babel/runtime integrations
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Use Node.js latest
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: "*"
|
||||||
|
- name: Get yarn cache directory path
|
||||||
|
id: yarn-cache-dir-path
|
||||||
|
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
|
||||||
|
- uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
|
||||||
|
key: yarn-${{ hashFiles('yarn.lock') }}
|
||||||
|
- name: Install
|
||||||
|
run: yarn install
|
||||||
|
- uses: actions/download-artifact@v2
|
||||||
|
with:
|
||||||
|
name: babel-artifact
|
||||||
|
- name: Generate runtime helpers
|
||||||
|
run: |
|
||||||
|
make build-plugin-transform-runtime-dist
|
||||||
|
- name: Generate absoluteRuntime tests
|
||||||
|
run: yarn test:runtime:generate-absolute-runtime
|
||||||
|
- name: Test bundlers
|
||||||
|
run: yarn test:runtime:bundlers
|
||||||
|
- name: Test Node.js
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 10
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: 10
|
||||||
|
- name: Test Node.js 10
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 12.0
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: "12.0" # quoted, otherwise it's just 13
|
||||||
|
- name: Test Node.js 12.0
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 12.17
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: 12.17
|
||||||
|
- name: Test Node.js 12.17
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 13.0
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: "13.0" # quoted, otherwise it's just 13
|
||||||
|
- name: Test Node.js 13.0
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
# - name: Use Node.js 13.2
|
||||||
|
# uses: actions/setup-node@v2-beta
|
||||||
|
# with:
|
||||||
|
# node-version: 13.2
|
||||||
|
# - name: Test Node.js 13.2
|
||||||
|
# run: yarn test:runtime:node
|
||||||
|
# - name: Use Node.js 13.6
|
||||||
|
# uses: actions/setup-node@v2-beta
|
||||||
|
# with:
|
||||||
|
# node-version: 13.6
|
||||||
|
# - name: Test Node.js 13.6
|
||||||
|
# run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 13.7
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: 13.7
|
||||||
|
- name: Test Node.js 13.7
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
- name: Use Node.js 14.2
|
||||||
|
uses: actions/setup-node@v2-beta
|
||||||
|
with:
|
||||||
|
node-version: 14.2
|
||||||
|
- name: Test Node.js 14.2
|
||||||
|
run: yarn test:runtime:node
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@ -79,3 +79,6 @@ packages/babel-standalone/babel.min.js
|
|||||||
|
|
||||||
tsconfig.json
|
tsconfig.json
|
||||||
tsconfig.tsbuildinfo
|
tsconfig.tsbuildinfo
|
||||||
|
|
||||||
|
/test/runtime-integration/*/output.js
|
||||||
|
/test/runtime-integration/*/absolute-output.js
|
||||||
|
|||||||
@ -12,7 +12,10 @@
|
|||||||
"lint": "make lint",
|
"lint": "make lint",
|
||||||
"test": "make test",
|
"test": "make test",
|
||||||
"version": "yarn --immutable-cache && git add yarn.lock",
|
"version": "yarn --immutable-cache && git add yarn.lock",
|
||||||
"test:esm": "node test/esm/index.js"
|
"test:esm": "node test/esm/index.js",
|
||||||
|
"test:runtime:generate-absolute-runtime": "node test/runtime-integration/generate-absolute-runtime.cjs",
|
||||||
|
"test:runtime:bundlers": "node test/runtime-integration/bundlers.cjs",
|
||||||
|
"test:runtime:node": "node test/runtime-integration/node.cjs"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/cli": "^7.12.0",
|
"@babel/cli": "^7.12.0",
|
||||||
@ -73,7 +76,8 @@
|
|||||||
"codemods/*",
|
"codemods/*",
|
||||||
"eslint/*",
|
"eslint/*",
|
||||||
"packages/*",
|
"packages/*",
|
||||||
"test/esm"
|
"test/esm",
|
||||||
|
"test/runtime-integration/*"
|
||||||
],
|
],
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"browserslist": "npm:4.14.5",
|
"browserslist": "npm:4.14.5",
|
||||||
|
|||||||
@ -248,6 +248,7 @@ function buildHelper(
|
|||||||
[transformRuntime, { corejs, version: runtimeVersion }],
|
[transformRuntime, { corejs, version: runtimeVersion }],
|
||||||
buildRuntimeRewritePlugin(runtimeName, helperName),
|
buildRuntimeRewritePlugin(runtimeName, helperName),
|
||||||
esm ? null : addDefaultCJSExport,
|
esm ? null : addDefaultCJSExport,
|
||||||
|
esm ? useRelativeImports : null,
|
||||||
].filter(Boolean),
|
].filter(Boolean),
|
||||||
overrides: [
|
overrides: [
|
||||||
{
|
{
|
||||||
@ -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"
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@ -88,13 +88,6 @@ export default declare((api, options, dirname) => {
|
|||||||
// TODO(Babel 8): Remove this check, it's always true
|
// TODO(Babel 8): Remove this check, it's always true
|
||||||
const DUAL_MODE_RUNTIME = "7.13.0";
|
const DUAL_MODE_RUNTIME = "7.13.0";
|
||||||
const supportsCJSDefault = hasMinVersion(DUAL_MODE_RUNTIME, runtimeVersion);
|
const supportsCJSDefault = hasMinVersion(DUAL_MODE_RUNTIME, runtimeVersion);
|
||||||
if (supportsCJSDefault && useESModules && !absoluteRuntime) {
|
|
||||||
console.warn(
|
|
||||||
`[@babel/plugin-transform-runtime] The 'useESModules' option is not necessary when using` +
|
|
||||||
` a @babel/runtime version >= ${DUAL_MODE_RUNTIME} and not using the 'absoluteRuntime'` +
|
|
||||||
` option, because it automatically detects the necessary module format.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function has(obj, key) {
|
function has(obj, key) {
|
||||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
[@babel/plugin-transform-runtime] The 'useESModules' option is not necessary when using a @babel/runtime version >= 7.13.0 and not using the 'absoluteRuntime' option, because it automatically detects the necessary module format.
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import undef from "@babel/runtime-corejs2/helpers/temporalUndefined";
|
import undef from "../temporalUndefined/_index.mjs";
|
||||||
import err from "@babel/runtime-corejs2/helpers/tdz";
|
import err from "../tdz/_index.mjs";
|
||||||
export default function _temporalRef(val, name) {
|
export default function _temporalRef(val, name) {
|
||||||
return val === undef ? err(name) : val;
|
return val === undef ? err(name) : val;
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import arrayWithHoles from "@babel/runtime-corejs2/helpers/arrayWithHoles";
|
import arrayWithHoles from "../arrayWithHoles/_index.mjs";
|
||||||
import iterableToArray from "@babel/runtime-corejs2/helpers/iterableToArray";
|
import iterableToArray from "../iterableToArray/_index.mjs";
|
||||||
import unsupportedIterableToArray from "@babel/runtime-corejs2/helpers/unsupportedIterableToArray";
|
import unsupportedIterableToArray from "../unsupportedIterableToArray/_index.mjs";
|
||||||
import nonIterableRest from "@babel/runtime-corejs2/helpers/nonIterableRest";
|
import nonIterableRest from "../nonIterableRest/_index.mjs";
|
||||||
export default function _toArray(arr) {
|
export default function _toArray(arr) {
|
||||||
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
|
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
|
||||||
}
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import undef from "@babel/runtime/helpers/temporalUndefined";
|
import undef from "../temporalUndefined/_index.mjs";
|
||||||
import err from "@babel/runtime/helpers/tdz";
|
import err from "../tdz/_index.mjs";
|
||||||
export default function _temporalRef(val, name) {
|
export default function _temporalRef(val, name) {
|
||||||
return val === undef ? err(name) : val;
|
return val === undef ? err(name) : val;
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import arrayWithHoles from "@babel/runtime/helpers/arrayWithHoles";
|
import arrayWithHoles from "../arrayWithHoles/_index.mjs";
|
||||||
import iterableToArray from "@babel/runtime/helpers/iterableToArray";
|
import iterableToArray from "../iterableToArray/_index.mjs";
|
||||||
import unsupportedIterableToArray from "@babel/runtime/helpers/unsupportedIterableToArray";
|
import unsupportedIterableToArray from "../unsupportedIterableToArray/_index.mjs";
|
||||||
import nonIterableRest from "@babel/runtime/helpers/nonIterableRest";
|
import nonIterableRest from "../nonIterableRest/_index.mjs";
|
||||||
export default function _toArray(arr) {
|
export default function _toArray(arr) {
|
||||||
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
|
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
|
||||||
}
|
}
|
||||||
48
test/runtime-integration/bundlers.cjs
Normal file
48
test/runtime-integration/bundlers.cjs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
const cp = require("child_process");
|
||||||
|
const path = require("path");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
for (const absolute of [false, true]) {
|
||||||
|
const output = absolute ? "output-absolute.js" : "output.js";
|
||||||
|
const title = absolute ? "(absolute runtime)" : "";
|
||||||
|
|
||||||
|
const webpack = absolute
|
||||||
|
? "webpack --config webpack.absolute.config.js"
|
||||||
|
: "webpack";
|
||||||
|
const rollup = absolute ? "rollup -c rollup.absolute.config.js" : "rollup -c";
|
||||||
|
|
||||||
|
// TODO: This never worked in any Babel version
|
||||||
|
if (!absolute) {
|
||||||
|
test(`Webpack 5 ${title}`, webpack, "webpack-5", output, true);
|
||||||
|
}
|
||||||
|
test(`Webpack 4 ${title}`, webpack, "webpack-4", output);
|
||||||
|
test(`Webpack 3 ${title}`, webpack, "webpack-3", output);
|
||||||
|
test(`Rollup ${title}`, rollup, "rollup", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test(name, command, directory, output, first) {
|
||||||
|
console.log(`Building with ${name}`);
|
||||||
|
cp.execSync(`yarn ${command}`, {
|
||||||
|
cwd: path.join(__dirname, directory),
|
||||||
|
encoding: "utf8",
|
||||||
|
});
|
||||||
|
console.log(`Testing the ${name} bundle`);
|
||||||
|
const out = cp.execSync(`node ${output}`, {
|
||||||
|
cwd: path.join(__dirname, directory),
|
||||||
|
encoding: "utf8",
|
||||||
|
});
|
||||||
|
|
||||||
|
const expectedPath = path.join(__dirname, "expected-bundler.txt");
|
||||||
|
let expected = fs.readFileSync(expectedPath, "utf8");
|
||||||
|
|
||||||
|
if (expected === out) {
|
||||||
|
console.log("OK");
|
||||||
|
} else if (first && process.env.OVERWRITE) {
|
||||||
|
fs.writeFileSync(expectedPath, out);
|
||||||
|
expected = out;
|
||||||
|
console.log("UPDATED");
|
||||||
|
} else {
|
||||||
|
console.error("FAILED\n");
|
||||||
|
console.error(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
test/runtime-integration/expected-bundler-absolute.txt
Normal file
20
test/runtime-integration/expected-bundler-absolute.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
================== import - auto ====================
|
||||||
|
typeof inheritsLoose: function
|
||||||
|
A.__proto__ === B true
|
||||||
|
================= import - esm ======================
|
||||||
|
typeof toArray: function
|
||||||
|
arr: 1,2,3
|
||||||
|
=============== import - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
typeof toPrimitive: object
|
||||||
|
typeof toPrimitive.default: function
|
||||||
|
Value: 2
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
20
test/runtime-integration/expected-bundler.txt
Normal file
20
test/runtime-integration/expected-bundler.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
================== import - auto ====================
|
||||||
|
typeof inheritsLoose: function
|
||||||
|
A.__proto__ === B true
|
||||||
|
================= import - esm ======================
|
||||||
|
typeof toArray: function
|
||||||
|
arr: 1,2,3
|
||||||
|
=============== import - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
typeof toPrimitive: object
|
||||||
|
typeof toPrimitive.default: function
|
||||||
|
Value: 2
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
9
test/runtime-integration/expected-cjs-10.txt
Normal file
9
test/runtime-integration/expected-cjs-10.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Unexpected token export
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
9
test/runtime-integration/expected-cjs-absolute-10.txt
Normal file
9
test/runtime-integration/expected-cjs-absolute-10.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Unexpected token export
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
9
test/runtime-integration/expected-cjs-absolute-13.0.txt
Normal file
9
test/runtime-integration/expected-cjs-absolute-13.0.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Unexpected token 'export'
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
13
test/runtime-integration/expected-cjs-absolute.txt
Normal file
13
test/runtime-integration/expected-cjs-absolute.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Must use import to load ES Module: <ROOT>/packages/babel-runtime/helpers/esm/toPrimitive.js
|
||||||
|
require() of ES modules is not supported.
|
||||||
|
require() of <ROOT>/packages/babel-runtime/helpers/esm/toPrimitive.js from <ROOT>/test/runtime-integration/src/absolute/require-esm.cjs is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
|
||||||
|
Instead rename toPrimitive.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from <ROOT>/packages/babel-runtime/helpers/esm/package.json.
|
||||||
|
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
9
test/runtime-integration/expected-cjs.txt
Normal file
9
test/runtime-integration/expected-cjs.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Must use import to load ES Module: <ROOT>/packages/babel-runtime/helpers/toPrimitive/_index.mjs
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
0
test/runtime-integration/expected-esm-absolute.txt
Normal file
0
test/runtime-integration/expected-esm-absolute.txt
Normal file
18
test/runtime-integration/expected-esm.txt
Normal file
18
test/runtime-integration/expected-esm.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
================== import - auto ====================
|
||||||
|
typeof inheritsLoose: function
|
||||||
|
A.__proto__ === B true
|
||||||
|
================= import - esm ======================
|
||||||
|
typeof toArray: function
|
||||||
|
arr: 1,2,3
|
||||||
|
=============== import - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
|
================= require - auto ====================
|
||||||
|
typeof objectWithoutProperties: function
|
||||||
|
typeof objectWithoutProperties.default: function
|
||||||
|
obj: { b: 2, [Symbol(Symbol.toStringTag)]: 5 }
|
||||||
|
================= require - esm =====================
|
||||||
|
Error: Must use import to load ES Module: <ROOT>/packages/babel-runtime/helpers/toPrimitive/_index.mjs
|
||||||
|
=============== require - corejs ====================
|
||||||
|
typeof Set: function
|
||||||
|
arr: 1,2,3
|
||||||
19
test/runtime-integration/generate-absolute-runtime.cjs
Normal file
19
test/runtime-integration/generate-absolute-runtime.cjs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
const runtimePath = path.resolve(__dirname, "../../packages/babel-runtime");
|
||||||
|
const runtimeCorejs3Path = path.resolve(
|
||||||
|
__dirname,
|
||||||
|
"../../packages/babel-runtime-corejs3"
|
||||||
|
);
|
||||||
|
const input = path.resolve(__dirname, "src");
|
||||||
|
|
||||||
|
for (const file of fs.readdirSync(input)) {
|
||||||
|
if (!/\.[cm]js$/.test(file)) continue;
|
||||||
|
|
||||||
|
let contents = fs.readFileSync(path.join(input, file), "utf8");
|
||||||
|
contents = contents.replace("@babel/runtime-corejs3", runtimeCorejs3Path);
|
||||||
|
contents = contents.replace("@babel/runtime", runtimePath);
|
||||||
|
|
||||||
|
fs.writeFileSync(path.resolve(input, "absolute", file), contents);
|
||||||
|
}
|
||||||
65
test/runtime-integration/node.cjs
Normal file
65
test/runtime-integration/node.cjs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
const cp = require("child_process");
|
||||||
|
const path = require("path");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
const [major, minor] = process.versions.node.split(".").map(n => +n);
|
||||||
|
|
||||||
|
if (major > 12 || (major === 12 && minor >= 17)) {
|
||||||
|
test("ESM", "--experimental-modules ./src/main-esm.mjs", "expected-esm.txt");
|
||||||
|
// TODO: This never worked in any Babel version
|
||||||
|
// test("ESM - absoluteRuntime", "--esperimental-modules ./src/absolute/main-esm.mjs", "expected-esm-absolute.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
const expectedCjs =
|
||||||
|
major === 10 || (major === 12 && minor < 12.17)
|
||||||
|
? "expected-cjs-10.txt"
|
||||||
|
: "expected-cjs.txt";
|
||||||
|
|
||||||
|
test("CJS", "./src/main-cjs.cjs", expectedCjs);
|
||||||
|
|
||||||
|
const expectedCjsAbsolute =
|
||||||
|
major === 10 || (major === 12 && minor < 12.17)
|
||||||
|
? "expected-cjs-absolute-10.txt"
|
||||||
|
: major === 13 && minor <= 1
|
||||||
|
? "expected-cjs-absolute-13.0.txt"
|
||||||
|
: "expected-cjs-absolute.txt";
|
||||||
|
|
||||||
|
test(
|
||||||
|
"CJS - absoluteRuntime",
|
||||||
|
"./src/absolute/main-cjs.cjs",
|
||||||
|
expectedCjsAbsolute
|
||||||
|
);
|
||||||
|
|
||||||
|
function test(title, command, expectedName) {
|
||||||
|
const expectedPath = path.join(__dirname, expectedName);
|
||||||
|
const expected = fs.readFileSync(expectedPath, "utf8");
|
||||||
|
|
||||||
|
console.log(`Testing with Node.js ${process.version} - ${title}`);
|
||||||
|
const out = normalize(
|
||||||
|
cp.execSync(`node ${command}`, {
|
||||||
|
cwd: __dirname,
|
||||||
|
encoding: "utf8",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (expected === out) {
|
||||||
|
console.log("OK");
|
||||||
|
} else if (process.env.OVERWRITE) {
|
||||||
|
fs.writeFileSync(expectedPath, out);
|
||||||
|
console.log("UPDATED");
|
||||||
|
} else {
|
||||||
|
console.error("FAILED\n\n");
|
||||||
|
console.error(out);
|
||||||
|
process.exitCode = 1;
|
||||||
|
}
|
||||||
|
console.log("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalize(output) {
|
||||||
|
const root = path.resolve(__dirname, "../..");
|
||||||
|
let next;
|
||||||
|
while ((next = output.replace(root, "<ROOT>")) !== output) {
|
||||||
|
output = next;
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
10
test/runtime-integration/rollup/package.json
Normal file
10
test/runtime-integration/rollup/package.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "@babel-internal/runtime-integration-rollup",
|
||||||
|
"private": true,
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/runtime": "workspace:*",
|
||||||
|
"@rollup/plugin-commonjs": "^17.1.0",
|
||||||
|
"@rollup/plugin-node-resolve": "^11.2.0",
|
||||||
|
"rollup": "^2.39.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
13
test/runtime-integration/rollup/rollup.absolute.config.js
Normal file
13
test/runtime-integration/rollup/rollup.absolute.config.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import commonjs from "@rollup/plugin-commonjs";
|
||||||
|
import nodeResolve from "@rollup/plugin-node-resolve";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
input: path.resolve(__dirname, "../src/absolute/main-esm.mjs"),
|
||||||
|
plugins: [commonjs(), nodeResolve()],
|
||||||
|
|
||||||
|
output: {
|
||||||
|
file: path.resolve(__dirname, "output-absolute.js"),
|
||||||
|
format: "cjs",
|
||||||
|
},
|
||||||
|
};
|
||||||
13
test/runtime-integration/rollup/rollup.config.js
Normal file
13
test/runtime-integration/rollup/rollup.config.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import commonjs from "@rollup/plugin-commonjs";
|
||||||
|
import nodeResolve from "@rollup/plugin-node-resolve";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
input: path.resolve(__dirname, "../src/main-esm.mjs"),
|
||||||
|
plugins: [commonjs(), nodeResolve()],
|
||||||
|
|
||||||
|
output: {
|
||||||
|
file: path.resolve(__dirname, "output.js"),
|
||||||
|
format: "cjs",
|
||||||
|
},
|
||||||
|
};
|
||||||
2
test/runtime-integration/src/absolute/.gitignore
vendored
Normal file
2
test/runtime-integration/src/absolute/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
||||||
10
test/runtime-integration/src/import-auto.mjs
Normal file
10
test/runtime-integration/src/import-auto.mjs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
|
||||||
|
|
||||||
|
console.log("================== import - auto ====================");
|
||||||
|
console.log("typeof inheritsLoose:", typeof inheritsLoose);
|
||||||
|
|
||||||
|
function A() {}
|
||||||
|
function B() {}
|
||||||
|
inheritsLoose(A, B);
|
||||||
|
|
||||||
|
console.log("A.__proto__ === B", A.__proto__ === B);
|
||||||
7
test/runtime-integration/src/import-corejs.mjs
Normal file
7
test/runtime-integration/src/import-corejs.mjs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import Set from "@babel/runtime-corejs3/core-js/set.js";
|
||||||
|
|
||||||
|
console.log("=============== import - corejs ====================");
|
||||||
|
console.log("typeof Set:", typeof Set);
|
||||||
|
|
||||||
|
const arr = Array.from(new Set([1, 2, 3]));
|
||||||
|
console.log("arr:", arr.toString());
|
||||||
8
test/runtime-integration/src/import-esm.mjs
Normal file
8
test/runtime-integration/src/import-esm.mjs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import toArray from "@babel/runtime/helpers/esm/toArray";
|
||||||
|
|
||||||
|
console.log("================= import - esm ======================");
|
||||||
|
console.log("typeof toArray:", typeof toArray);
|
||||||
|
|
||||||
|
const arr = toArray(new Set([1, 2, 3]));
|
||||||
|
|
||||||
|
console.log("arr:", arr.toString());
|
||||||
3
test/runtime-integration/src/main-cjs.cjs
Normal file
3
test/runtime-integration/src/main-cjs.cjs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
require("./require-auto.cjs");
|
||||||
|
require("./require-esm.cjs");
|
||||||
|
require("./require-corejs.cjs");
|
||||||
6
test/runtime-integration/src/main-esm.mjs
Normal file
6
test/runtime-integration/src/main-esm.mjs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import "./import-auto.mjs";
|
||||||
|
import "./import-esm.mjs";
|
||||||
|
import "./import-corejs.mjs";
|
||||||
|
import "./require-auto.cjs";
|
||||||
|
import "./require-esm.cjs";
|
||||||
|
import "./require-corejs.cjs";
|
||||||
8
test/runtime-integration/src/package.json
Normal file
8
test/runtime-integration/src/package.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "@babel-internal/runtime-integration-src",
|
||||||
|
"private": true,
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/runtime": "workspace:*",
|
||||||
|
"@babel/runtime-corejs3": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
test/runtime-integration/src/require-auto.cjs
Normal file
14
test/runtime-integration/src/require-auto.cjs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const objectWithoutProperties = require("@babel/runtime/helpers/objectWithoutProperties");
|
||||||
|
|
||||||
|
console.log("================= require - auto ====================");
|
||||||
|
console.log("typeof objectWithoutProperties:", typeof objectWithoutProperties);
|
||||||
|
console.log(
|
||||||
|
"typeof objectWithoutProperties.default:",
|
||||||
|
typeof objectWithoutProperties.default
|
||||||
|
);
|
||||||
|
|
||||||
|
const obj = objectWithoutProperties(
|
||||||
|
{ a: 1, b: 2, c: 3, [Symbol.iterator]: 4, [Symbol.toStringTag]: 5 },
|
||||||
|
["a", "c", Symbol.iterator]
|
||||||
|
);
|
||||||
|
console.log("obj:", obj);
|
||||||
7
test/runtime-integration/src/require-corejs.cjs
Normal file
7
test/runtime-integration/src/require-corejs.cjs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
const Set = require("@babel/runtime-corejs3/core-js/set.js");
|
||||||
|
|
||||||
|
console.log("=============== require - corejs ====================");
|
||||||
|
console.log("typeof Set:", typeof Set);
|
||||||
|
|
||||||
|
const arr = Array.from(new Set([1, 2, 3]));
|
||||||
|
console.log("arr:", arr.toString());
|
||||||
13
test/runtime-integration/src/require-esm.cjs
Normal file
13
test/runtime-integration/src/require-esm.cjs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
console.log("================= require - esm =====================");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const toPrimitive = require("@babel/runtime/helpers/esm/toPrimitive");
|
||||||
|
|
||||||
|
console.log("typeof toPrimitive:", typeof toPrimitive);
|
||||||
|
console.log("typeof toPrimitive.default:", typeof toPrimitive.default);
|
||||||
|
|
||||||
|
const value = toPrimitive.default({ valueOf: () => 2 });
|
||||||
|
console.log("Value:", value);
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error:", error.message);
|
||||||
|
}
|
||||||
8
test/runtime-integration/webpack-3/package.json
Normal file
8
test/runtime-integration/webpack-3/package.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "@babel-internal/runtime-integration-webpack-3",
|
||||||
|
"private": true,
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/runtime": "workspace:*",
|
||||||
|
"webpack": "^3.12.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: path.join(__dirname, "../src/absolute/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output-absolute.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
11
test/runtime-integration/webpack-3/webpack.config.js
Normal file
11
test/runtime-integration/webpack-3/webpack.config.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: path.join(__dirname, "../src/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
9
test/runtime-integration/webpack-4/package.json
Normal file
9
test/runtime-integration/webpack-4/package.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "@babel-internal/runtime-integration-webpack-4",
|
||||||
|
"private": true,
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/runtime": "workspace:*",
|
||||||
|
"webpack": "^4.46.0",
|
||||||
|
"webpack-cli": "^4.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
|
||||||
|
entry: path.join(__dirname, "../src/absolute/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output-absolute.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
13
test/runtime-integration/webpack-4/webpack.config.js
Normal file
13
test/runtime-integration/webpack-4/webpack.config.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
|
||||||
|
entry: path.join(__dirname, "../src/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
10
test/runtime-integration/webpack-5/package.json
Normal file
10
test/runtime-integration/webpack-5/package.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "@babel-internal/runtime-integration-webpack-5",
|
||||||
|
"private": true,
|
||||||
|
"exports": "./index.js",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/runtime": "workspace:*",
|
||||||
|
"webpack": "^5.24.1",
|
||||||
|
"webpack-cli": "^4.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
|
||||||
|
entry: path.join(__dirname, "../src/absolute/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output-absolute.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
13
test/runtime-integration/webpack-5/webpack.config.js
Normal file
13
test/runtime-integration/webpack-5/webpack.config.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: "development",
|
||||||
|
|
||||||
|
entry: path.join(__dirname, "../src/main-esm.mjs"),
|
||||||
|
output: {
|
||||||
|
path: __dirname,
|
||||||
|
filename: "output.js",
|
||||||
|
},
|
||||||
|
|
||||||
|
devtool: false,
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user