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.

81 lines
2.0 KiB
JavaScript
Raw Normal View History

import { createRequire } from "node:module";
import path from "node:path";
import url from "node:url";
2022-03-23 10:21:02 +08:00
import webpack from "webpack";
2025-04-09 08:38:46 +08:00
import packageConfig from "../../build/config.js";
2022-03-23 10:21:02 +08:00
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);
});
});
}
2022-10-06 21:22:41 +08:00
const TEMPORARY_DIRECTORY = url.fileURLToPath(
new URL("./.tmp", import.meta.url),
);
2022-03-23 10:21:02 +08:00
2025-04-09 08:38:46 +08:00
const { distDirectory, files } = packageConfig;
2022-03-23 10:21:02 +08:00
/* `require` in `parser-typescript.js`, #12338 */
2022-10-06 21:22:41 +08:00
for (const file of files) {
if (file.platform !== "universal") {
continue;
2022-03-23 10:21:02 +08:00
}
2022-10-06 21:22:41 +08:00
console.log(`${file.output.file}: `);
const stats = await runWebpack({
mode: "production",
2025-04-09 08:38:46 +08:00
entry: path.join(distDirectory, file.output.file),
2022-10-06 21:22:41 +08:00
output: {
path: TEMPORARY_DIRECTORY,
filename: `${file.output.file}.[contenthash:7].${
file.output.format === "esm" ? "mjs" : "cjs"
}`,
},
performance: { hints: false },
optimization: { minimize: false },
});
const result = stats.toJson();
const { warnings, assets } = result;
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.");
}
const outputFileName = assets[0].name;
const outputFile = path.join(TEMPORARY_DIRECTORY, outputFileName);
const require = createRequire(import.meta.url);
try {
if (file.output.format !== "esm") {
require(outputFile);
} else {
await import(url.pathToFileURL(outputFile));
}
} catch (error) {
console.log(`'${outputFileName}' is not functional.`);
throw error;
}
console.log(" Passed.");
}