diff --git a/scripts/tools/bundle-test/index.js b/scripts/tools/bundle-test/index.js index 7bac6724d10c..2150dc876da4 100644 --- a/scripts/tools/bundle-test/index.js +++ b/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"; @@ -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, @@ -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."); } })();