Skip to content

Commit

Permalink
Add test for prettier#12493
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 23, 2022
1 parent 4250812 commit 0c7a6df
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions scripts/tools/bundle-test/index.js
@@ -1,6 +1,7 @@
import { fileURLToPath } from "node:url";
import path from "node:path";
import fs from "node:fs/promises";
import { createRequire } from "node:module";
import webpack from "webpack";
import { DIST_DIR } from "../../../scripts/utils/index.mjs";

Expand Down Expand Up @@ -39,7 +40,11 @@ const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url));
name === "standalone.js" ||
name === "doc.js"
)
.map((name) => ({ name, file: path.join(DIST_DIR, name) })),
.map((name) => ({
displayName: name,
name,
file: path.join(DIST_DIR, name),
})),
(await fs.readdir(esmFilesDirectory)).map((name) => ({
displayName: `esm/${name}`,
name,
Expand All @@ -48,26 +53,44 @@ const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url));
].flat();

for (const { displayName, name, file } of files) {
console.log(`${displayName || name}: `);
console.log(`${displayName}: `);
const isEsmModule = name.endsWith(".mjs");

const stats = await runWebpack({
mode: "production",
entry: file,
output: {
path: TEMPORARY_DIRECTORY,
filename: `${name}.[contenthash:7].js`,
filename: `${name}.[contenthash:7].${isEsmModule ? "mjs" : "cjs"}`,
},
performance: { hints: false },
optimization: { minimize: false },
});
const result = stats.toJson();
const { warnings } = result;
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.");
}

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;
}
}

console.log(" Passed.");
}
})();

0 comments on commit 0c7a6df

Please sign in to comment.