* Helper method to collect all referenced modules recursively. * @param {Compilation} compilation The webpack compilation instance. * @param {Module} module The module to start collecting from. * @param {string} type The type of modules to collect ("initial", "external", or "all"). * @param {boo
(compilation, module, type, includeInitial)
| 201 | * @returns {Set<Module>} Set of collected modules. |
| 202 | */ |
| 203 | function getAllReferencedModules(compilation, module, type, includeInitial) { |
| 204 | const collectedModules = new Set(includeInitial ? [module] : []); |
| 205 | /** @type {WeakSet<Module>} */ |
| 206 | const visitedModules = new WeakSet([module]); |
| 207 | /** @type {Module[]} */ |
| 208 | const stack = [module]; |
| 209 | |
| 210 | while (stack.length > 0) { |
| 211 | const currentModule = stack.pop(); |
| 212 | if (!currentModule) continue; |
| 213 | |
| 214 | const outgoingConnections = |
| 215 | compilation.moduleGraph.getOutgoingConnections(currentModule); |
| 216 | if (outgoingConnections) { |
| 217 | for (const connection of outgoingConnections) { |
| 218 | const connectedModule = connection.module; |
| 219 | |
| 220 | // Skip if module has already been visited |
| 221 | if (!connectedModule || visitedModules.has(connectedModule)) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | // Handle 'initial' type (skipping async blocks) |
| 226 | if (type === "initial") { |
| 227 | const parentBlock = compilation.moduleGraph.getParentBlock( |
| 228 | /** @type {Dependency} */ |
| 229 | (connection.dependency) |
| 230 | ); |
| 231 | if (parentBlock instanceof AsyncDependenciesBlock) { |
| 232 | continue; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Handle 'external' type (collecting only external modules) |
| 237 | if (type === "external") { |
| 238 | if (connection.module instanceof ExternalModule) { |
| 239 | collectedModules.add(connectedModule); |
| 240 | } |
| 241 | } else { |
| 242 | // Handle 'all' or unspecified types |
| 243 | collectedModules.add(connectedModule); |
| 244 | } |
| 245 | |
| 246 | // Add connected module to the stack and mark it as visited |
| 247 | visitedModules.add(connectedModule); |
| 248 | stack.push(connectedModule); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return collectedModules; |
| 254 | } |
| 255 | |
| 256 | module.exports = HoistContainerReferences; |
no test coverage detected