/
node.cjs
65 lines (55 loc) 路 1.74 KB
/
node.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}