| 243 | * @returns {[UsedModuleIds, Module[]]} used module ids as strings and modules without id matching the filter |
| 244 | */ |
| 245 | const getUsedModuleIdsAndModules = (compilation, filter) => { |
| 246 | const chunkGraph = compilation.chunkGraph; |
| 247 | /** @type {Module[]} */ |
| 248 | const modules = []; |
| 249 | |
| 250 | /** @type {UsedModuleIds} */ |
| 251 | const usedIds = new Set(); |
| 252 | if (compilation.usedModuleIds) { |
| 253 | for (const id of compilation.usedModuleIds) { |
| 254 | usedIds.add(String(id)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | for (const module of compilation.modules) { |
| 259 | if (!module.needId) continue; |
| 260 | const moduleId = chunkGraph.getModuleId(module); |
| 261 | if (moduleId !== null) { |
| 262 | usedIds.add(String(moduleId)); |
| 263 | } else if ( |
| 264 | (!filter || filter(module)) && |
| 265 | (chunkGraph.getNumberOfModuleChunks(module) !== 0 || |
| 266 | // CSS modules need IDs even when not in chunks, for generating CSS class names(i.e. [id]-[local]) |
| 267 | /** @type {CssModuleBuildMeta} */ (module.buildMeta).isCssModule || |
| 268 | /** @type {CssModuleBuildMeta} */ (module.buildMeta) |
| 269 | .needIdInConcatenation) |
| 270 | ) { |
| 271 | modules.push(module); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return [usedIds, modules]; |
| 276 | }; |
| 277 | |
| 278 | /** @typedef {Set<string>} UsedChunkIds */ |
| 279 | |