diff --git a/packages/metro/src/DeltaBundler/Graph.js b/packages/metro/src/DeltaBundler/Graph.js index a01e335da5..eee18685e2 100644 --- a/packages/metro/src/DeltaBundler/Graph.js +++ b/packages/metro/src/DeltaBundler/Graph.js @@ -171,44 +171,21 @@ export class Graph { ): Promise> { const internalOptions = getInternalOptions(options); - const modifiedPathsInBaseGraph = paths.filter(path => - this.dependencies.has(path), + const modifiedPathsInBaseGraph = new Set( + paths.filter(path => this.dependencies.has(path)), ); const allModifiedPaths = new Set(paths); - const moduleData = await buildSubgraph( - new Set(modifiedPathsInBaseGraph), - this.#resolvedContexts, - { - resolve: options.resolve, - transform: async (absolutePath, requireContext) => { - internalOptions.onDependencyAdd(); - const result = await options.transform(absolutePath, requireContext); - internalOptions.onDependencyAdded(); - return result; - }, - shouldTraverse: (dependency: Dependency) => { - if ( - // Don't traverse into any path that hasn't been modified and as is - // already in the graph. - this.dependencies.has(dependency.absolutePath) && - !allModifiedPaths.has(dependency.absolutePath) - ) { - return false; - } - return !options.shallow && !isWeakOrLazy(dependency, internalOptions); - }, - }, + const delta = await this._buildDelta( + modifiedPathsInBaseGraph, + internalOptions, + // Traverse new or modified paths + absolutePath => + !this.dependencies.has(absolutePath) || + allModifiedPaths.has(absolutePath), ); - const delta = { - added: new Set(), - touched: new Set(), - deleted: new Set(), - moduleData, - }; - for (const modified of modifiedPathsInBaseGraph) { if (this.dependencies.has(modified)) { this._recursivelyCommitModule(modified, delta, internalOptions); @@ -257,28 +234,11 @@ export class Graph { this.#gc.color.set(path, 'black'); } - const moduleData = await buildSubgraph(this.entryPoints, new Map(), { - resolve: options.resolve, - transform: async (absolutePath, requireContext) => { - internalOptions.onDependencyAdd(); - const result = await options.transform(absolutePath, requireContext); - internalOptions.onDependencyAdded(); - return result; - }, - shouldTraverse: (dependency: Dependency) => - !options.shallow && !isWeakOrLazy(dependency, internalOptions), - }); - - const delta = { - added: new Set(), - touched: new Set(), - deleted: new Set(), - moduleData, - }; + const delta = await this._buildDelta(this.entryPoints, internalOptions); - [...this.entryPoints].forEach((path: string) => - this._recursivelyCommitModule(path, delta, internalOptions), - ); + for (const path of this.entryPoints) { + this._recursivelyCommitModule(path, delta, internalOptions); + } this.reorderGraph({ shallow: options.shallow, @@ -291,6 +251,39 @@ export class Graph { }; } + async _buildDelta( + pathsToVisit: $ReadOnlySet, + options: InternalOptions, + moduleFilter?: (path: string) => boolean, + ): Promise> { + const moduleData = await buildSubgraph( + pathsToVisit, + this.#resolvedContexts, + { + resolve: options.resolve, + transform: async (absolutePath, requireContext) => { + options.onDependencyAdd(); + const result = await options.transform(absolutePath, requireContext); + options.onDependencyAdded(); + return result; + }, + shouldTraverse: (dependency: Dependency) => { + if (options.shallow || isWeakOrLazy(dependency, options)) { + return false; + } + return moduleFilter == null || moduleFilter(dependency.absolutePath); + }, + }, + ); + + return { + added: new Set(), + touched: new Set(), + deleted: new Set(), + moduleData, + }; + } + _recursivelyCommitModule( path: string, delta: Delta,