Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test
@babel/runtime
behavior with Webpack and old Node.js
- Loading branch information
1 parent
8b20b01
commit e7a61df
Showing
38 changed files
with
2,660 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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"); | ||
} | ||
|
||
test("CJS", "./src/main-cjs.cjs", "expected-cjs.txt"); | ||
|
||
const expectedCjsAbsolute = | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
Oops, something went wrong.