Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions packages/metro-file-map/src/lib/FileProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;
#maxFilesPerWorker: number;
#maxWorkers: number;
#perfLogger: ?PerfLogger;
Expand All @@ -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),
});
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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<number>) ?? [];
const pluginsToRun = this.#eagerPluginIdxs.filter(idx =>
this.#pluginWorkers[idx].filter({isNodeModules, normalPath}),
);

if (!computeSha1 && pluginsToRun.length === 0) {
// Nothing to process
Expand Down
36 changes: 36 additions & 0 deletions packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,42 @@ describe('processBatch', () => {
expect(mockWorkerFn).not.toHaveBeenCalled();
});

test.each([
['no plugins', [] as ReadonlyArray<FileMapPluginWorker>],
[
'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<string | symbol> = [];
const fileMetadata = new Proxy<FileMetadata>(
[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]],
Expand Down
Loading