Files
test-repo/scripts/tools/bundle-test/index.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-03-23 10:21:02 +08:00
import { fileURLToPath } from "node:url";
import path from "node:path";
import fs from "node:fs/promises";
import { createRequire } from "node:module";
2022-03-23 10:21:02 +08:00
import webpack from "webpack";
import { DIST_DIR } from "../../../scripts/utils/index.mjs";
function runWebpack(config) {
return new Promise((resolve, reject) => {
webpack(config, (error, stats) => {
if (error) {
reject(error);
return;
}
if (stats.hasErrors()) {
const { errors } = stats.toJson();
const error = new Error(errors[0].message);
error.errors = errors;
reject(error);
return;
}
resolve(stats);
});
});
}
const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url));
/* `require` in `parser-typescript.js`, #12338 */
(async () => {
const esmFilesDirectory = path.join(DIST_DIR, "esm");
const files = [
(await fs.readdir(DIST_DIR))
.filter(
(name) =>
name.startsWith("parser-") ||
name === "standalone.js" ||
name === "doc.js"
)
.map((name) => ({
displayName: name,
name,
file: path.join(DIST_DIR, name),
})),
2022-03-23 10:21:02 +08:00
(await fs.readdir(esmFilesDirectory)).map((name) => ({
displayName: `esm/${name}`,
name,
file: path.join(esmFilesDirectory, name),
})),
].flat();
for (const { displayName, name, file } of files) {
console.log(`${displayName}: `);
const isEsmModule = name.endsWith(".mjs");
2022-03-23 10:21:02 +08:00
const stats = await runWebpack({
mode: "production",
entry: file,
output: {
path: TEMPORARY_DIRECTORY,
filename: `${name}.[contenthash:7].${isEsmModule ? "mjs" : "cjs"}`,
2022-03-23 10:21:02 +08:00
},
performance: { hints: false },
optimization: { minimize: false },
});
const result = stats.toJson();
const { warnings, assets } = result;
2022-03-23 10:21:02 +08:00
if (warnings.length > 0) {
console.log(warnings);
throw new Error("Unexpected webpack warning.");
}
if (assets.length > 1) {
console.log(assets);
throw new Error("Unexpected assets.");
}
if (!isEsmModule) {
const outputFile = assets[0].name;
const require = createRequire(import.meta.url);
try {
require(path.join(TEMPORARY_DIRECTORY, assets[0].name));
} catch (error) {
console.log(`'${outputFile}' is not functional.`);
throw error;
}
}
2022-03-23 10:21:02 +08:00
console.log(" Passed.");
}
})();