Skip to content

Commit 08303d7

Browse files
authored
metro-file-map: Let plugins report what changed in a batch, on the change event
## Context A file map plugin often knows things about a batch of changes that nothing else can work out. For Haste, #1952 records the names a resolution looked up, and invalidating from those means knowing which names a change rebound. The `ChangeEvent` carries paths, and by the time a consumer sees it a removed file's name is already gone from the map, but `HastePlugin.onChanged` has it to hand: https://github.com/react/metro/blob/6dbe97956af0f66f7a1d88014ba0b784a4a4d893/packages/metro-file-map/src/plugins/HastePlugin.js#L241-L245 ## This change This lets a plugin's `onChanged` return a summary, published on `ChangeEvent.pluginChanges` by plugin name. The summary type defaults to `void`, so existing plugins are unaffected. `HastePlugin` reports `changedNames`. Plugin names now have to be unique across all plugins, not just those with a worker. Persisted state was already keyed by name, so two sharing a name were already overwriting each other. Not addressed here: on a cold start `HastePlugin` builds a summary nobody reads (transient, and nothing where Haste is inert), and an edit that changes a file's Haste name is neither applied to the map nor reported, which predates this. Invalidating from the summary is the next PR. Changelog: Internal ## Test plan New unit tests cover the watch and recrawl paths and duplicate plugin names.
1 parent b51ffd3 commit 08303d7

7 files changed

Lines changed: 211 additions & 43 deletions

File tree

