|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +// Emits a compact static/dynamic module graph for the eager-graph budget check |
| 5 | +// (scripts/bundle/check-eager-graph.mjs). Only wired into the production |
| 6 | +// `build-javascript` stage, and only when EMIT_WEBPACK_STATS=true, so it never slows |
| 7 | +// local dev or normal CI builds. |
| 8 | +// |
| 9 | +// webpack 5 exposes the distinction we need directly on each module: |
| 10 | +// - module.dependencies -> synchronous (STATIC) edges |
| 11 | +// - module.blocks -> AsyncDependenciesBlock[], the dynamic import() edges |
| 12 | +// We resolve each dependency to its target module via compilation.moduleGraph and key |
| 13 | +// everything by a dense integer index (module.identifier() strings are unique but huge). |
| 14 | +class EmitWebpackGraphPlugin { |
| 15 | + constructor(outputPath) { |
| 16 | + this.outputPath = outputPath |
| 17 | + } |
| 18 | + |
| 19 | + apply(compiler) { |
| 20 | + compiler.hooks.emit.tapAsync('EmitWebpackGraphPlugin', (compilation, callback) => { |
| 21 | + try { |
| 22 | + this.writeGraph(compilation) |
| 23 | + } catch (error) { |
| 24 | + compilation.warnings.push( |
| 25 | + new Error(`EmitWebpackGraphPlugin: failed to write webpack graph: ${error.stack || error}`) |
| 26 | + ) |
| 27 | + } |
| 28 | + callback() |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + writeGraph(compilation) { |
| 33 | + const { moduleGraph, chunkGraph } = compilation |
| 34 | + const moduleList = [...compilation.modules] |
| 35 | + const indexOf = new Map() |
| 36 | + moduleList.forEach((module, i) => indexOf.set(module, i)) |
| 37 | + |
| 38 | + const sizeOf = (module) => { |
| 39 | + try { |
| 40 | + return module.size() || 0 |
| 41 | + } catch { |
| 42 | + return 0 |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const collectBlockTargets = (block, into) => { |
| 47 | + for (const dependency of block.dependencies || []) { |
| 48 | + const target = moduleGraph.getModule(dependency) |
| 49 | + const targetIndex = target && indexOf.get(target) |
| 50 | + if (targetIndex !== undefined) { |
| 51 | + into.add(targetIndex) |
| 52 | + } |
| 53 | + } |
| 54 | + for (const nested of block.blocks || []) { |
| 55 | + collectBlockTargets(nested, into) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const modules = {} |
| 60 | + moduleList.forEach((module, i) => { |
| 61 | + const staticTargets = new Set() |
| 62 | + for (const dependency of module.dependencies || []) { |
| 63 | + const target = moduleGraph.getModule(dependency) |
| 64 | + const targetIndex = target && indexOf.get(target) |
| 65 | + if (targetIndex !== undefined && targetIndex !== i) { |
| 66 | + staticTargets.add(targetIndex) |
| 67 | + } |
| 68 | + } |
| 69 | + const dynamicTargets = new Set() |
| 70 | + for (const block of module.blocks || []) { |
| 71 | + collectBlockTargets(block, dynamicTargets) |
| 72 | + } |
| 73 | + modules[i] = { |
| 74 | + name: module.readableIdentifier(compilation.requestShortener), |
| 75 | + size: sizeOf(module), |
| 76 | + static: [...staticTargets], |
| 77 | + dynamic: [...dynamicTargets], |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + const entrypoints = {} |
| 82 | + for (const [name, entrypoint] of compilation.entrypoints) { |
| 83 | + const chunk = entrypoint.getEntrypointChunk() |
| 84 | + const roots = [] |
| 85 | + for (const module of chunkGraph.getChunkEntryModulesIterable(chunk)) { |
| 86 | + const moduleIndex = indexOf.get(module) |
| 87 | + if (moduleIndex !== undefined) { |
| 88 | + roots.push(moduleIndex) |
| 89 | + } |
| 90 | + } |
| 91 | + entrypoints[name] = { roots } |
| 92 | + } |
| 93 | + |
| 94 | + fs.mkdirSync(path.dirname(this.outputPath), { recursive: true }) |
| 95 | + fs.writeFileSync(this.outputPath, JSON.stringify({ entrypoints, modules })) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = { EmitWebpackGraphPlugin } |
0 commit comments