diff --git a/packages/metro-file-map/src/lib/FileProcessor.js b/packages/metro-file-map/src/lib/FileProcessor.js index 72bc41346f..33d6d8c9f3 100644 --- a/packages/metro-file-map/src/lib/FileProcessor.js +++ b/packages/metro-file-map/src/lib/FileProcessor.js @@ -53,6 +53,8 @@ const NODE_MODULES_SEP = 'node_modules' + sep; const MAX_FILES_PER_WORKER = 100; export class FileProcessor { + // Indices of the plugins whose workers run on a batch, rather than on demand. + #eagerPluginIdxs: ReadonlyArray; #maxFilesPerWorker: number; #maxWorkers: number; #perfLogger: ?PerfLogger; @@ -72,6 +74,9 @@ export class FileProcessor { this.#maxFilesPerWorker = opts.maxFilesPerWorker ?? MAX_FILES_PER_WORKER; this.#maxWorkers = opts.maxWorkers; this.#pluginWorkers = opts.pluginWorkers ?? []; + this.#eagerPluginIdxs = this.#pluginWorkers.flatMap((plugin, idx) => + plugin.lazy === true ? [] : [idx], + ); this.#inBandWorker = new Worker({ plugins: this.#pluginWorkers.map(plugin => plugin.worker), }); @@ -90,6 +95,17 @@ export class FileProcessor { }> { const errors = []; + if (req.maybeReturnContent) { + throw new Error( + 'Batch processing does not support returning file contents', + ); + } + + if (!req.computeSha1 && this.#eagerPluginIdxs.length === 0) { + // No file needs a worker. + return {errors}; + } + const workerJobs = files .map(([normalFilePath, fileMetadata]) => { const maybeWorkerInput = this.#getWorkerInput( @@ -110,12 +126,6 @@ export class FileProcessor { ); const batchWorker = this.#getBatchWorker(numWorkers); - if (req.maybeReturnContent) { - throw new Error( - 'Batch processing does not support returning file contents', - ); - } - await Promise.all( workerJobs.map(([workerInput, fileMetadata]) => { return batchWorker @@ -200,16 +210,9 @@ export class FileProcessor { (nodeModulesIdx > 0 && normalPath[nodeModulesIdx - 1] === sep); // Indices of plugins with a passing filter - const pluginsToRun = - this.#pluginWorkers?.reduce((prev, plugin, idx) => { - if ( - plugin.lazy !== true && - plugin.filter({isNodeModules, normalPath}) - ) { - prev.push(idx); - } - return prev; - }, [] as Array) ?? []; + const pluginsToRun = this.#eagerPluginIdxs.filter(idx => + this.#pluginWorkers[idx].filter({isNodeModules, normalPath}), + ); if (!computeSha1 && pluginsToRun.length === 0) { // Nothing to process diff --git a/packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js b/packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js index 2dfbd426d3..b9b4230680 100644 --- a/packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js +++ b/packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js @@ -276,6 +276,42 @@ describe('processBatch', () => { expect(mockWorkerFn).not.toHaveBeenCalled(); }); + test.each([ + ['no plugins', [] as ReadonlyArray], + [ + 'only lazy plugins', + [ + { + worker: {modulePath: 'mock-lazy-plugin', setupArgs: {}}, + filter: () => true, + lazy: true, + }, + ], + ], + ])( + 'skips the per-file pass without a SHA-1 to compute and with %s', + async (_, pluginWorkers) => { + const readKeys: Array = []; + const fileMetadata = new Proxy( + [123, 234, 0, null, 0, null], + { + get(target, key, receiver) { + readKeys.push(key); + return Reflect.get(target, key, receiver); + }, + }, + ); + const processor = new FileProcessor({...defaultOptions, pluginWorkers}); + + await processor.processBatch([[p('src/index.js'), fileMetadata]], { + computeSha1: false, + maybeReturnContent: false, + }); + + expect(readKeys).toEqual([]); + }, + ); + describe('node_modules files', () => { const nodeModulesFiles = (): Array<[string, FileMetadata]> => [ [p('node_modules/pkg/index.js'), [123, 234, 0, null, 0, null]],