|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +/** @import { AsyncDependenciesBlock, Compilation, Compiler, Configuration, Dependency, Module, NormalModule } from "../../../../" */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Collects the dependencies of a block and its nested blocks. |
| 9 | + * @param {Module | AsyncDependenciesBlock} block the block |
| 10 | + * @param {Dependency[]} dependencies collected dependencies |
| 11 | + * @returns {Dependency[]} collected dependencies |
| 12 | + */ |
| 13 | +const collectDependencies = (block, dependencies) => { |
| 14 | + for (const dependency of block.dependencies) dependencies.push(dependency); |
| 15 | + for (const child of block.blocks) collectDependencies(child, dependencies); |
| 16 | + return dependencies; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Groups the module dependencies of a module by request, in source order. |
| 21 | + * @param {Module} module the module |
| 22 | + * @returns {Map<string, Dependency[]>} request -> dependencies |
| 23 | + */ |
| 24 | +const groupByRequest = (module) => { |
| 25 | + /** @type {Map<string, Dependency[]>} */ |
| 26 | + const groups = new Map(); |
| 27 | + for (const dependency of collectDependencies(module, [])) { |
| 28 | + const request = /** @type {{ request?: string }} */ (dependency).request; |
| 29 | + if (typeof request !== "string") continue; |
| 30 | + const group = groups.get(request); |
| 31 | + if (group === undefined) { |
| 32 | + groups.set(request, [dependency]); |
| 33 | + } else { |
| 34 | + group.push(dependency); |
| 35 | + } |
| 36 | + } |
| 37 | + return groups; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Returns the sorted export names the dependencies of one edge forward, `*` for all. |
| 42 | + * @param {Dependency[]} dependencies dependencies of the edge |
| 43 | + * @returns {string} edge label, empty when nothing is forwarded |
| 44 | + */ |
| 45 | +const getForwardIds = (dependencies) => { |
| 46 | + /** @type {Set<string>} */ |
| 47 | + const ids = new Set(); |
| 48 | + for (const dependency of dependencies) { |
| 49 | + const id = dependency.getForwardId(); |
| 50 | + if (id === true) return "*"; |
| 51 | + if (id !== null) ids.add(id); |
| 52 | + } |
| 53 | + return [...ids].sort().join(", "); |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Prints the module graph from the entries, each edge labeled with its forwarded ids. A side-effect-free module with edges is tagged `[barrel]`; an unresolved edge prints its request tagged `[raw]`, plus `[lazy]` when lazy barrel left it unbuilt. |
| 58 | + * @param {Compilation} compilation the compilation, read before seal |
| 59 | + * @returns {string} the printed graph |
| 60 | + */ |
| 61 | +const printLazyBarrelGraph = (compilation) => { |
| 62 | + const { moduleGraph, requestShortener } = compilation; |
| 63 | + /** @type {string[]} */ |
| 64 | + const lines = []; |
| 65 | + /** @type {Set<Module>} */ |
| 66 | + const seen = new Set(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Prints a module, tagged `[barrel]` when side-effect-free with edges lazy barrel may defer. |
| 70 | + * @param {Module} module the module |
| 71 | + * @returns {string} the printed module |
| 72 | + */ |
| 73 | + const printModule = (module) => { |
| 74 | + const name = module.readableIdentifier(requestShortener); |
| 75 | + const { factoryMeta } = module; |
| 76 | + return factoryMeta !== undefined && |
| 77 | + factoryMeta.sideEffectFree && |
| 78 | + groupByRequest(module).size > 0 |
| 79 | + ? `${name} [barrel]` |
| 80 | + : name; |
| 81 | + }; |
| 82 | + |
| 83 | + /** |
| 84 | + * Prints the outgoing edges of a module below it. |
| 85 | + * @param {Module} module the module |
| 86 | + * @param {string} prefix tree prefix of the edge lines |
| 87 | + */ |
| 88 | + const walk = (module, prefix) => { |
| 89 | + const groups = [...groupByRequest(module)]; |
| 90 | + for (let i = 0; i < groups.length; i++) { |
| 91 | + const [request, dependencies] = groups[i]; |
| 92 | + const last = i === groups.length - 1; |
| 93 | + /** @type {Module | null} */ |
| 94 | + let target = null; |
| 95 | + for (const dependency of dependencies) { |
| 96 | + target = moduleGraph.getModule(dependency); |
| 97 | + if (target !== null) break; |
| 98 | + } |
| 99 | + const ids = getForwardIds(dependencies); |
| 100 | + let line = `${prefix}${last ? "└─" : "├─"}${ids ? ` (${ids})` : ""} `; |
| 101 | + if (target === null) { |
| 102 | + line += `${request} [raw]`; |
| 103 | + if (dependencies.every((dependency) => dependency.isLazy())) { |
| 104 | + line += " [lazy]"; |
| 105 | + } |
| 106 | + lines.push(line); |
| 107 | + continue; |
| 108 | + } |
| 109 | + line += printModule(target); |
| 110 | + if (seen.has(target)) { |
| 111 | + lines.push(`${line} (deduped)`); |
| 112 | + continue; |
| 113 | + } |
| 114 | + seen.add(target); |
| 115 | + lines.push(line); |
| 116 | + walk(target, `${prefix}${last ? " " : "│ "}`); |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + for (const { dependencies } of compilation.entries.values()) { |
| 121 | + for (const dependency of dependencies) { |
| 122 | + const module = moduleGraph.getModule(dependency); |
| 123 | + if (module === null || seen.has(module)) continue; |
| 124 | + seen.add(module); |
| 125 | + lines.push(printModule(module)); |
| 126 | + walk(module, ""); |
| 127 | + } |
| 128 | + } |
| 129 | + return lines.join("\n"); |
| 130 | +}; |
| 131 | + |
| 132 | +/** |
| 133 | + * Creates the plugin asserting `lazyModules` stay unbuilt and snapshotting the module graph. |
| 134 | + * @param {string[]} lazyModules files, relative to the compiler context, that must never be built |
| 135 | + * @param {string=} hint snapshot hint, to tell configurations of one case apart |
| 136 | + * @returns {(compiler: Compiler) => void} plugin |
| 137 | + */ |
| 138 | +const createLazyBarrelPlugin = (lazyModules, hint) => (compiler) => { |
| 139 | + const lazyResources = lazyModules.map((file) => |
| 140 | + path.resolve(compiler.context, file) |
| 141 | + ); |
| 142 | + /** @type {Set<string>} */ |
| 143 | + const built = new Set(); |
| 144 | + compiler.hooks.thisCompilation.tap("createLazyBarrelTest", (compilation) => { |
| 145 | + compilation.hooks.buildModule.tap("createLazyBarrelTest", (module) => { |
| 146 | + built.add(/** @type {NormalModule} */ (module).resource); |
| 147 | + }); |
| 148 | + // before seal: concatenation re-points connections |
| 149 | + compilation.hooks.finishModules.tap("createLazyBarrelTest", () => { |
| 150 | + const graph = printLazyBarrelGraph(compilation); |
| 151 | + if (hint === undefined) { |
| 152 | + expect(graph).toMatchSnapshot(); |
| 153 | + } else { |
| 154 | + expect(graph).toMatchSnapshot(hint); |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | + compiler.hooks.done.tap("createLazyBarrelTest", () => { |
| 159 | + for (const resource of lazyResources) { |
| 160 | + expect(built.has(resource)).toBe(false); |
| 161 | + } |
| 162 | + }); |
| 163 | +}; |
| 164 | + |
| 165 | +/** |
| 166 | + * Applies the lazy barrel test plugin to a configuration. |
| 167 | + * @param {Configuration} options the configuration |
| 168 | + * @param {string[]} lazyModules files, relative to the case, that must never be built |
| 169 | + * @param {string=} hint snapshot hint |
| 170 | + * @returns {Configuration} the configuration with the plugin applied |
| 171 | + */ |
| 172 | +const applyLazyBarrelPlugin = (options, lazyModules, hint) => ({ |
| 173 | + ...options, |
| 174 | + plugins: [ |
| 175 | + ...(options.plugins || []), |
| 176 | + createLazyBarrelPlugin(lazyModules, hint) |
| 177 | + ] |
| 178 | +}); |
| 179 | + |
| 180 | +/** |
| 181 | + * Creates the lazy barrel test configuration; an array builds each configuration, its snapshot named by `name` or index. |
| 182 | + * @template {Configuration | Configuration[]} T |
| 183 | + * @param {string[]} lazyModules files, relative to the case, that must never be built in any configuration |
| 184 | + * @param {T=} options extra configuration, or one per build |
| 185 | + * @returns {T} the configuration(s) |
| 186 | + */ |
| 187 | +const createLazyBarrelTest = (lazyModules, options) => |
| 188 | + /** @type {T} */ ( |
| 189 | + Array.isArray(options) |
| 190 | + ? options.map((config, index) => |
| 191 | + applyLazyBarrelPlugin( |
| 192 | + config, |
| 193 | + lazyModules, |
| 194 | + config.name === undefined ? String(index) : config.name |
| 195 | + ) |
| 196 | + ) |
| 197 | + : applyLazyBarrelPlugin(options || {}, lazyModules) |
| 198 | + ); |
| 199 | + |
| 200 | +module.exports = createLazyBarrelTest; |
| 201 | +module.exports.printLazyBarrelGraph = printLazyBarrelGraph; |
0 commit comments