Skip to content

Commit

Permalink
Propose static WeakSet/WeakMap implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Sep 16, 2024
1 parent 5654813 commit b19c3e0
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ class DependencyExtractionWebpackPlugin {
}
}

static #staticDepsCurrent = new WeakSet();
static #staticDepsCache = new WeakMap();

/**
* Can we trace a line of static dependencies from an entry to a module
*
Expand All @@ -358,6 +361,20 @@ class DependencyExtractionWebpackPlugin {
* @return {boolean} True if there is a static import path to the root
*/
static hasStaticDependencyPathToRoot( compilation, block ) {
if ( DependencyExtractionWebpackPlugin.#staticDepsCache.has( block ) ) {
return DependencyExtractionWebpackPlugin.#staticDepsCache.get(
block
);
}

if (
DependencyExtractionWebpackPlugin.#staticDepsCurrent.has( block )
) {
return false;
}

DependencyExtractionWebpackPlugin.#staticDepsCurrent.add( block );

const incomingConnections = [
...compilation.moduleGraph.getIncomingConnections( block ),
].filter(
Expand All @@ -371,6 +388,13 @@ class DependencyExtractionWebpackPlugin {
// If we don't have non-entry, non-library incoming connections,
// we've reached a root of
if ( ! incomingConnections.length ) {
DependencyExtractionWebpackPlugin.#staticDepsCache.set(
block,
true
);
DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete(
block
);
return true;
}

Expand All @@ -389,16 +413,28 @@ class DependencyExtractionWebpackPlugin {

// All the dependencies were Async, the module was reached via a dynamic import
if ( ! staticDependentModules.length ) {
DependencyExtractionWebpackPlugin.#staticDepsCache.set(
block,
false
);
DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete(
block
);
return false;
}

// Continue to explore any static dependencies
return staticDependentModules.some( ( parentStaticDependentModule ) =>
DependencyExtractionWebpackPlugin.hasStaticDependencyPathToRoot(
compilation,
parentStaticDependentModule
)
const result = staticDependentModules.some(
( parentStaticDependentModule ) =>
DependencyExtractionWebpackPlugin.hasStaticDependencyPathToRoot(
compilation,
parentStaticDependentModule
)
);

DependencyExtractionWebpackPlugin.#staticDepsCache.set( block, result );
DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete( block );
return result;
}
}

Expand Down

0 comments on commit b19c3e0

Please sign in to comment.