‎packages/metro-file-map/API.md‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export type CacheManagerWriteOptions = Readonly<{
4747
export type ChangeEvent = Readonly<{
4848
logger?: null | undefined | RootPerfLogger;
4949
changes: ReadonlyFileSystemChanges<Readonly<ChangedFileMetadata>>;
50+
pluginChanges: ReadonlyMap<string, unknown>;
5051
rootDir: string;
5152
}>;
5253

@@ -209,6 +210,25 @@ interface FileSystem_2 {
209210
}
210211
export {FileSystem_2 as FileSystem};
211212

213+
export function getPluginChanges<ChangeSummary>(
214+
event: ChangeEvent,
215+
plugin: FileMapPlugin<
216+
/**
217+
* > | plugin: FileMapPlugin<empty, empty, ChangeSummary>,
218+
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
219+
**/
220+
any,
221+
/**
222+
* > | plugin: FileMapPlugin<empty, empty, ChangeSummary>,
223+
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
224+
**/
225+
any,
226+
ChangeSummary
227+
>,
228+
): ChangeSummary | void;
229+
230+
export type HasteChanges = Readonly<{changedNames: ReadonlySet<string>}>;
231+
212232
export class HasteConflictsError extends Error {
213233
constructor(conflicts: ReadonlyArray<HasteConflict>);
214234
getDetailedMessage(pathsRelativeToRoot: null | undefined | string): string;
@@ -227,7 +247,7 @@ export type HasteMapItem = {
227247
[platform: string]: HasteMapItemMetadata;
228248
};
229249

230-
export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null> {
250+
export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null, HasteChanges | void> {
231251
constructor(options: HasteMapOptions);
232252
assertValid(): void;
233253
computeConflicts(): Array<HasteConflict>;
@@ -239,7 +259,7 @@ export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null>
239259
getWorker(): FileMapPluginWorker;
240260
initialize(opts: FileMapPluginInitOptions<null, string | null>): Promise<void>;
241261
readonly name: 'haste';
242-
onChanged(delta: ReadonlyFileSystemChanges<null | undefined | string>): void;
262+
onChanged(delta: ReadonlyFileSystemChanges<null | undefined | string>): HasteChanges | void;
243263
setModule(id: string, module: HasteMapItemMetadata): void;
244264
}
245265

@@ -265,15 +285,16 @@ export type HealthCheckResult =
265285

266286
export type InputFileMapPlugin = FileMapPlugin<
267287
/**
268-
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
288+
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
269289
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
270290
**/
271291
any,
272292
/**
273-
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
293+
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
274294
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
275295
**/
276-
any
296+
any,
297+
unknown
277298
>;
278299

279300
export type InputOptions = Readonly<{

‎packages/metro-file-map/src/__tests__/index-test.js‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
} from '../flow-types';
2828
import type {DependencyPlugin} from '../index';
2929
import type {default as FileMapT} from '../index';
30-
import type {HasteMapOptions} from '../plugins/HastePlugin';
30+
import type HastePluginT, {HasteMapOptions} from '../plugins/HastePlugin';
3131
import type {MockMapOptions} from '../plugins/MockPlugin';
3232
import type {WatcherOptions} from '../watchers/common';
3333
import typeof WorkerModule from '../worker';
@@ -312,7 +312,7 @@ let buildNewFileMap: (
312312
) => Promise<{
313313
...BuildResult,
314314
fileMap: FileMapT,
315-
hasteMap: HasteMap,
315+
hasteMap: HastePluginT,
316316
mockMap: ?MockMap,
317317
dependencyPlugin: ?DependencyPlugin,
318318
}>;
@@ -514,6 +514,24 @@ describe('FileMap', () => {
514514
).toMatchObject({exists: true, type: 'f', data: null});
515515
});
516516

517+
test('throws on plugins that share a name, with or without a worker', () => {
518+
const MockPlugin = require('../plugins/MockPlugin').default;
519+
const mockConfig = {
520+
console: globalThis.console,
521+
rootDir: defaultConfig.rootDir,
522+
mocksPattern: /__mocks__/,
523+
throwOnModuleCollision: false,
524+
};
525+
expect(
526+
() =>
527+
new FileMap({
528+
...defaultConfig,
529+
// MockPlugin has no worker, so holds no plugin data slot
530+
plugins: [new MockPlugin(mockConfig), new MockPlugin(mockConfig)],
531+
}),
532+
).toThrow('metro-file-map: Duplicate plugin name: mocks');
533+
});
534+
517535
test('ignores files given a pattern', async () => {
518536
mockFs[p('/project/fruits/Kiwi.js')] = `
519537
// Kiwi!
@@ -1758,6 +1776,28 @@ describe('FileMap', () => {
17581776
}
17591777
});
17601778

1779+
fm_it(
1780+
'publishes what each plugin reports about a batch on the change event',
1781+
async ({fileMap, hasteMap}) => {
1782+
const getPluginChanges = require('../lib/getPluginChanges').default;
1783+
const fruitsRoot = path.join('/', 'project', 'fruits');
1784+
mockFs[path.join(fruitsRoot, 'Tomato.js')] = '// Tomato!';
1785+
mockEmitters[fruitsRoot].emitFileEvent({
1786+
event: 'touch',
1787+
relativePath: 'Tomato.js',
1788+
metadata: MOCK_CHANGE_FILE,
1789+
});
1790+
mockDeleteFile(fruitsRoot, 'Banana.js');
1791+
const changeEvent = await waitForItToChange(fileMap);
1792+
1793+
expect(getPluginChanges(changeEvent, hasteMap)).toEqual({
1794+
changedNames: new Set(['Tomato', 'Banana']),
1795+
});
1796+
// Only plugins with something to say are present
1797+
expect([...changeEvent.pluginChanges.keys()]).toEqual([hasteMap.name]);
1798+
},
1799+
);
1800+
17611801
function mockDeleteFile(root: string, relativePath: string) {
17621802
const e = mockEmitters[root];
17631803
e.emitFileEvent({event: 'delete', relativePath});
@@ -1772,7 +1812,7 @@ describe('FileMap', () => {
17721812

17731813
function fm_it(
17741814
title: string,
1775-
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HasteMap}>) => unknown,
1815+
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HastePluginT}>) => unknown,
17761816
options?: FileMapTestOptions = {},
17771817
): void {
17781818
options = options || {};
@@ -1795,7 +1835,7 @@ describe('FileMap', () => {
17951835

17961836
fm_it.only = (
17971837
title: string,
1798-
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HasteMap}>) => unknown,
1838+
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HastePluginT}>) => unknown,
17991839
options?: FileMapTestOptions,
18001840
): void => fm_it(title, fn, {...options, only: true});
18011841

@@ -2635,7 +2675,12 @@ describe('FileMap', () => {
26352675
relativePath: 'tropical',
26362676
});
26372677

2638-
await waitForItToChange(fileMap);
2678+
const changeEvent = await waitForItToChange(fileMap);
2679+
2680+
// What plugins report during a recrawl still reaches the event
2681+
expect(
2682+
require('../lib/getPluginChanges').default(changeEvent, hasteMap),
2683+
).toEqual({changedNames: new Set(['Mango', 'Papaya'])});
26392684

26402685
// Verify crawl was called with the correct directory
26412686
expect(mockNodeCrawler).toHaveBeenNthCalledWith(

‎packages/metro-file-map/src/flow-types.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ export type ChangedFileMetadata = Readonly<{
113113
export type ChangeEvent = Readonly<{
114114
logger?: ?RootPerfLogger,
115115
changes: ReadonlyFileSystemChanges<Readonly<ChangedFileMetadata>>,
116+
/**
117+
* What each plugin reported about this batch, by name, if anything. Read
118+
* entries with `getPluginChanges`.
119+
*/
120+
pluginChanges: ReadonlyMap<string, unknown>,
116121
rootDir: string,
117122
}>;
118123

@@ -302,19 +307,25 @@ export type V8Serializable = V8SerializablePrimitive | V8SerializableCollection;
302307
export interface FileMapPlugin<
303308
in SerializableState extends void | V8Serializable = void | V8Serializable,
304309
in PerFileData extends void | V8Serializable = void | V8Serializable,
310+
out ChangeSummary = void,
305311
> {
312+
// Must be unique among the plugins of a file map.
306313
readonly name: string;
307314
initialize(
308315
initOptions: FileMapPluginInitOptions<SerializableState, PerFileData>,
309316
): Promise<void>;
310317
assertValid(): void;
311-
onChanged(changes: ReadonlyFileSystemChanges<?PerFileData>): void;
318+
/**
319+
* Update the plugin's state for a batch of changes, synchronously before it
320+
* is emitted. May return a summary for `ChangeEvent.pluginChanges`.
321+
*/
322+
onChanged(changes: ReadonlyFileSystemChanges<?PerFileData>): ChangeSummary;
312323
getSerializableSnapshot(): void | V8Serializable;
313324
getCacheKey(): string;
314325
getWorker(): ?FileMapPluginWorker;
315326
}
316327

317-
export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
328+
export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
318329

319330
export interface MetadataWorker {
320331
processFile(

‎packages/metro-file-map/src/index.js‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type InternalOptions = Readonly<{
123123

124124
type IndexedPlugin = Readonly<{
125125
// $FlowFixMe[unclear-type] Plugin types cannot be known statically
126-
plugin: FileMapPlugin<any, any>,
126+
plugin: FileMapPlugin<any, any, unknown>,
127127
dataIdx: ?number,
128128
}>;
129129
type InternalEnqueuedEvent = Readonly<
@@ -148,7 +148,9 @@ export type {FileDataPluginOptions} from './plugins/FileDataPlugin';
148148
export type {DependencyPluginOptions} from './plugins/DependencyPlugin';
149149
export {DuplicateHasteCandidatesError} from './plugins/haste/DuplicateHasteCandidatesError';
150150
export {HasteConflictsError} from './plugins/haste/HasteConflictsError';
151+
export {default as getPluginChanges} from './lib/getPluginChanges';
151152
export {default as HastePlugin} from './plugins/HastePlugin';
153+
export type {HasteChanges} from './plugins/HastePlugin';
152154

153155
export type {HasteMap} from './flow-types';
154156
export type {HealthCheckResult} from './Watcher';
@@ -331,19 +333,19 @@ export default class FileMap extends EventEmitter {
331333
const pluginWorkers: Array<FileMapPluginWorker> = [];
332334
const pluginDataIndices = new Map<string, number>();
333335
const plugins = options.plugins ?? [];
336+
const pluginNames = new Set<string>();
334337
for (const plugin of plugins) {
338+
// Slots, persisted state and change summaries are all keyed by name
339+
invariant(
340+
!pluginNames.has(plugin.name),
341+
'metro-file-map: Duplicate plugin name: %s',
342+
plugin.name,
343+
);
344+
pluginNames.add(plugin.name);
335345
const maybeWorker = plugin.getWorker();
336346
const dataIdx = maybeWorker != null ? dataSlot++ : null;
337347
indexedPlugins.push({plugin, dataIdx});
338348
if (dataIdx != null) {
339-
// Crawlers address plugin data by name, so names must be unique among
340-
// plugins holding a slot - otherwise a crawler would silently write to
341-
// the shadowed plugin's slot.
342-
invariant(
343-
!pluginDataIndices.has(plugin.name),
344-
'metro-file-map: Duplicate plugin name: %s',
345-
plugin.name,
346-
);
347349
pluginDataIndices.set(plugin.name, dataIdx);
348350
}
349351
if (maybeWorker != null) {
@@ -557,7 +559,8 @@ export default class FileMap extends EventEmitter {
557559
}
558560

559561
// Update `fileSystem` and plugins based on the file delta.
560-
const actualChanges = await this.#applyFileDelta(
562+
// The initial build emits nothing, so plugin summaries are unused
563+
const {changeAggregator: actualChanges} = await this.#applyFileDelta(
561564
fileSystem,
562565
plugins,
563566
fileDelta,
@@ -685,7 +688,7 @@ export default class FileMap extends EventEmitter {
685688
// end() may have been called during the crawl or while applying it.
686689
const abortSignal = this.#crawlerAbortController.signal;
687690
abortSignal.throwIfAborted();
688-
const changeAggregator = await this.#applyFileDelta(
691+
const {changeAggregator, pluginChanges} = await this.#applyFileDelta(
689692
fileSystem,
690693
this.#plugins,
691694
crawlResult,
@@ -697,6 +700,7 @@ export default class FileMap extends EventEmitter {
697700
const changeEvent: ChangeEvent = {
698701
changes: changeAggregator.getMappedView(toPublicMetadata),
699702
logger: null,
703+
pluginChanges,
700704
rootDir: this.#options.rootDir,
701705
};
702706
this.emit('change', changeEvent);
@@ -797,6 +801,24 @@ export default class FileMap extends EventEmitter {
797801
return null;
798802
}
799803

804+
/** Update every plugin with a batch and collect their summaries. */
805+
#updatePlugins(
806+
changeAggregator: FileSystemChangeAggregator,
807+
): Map<string, unknown> {
808+
const pluginChanges = new Map<string, unknown>();
809+
this.#plugins.forEach(({plugin, dataIdx}) => {
810+
const summary = plugin.onChanged(
811+
changeAggregator.getMappedView(
812+
dataIdx != null ? metadata => metadata[dataIdx] : () => null,
813+
),
814+
);
815+
if (summary != null) {
816+
pluginChanges.set(plugin.name, summary);
817+
}
818+
});
819+
return pluginChanges;
820+
}
821+
800822
async #applyFileDelta(
801823
fileSystem: MutableFileSystem,
802824
plugins: ReadonlyArray<IndexedPlugin>,
@@ -805,7 +827,10 @@ export default class FileMap extends EventEmitter {
805827
removedFiles: ReadonlySet<CanonicalPath>,
806828
clocks?: WatchmanClocks,
807829
}>,
808-
): Promise<FileSystemChangeAggregator> {
830+
): Promise<{
831+
changeAggregator: FileSystemChangeAggregator,
832+
pluginChanges: ReadonlyMap<string, unknown>,
833+
}> {
809834
this.#startupPerfLogger?.point('applyFileDelta_start');
810835
const {changedFiles, removedFiles} = delta;
811836
this.#startupPerfLogger?.point('applyFileDelta_preprocess_start');
@@ -894,17 +919,11 @@ export default class FileMap extends EventEmitter {
894919
this.#startupPerfLogger?.point('applyFileDelta_add_end');
895920

896921
this.#startupPerfLogger?.point('applyFileDelta_updatePlugins_start');
897-
this.#plugins.forEach(({plugin, dataIdx}) => {
898-
plugin.onChanged(
899-
changeAggregator.getMappedView(
900-
dataIdx != null ? metadata => metadata[dataIdx] : () => null,
901-
),
902-
);
903-
});
922+
const pluginChanges = this.#updatePlugins(changeAggregator);
904923
this.#startupPerfLogger?.point('applyFileDelta_updatePlugins_end');
905924
this.#startupPerfLogger?.point('applyFileDelta_end');
906925

907-
return changeAggregator;
926+
return {changeAggregator, pluginChanges};
908927
}
909928

910929
/**
@@ -1012,13 +1031,7 @@ export default class FileMap extends EventEmitter {
10121031
}
10131032

10141033
const _netChange = changeAggregator.getView();
1015-
this.#plugins.forEach(({plugin, dataIdx}) => {
1016-
plugin.onChanged(
1017-
changeAggregator.getMappedView(
1018-
dataIdx != null ? metadata => metadata[dataIdx] : () => null,
1019-
),
1020-
);
1021-
});
1034+
const pluginChanges = this.#updatePlugins(changeAggregator);
10221035

10231036
const changesWithMetadata =
10241037
changeAggregator.getMappedView(toPublicMetadata);
@@ -1038,6 +1051,7 @@ export default class FileMap extends EventEmitter {
10381051
const changeEvent: ChangeEvent = {
10391052
changes: changesWithMetadata,
10401053
logger: hmrPerfLogger,
1054+
pluginChanges,
10411055
rootDir: this.#options.rootDir,
10421056
};
10431057
this.emit('change', changeEvent);
@@ -1182,7 +1196,10 @@ export default class FileMap extends EventEmitter {
11821196
}
11831197

11841198
// Reuse the same batch processing logic as build()
1185-
const recrawlChangeAggregator = await this.#applyFileDelta(
1199+
const {
1200+
changeAggregator: recrawlChangeAggregator,
1201+
pluginChanges: recrawlPluginChanges,
1202+
} = await this.#applyFileDelta(
11861203
fileSystem,
11871204
this.#plugins,
11881205
crawlResult,
@@ -1203,6 +1220,7 @@ export default class FileMap extends EventEmitter {
12031220
const changeEvent: ChangeEvent = {
12041221
changes: changesWithMetadata,
12051222
logger: null,
1223+
pluginChanges: recrawlPluginChanges,
12061224
rootDir: this.#options.rootDir,
12071225
};
12081226
this.emit('change', changeEvent);

0 commit comments

Comments
 (0)