|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Alexander Akait @alexander-akait |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +// Counts the modules a fresh process loads for each way webpack is used, and |
| 9 | +// fails when a count grows past `module-loading.json` or a module outside |
| 10 | +// webpack loads one of its internal files: such a file then hangs under that |
| 11 | +// package, so the cache's build-dependency walk drags all of webpack in. |
| 12 | +// |
| 13 | +// node tooling/check-module-loading.js # check |
| 14 | +// node tooling/check-module-loading.js --write # rewrite the baseline |
| 15 | + |
| 16 | +const { fork } = require("child_process"); |
| 17 | +const fs = require("fs"); |
| 18 | +const os = require("os"); |
| 19 | +const path = require("path"); |
| 20 | +const { fileURLToPath, pathToFileURL } = require("url"); |
| 21 | + |
| 22 | +const ROOT = path.resolve(__dirname, ".."); |
| 23 | +const INDEX = path.join(ROOT, "lib/index.js"); |
| 24 | +const NODE_MODULES = path.join(ROOT, "node_modules") + path.sep; |
| 25 | +const BASELINE = path.join(__dirname, "module-loading.json"); |
| 26 | + |
| 27 | +/** @typedef {{ webpack: number, dependencies: number }} Counts */ |
| 28 | +/** @typedef {Counts & { outsideLoads: string[] }} ScenarioResult */ |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {string} name a minifier's path under `lib/` |
| 32 | + * @param {string} file the asset's filename |
| 33 | + * @param {string} code the asset's source |
| 34 | + * @returns {() => Promise<void>} a scenario running that minifier once |
| 35 | + */ |
| 36 | +const minify = (name, file, code) => async () => { |
| 37 | + await require(path.join(ROOT, "lib", name))({ [file]: code }); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * @returns {Promise<void>} resolves once a production build of JavaScript, CSS |
| 42 | + * and HTML with the default minimizers has finished |
| 43 | + */ |
| 44 | +const build = async () => { |
| 45 | + const context = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-loading-")); |
| 46 | + fs.writeFileSync( |
| 47 | + path.join(context, "index.html"), |
| 48 | + '<!doctype html><link rel="stylesheet" href="./style.css"><script src="./index.js"></script>' |
| 49 | + ); |
| 50 | + fs.writeFileSync(path.join(context, "style.css"), "body { color: red; }"); |
| 51 | + fs.writeFileSync(path.join(context, "index.js"), "console.log(1 + 1);"); |
| 52 | + |
| 53 | + const webpack = require(INDEX); |
| 54 | + |
| 55 | + try { |
| 56 | + await new Promise((resolve, reject) => { |
| 57 | + webpack( |
| 58 | + { |
| 59 | + mode: "production", |
| 60 | + context, |
| 61 | + entry: "./index.html", |
| 62 | + output: { path: path.join(context, "dist") }, |
| 63 | + experiments: { css: true, html: true } |
| 64 | + }, |
| 65 | + /** |
| 66 | + * @param {Error | null} err a fatal error |
| 67 | + * @param {import("../lib/stats/Stats") | undefined} stats the build's stats |
| 68 | + * @returns {void} |
| 69 | + */ |
| 70 | + (err, stats) => { |
| 71 | + if (err) return reject(err); |
| 72 | + if (stats && stats.hasErrors()) { |
| 73 | + return reject(new Error(stats.toString("errors-only"))); |
| 74 | + } |
| 75 | + resolve(undefined); |
| 76 | + } |
| 77 | + ); |
| 78 | + }); |
| 79 | + } finally { |
| 80 | + fs.rmSync(context, { recursive: true, force: true }); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +/** @type {Record<string, () => Promise<void>>} */ |
| 85 | +const SCENARIOS = { |
| 86 | + 'require("webpack")': async () => { |
| 87 | + require(INDEX); |
| 88 | + }, |
| 89 | + 'import "webpack"': async () => { |
| 90 | + await import(pathToFileURL(INDEX).href); |
| 91 | + }, |
| 92 | + "javascript minify": minify( |
| 93 | + "javascript/jsMinify", |
| 94 | + "a.js", |
| 95 | + "function add(a, b) { return a + b; } console.log(add(1, 2));" |
| 96 | + ), |
| 97 | + "css minify": minify("css/cssMinify", "a.css", "a { color: #ff0000; }"), |
| 98 | + "html minify": minify( |
| 99 | + "html/htmlMinify", |
| 100 | + "a.html", |
| 101 | + "<!doctype html><p class=' a '>text</p><style>a { color: red }</style>" |
| 102 | + ), |
| 103 | + "xml minify": minify( |
| 104 | + "html/xmlMinify", |
| 105 | + "a.svg", |
| 106 | + '<svg xmlns="http://www.w3.org/2000/svg"> <g/> </svg>' |
| 107 | + ), |
| 108 | + "json minify": minify("json/jsonMinify", "a.json", '{ "a": [1, 2] }'), |
| 109 | + "production build": build |
| 110 | +}; |
| 111 | + |
| 112 | +/** |
| 113 | + * Runs one scenario in this process, recording every module it loads and the |
| 114 | + * module that first asked for it, then reports to the parent process. |
| 115 | + * @param {string} name the scenario |
| 116 | + * @returns {Promise<void>} resolves once the result has been sent |
| 117 | + */ |
| 118 | +const runScenario = async (name) => { |
| 119 | + const { registerHooks } = require("module"); |
| 120 | + |
| 121 | + /** @type {Map<string, string | undefined>} */ |
| 122 | + const firstParent = new Map(); |
| 123 | + /** @type {Set<string>} */ |
| 124 | + const loaded = new Set(); |
| 125 | + registerHooks({ |
| 126 | + resolve(specifier, context, nextResolve) { |
| 127 | + const result = nextResolve(specifier, context); |
| 128 | + if (!firstParent.has(result.url)) { |
| 129 | + firstParent.set(result.url, context.parentURL); |
| 130 | + } |
| 131 | + return result; |
| 132 | + }, |
| 133 | + load(url, context, nextLoad) { |
| 134 | + loaded.add(url); |
| 135 | + return nextLoad(url, context); |
| 136 | + } |
| 137 | + }); |
| 138 | + await SCENARIOS[name](); |
| 139 | + const self = pathToFileURL(__filename).href; |
| 140 | + /** @type {ScenarioResult} */ |
| 141 | + const result = { webpack: 0, dependencies: 0, outsideLoads: [] }; |
| 142 | + for (const url of loaded) { |
| 143 | + if (!url.startsWith("file:")) continue; |
| 144 | + const file = fs.realpathSync(fileURLToPath(url)); |
| 145 | + if (file.startsWith(NODE_MODULES)) { |
| 146 | + result.dependencies++; |
| 147 | + continue; |
| 148 | + } |
| 149 | + if (!file.startsWith(ROOT + path.sep) || file === __filename) continue; |
| 150 | + result.webpack++; |
| 151 | + const parentUrl = firstParent.get(url); |
| 152 | + if (file === INDEX || !parentUrl || parentUrl === self) continue; |
| 153 | + const parent = fs.realpathSync(fileURLToPath(parentUrl)); |
| 154 | + if (parent.startsWith(NODE_MODULES) || !parent.startsWith(ROOT)) { |
| 155 | + result.outsideLoads.push( |
| 156 | + `${path.relative(ROOT, parent)} -> ${path.relative(ROOT, file)}` |
| 157 | + ); |
| 158 | + } |
| 159 | + } |
| 160 | + /** @type {NonNullable<typeof process.send>} */ (process.send)(result); |
| 161 | +}; |
| 162 | + |
| 163 | +/** |
| 164 | + * @param {string} name the scenario |
| 165 | + * @returns {Promise<ScenarioResult>} what a fresh process running it loaded |
| 166 | + */ |
| 167 | +const measure = (name) => |
| 168 | + new Promise((resolve, reject) => { |
| 169 | + /** @type {ScenarioResult | undefined} */ |
| 170 | + let result; |
| 171 | + const child = fork(__filename, ["--scenario", name], { stdio: "inherit" }); |
| 172 | + child.on("message", (message) => { |
| 173 | + result = /** @type {ScenarioResult} */ (message); |
| 174 | + }); |
| 175 | + child.on("error", reject); |
| 176 | + child.on("exit", (code) => { |
| 177 | + if (code === 0 && result) return resolve(result); |
| 178 | + reject(new Error(`scenario "${name}" exited with code ${code}`)); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | +const main = async () => { |
| 183 | + if (typeof require("module").registerHooks !== "function") { |
| 184 | + console.log( |
| 185 | + `Skipped: module.registerHooks needs Node.js 22.15 or newer (running ${process.version}).` |
| 186 | + ); |
| 187 | + return; |
| 188 | + } |
| 189 | + const write = process.argv.includes("--write"); |
| 190 | + /** @type {Record<string, Counts>} */ |
| 191 | + const baseline = fs.existsSync(BASELINE) |
| 192 | + ? JSON.parse(fs.readFileSync(BASELINE, "utf8")) |
| 193 | + : {}; |
| 194 | + /** @type {Record<string, Counts>} */ |
| 195 | + const next = {}; |
| 196 | + /** @type {string[]} */ |
| 197 | + const failures = []; |
| 198 | + /** @type {string[]} */ |
| 199 | + const rows = []; |
| 200 | + for (const name of Object.keys(SCENARIOS)) { |
| 201 | + const { webpack, dependencies, outsideLoads } = await measure(name); |
| 202 | + next[name] = { webpack, dependencies }; |
| 203 | + const before = baseline[name] || { webpack: 0, dependencies: 0 }; |
| 204 | + rows.push( |
| 205 | + `| ${name} | ${webpack} | ${before.webpack} | ${dependencies} | ${before.dependencies} |` |
| 206 | + ); |
| 207 | + for (const load of outsideLoads) { |
| 208 | + failures.push( |
| 209 | + `${name}: a module outside webpack loads an internal file: ${load}` |
| 210 | + ); |
| 211 | + } |
| 212 | + if (!write) { |
| 213 | + for (const key of /** @type {(keyof Counts)[]} */ ([ |
| 214 | + "webpack", |
| 215 | + "dependencies" |
| 216 | + ])) { |
| 217 | + if (next[name][key] > before[key]) { |
| 218 | + failures.push( |
| 219 | + `${name}: loads ${next[name][key]} ${key} modules, over its baseline of ${before[key]}` |
| 220 | + ); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + console.log( |
| 226 | + "| Scenario | webpack files | baseline | dependency files | baseline |" |
| 227 | + ); |
| 228 | + console.log("| :-- | --: | --: | --: | --: |"); |
| 229 | + for (const row of rows) console.log(row); |
| 230 | + if (write) { |
| 231 | + fs.writeFileSync(BASELINE, `${JSON.stringify(next, null, 2)}\n`); |
| 232 | + console.log(`Wrote ${path.relative(ROOT, BASELINE)}`); |
| 233 | + } |
| 234 | + if (failures.length > 0) { |
| 235 | + console.error(`\n${failures.join("\n")}`); |
| 236 | + console.error( |
| 237 | + "\nLoad a module only where it is used, and hand other packages functions rather than paths to webpack's files; if the growth is intended, run with --write." |
| 238 | + ); |
| 239 | + process.exitCode = 1; |
| 240 | + } |
| 241 | +}; |
| 242 | + |
| 243 | +const scenarioIndex = process.argv.indexOf("--scenario"); |
| 244 | +if (scenarioIndex === -1) { |
| 245 | + main().catch((err) => { |
| 246 | + console.error(err); |
| 247 | + process.exitCode = 1; |
| 248 | + }); |
| 249 | +} else { |
| 250 | + runScenario(process.argv[scenarioIndex + 1]).catch((err) => { |
| 251 | + console.error(err); |
| 252 | + process.exitCode = 1; |
| 253 | + }); |
| 254 | +} |
0 commit comments