Skip to content

Commit e3fd11a

Browse files
committed
metro-file-map: Let plugins report what changed in a batch, on the change event
Stacked on #1952. ## Summary A file map plugin often knows things about a batch of changes that nothing else can work out. The motivating case is Haste - #1952 records the Haste names a resolution looks up, and to invalidate from those a consumer needs to know which names were rebound by a change. It can't derive that from the `ChangeEvent`, which carries paths and modified times, and by the time it sees the event a removed file's name is already gone from the map. `HastePlugin` has it to hand - its `onChanged` is given each removed file's old name and each added file's new one. This gives plugins a way to say so. `FileMapPlugin.onChanged` may now return a summary, and the file map publishes what each plugin returned as `ChangeEvent.pluginChanges`, by plugin name. A plugin that returns nothing is absent, so existing plugins are unaffected - the summary is a third type parameter on `FileMapPlugin` that defaults to `void`. `getPluginChanges(event, plugin)` reads an entry with the plugin's own summary type, so a consumer never writes the name or casts. The summary describes exactly the batch in `changes`. Plugins are already updated synchronously, immediately before the emit, so there's no ordering for a consumer to get wrong - which is the problem with the alternative of asking the plugin afterwards. There are two emit sites: the batched watch path updates plugins inline, and a recrawl updates them inside `#applyFileDelta`, which now returns what they reported along with the changes. The initial build goes through `#applyFileDelta` too but emits nothing, so its summaries go unused. `HastePlugin` is the first user. It reports `changedNames`, the names bound or unbound for any platform by the files added and removed - a bare name, matching what #1952 records. Plugin names now have to be unique across all of a file map's plugins. That was only checked for plugins with a worker, but persisted plugin state is keyed by name for every plugin, so two that shared a name would already have overwritten each other's state. Two things worth knowing: - On a cold start `onChanged` sees every file as added, so `HastePlugin` builds a set of every name for a summary nobody reads. That's nothing where Haste is inert, which is Metro's default, and transient otherwise. If it matters, the alternative is an optional method the file map calls only for a batch it's about to emit. - A modified file reaches plugins in `modifiedFiles` with its new data only, and `HastePlugin.onChanged` doesn't read that list. So an edit that changes a file's Haste name (a docblock, or a global package's `name`) is neither applied to the map nor reported here. That predates this, and can't happen with a path-based `hasteImpl`. `FileSystemChangeAggregator` keeps each file's pre-batch metadata, so giving plugins the old value is feasible, separately. Nothing consumes this yet. ## Test plan ``` yarn jest packages/metro-file-map packages/metro-resolver packages/metro/src/node-haste packages/metro/src/DeltaBundler packages/metro/src/integration_tests packages/metro/src/__tests__ packages/metro/src/Server yarn flow check yarn typecheck-ts yarn verify-api-snapshots ``` - `HastePlugin-test.js`: `onChanged` reports the names bound and unbound, and nothing when no file with a name was added or removed. - `index-test.js`: in watch mode an added and a removed file put both names on the event, read back through `getPluginChanges`, and plugins with nothing to say are absent. A recrawl publishes what plugins reported too. Two plugins sharing a name throw, with or without a worker. Changelog: Internal
1 parent fe0a1bc commit e3fd11a

7 files changed

Lines changed: 231 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

@@ -202,6 +203,25 @@ interface FileSystem_2 {
202203
}
203204
export {FileSystem_2 as FileSystem};
204205

206+
export function getPluginChanges<ChangeSummary>(
207+
event: ChangeEvent,
208+
plugin: FileMapPlugin<
209+
/**
210+
* > | plugin: FileMapPlugin<empty, empty, ChangeSummary>,
211+
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
212+
**/
213+
any,
214+
/**
215+
* > | plugin: FileMapPlugin<empty, empty, ChangeSummary>,
216+
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
217+
**/
218+
any,
219+
ChangeSummary
220+
>,
221+
): ChangeSummary | void;
222+
223+
export type HasteChanges = Readonly<{changedNames: ReadonlySet<string>}>;
224+
205225
export class HasteConflictsError extends Error {
206226
constructor(conflicts: ReadonlyArray<HasteConflict>);
207227
getDetailedMessage(pathsRelativeToRoot: null | undefined | string): string;
@@ -220,7 +240,7 @@ export type HasteMapItem = {
220240
[platform: string]: HasteMapItemMetadata;
221241
};
222242

223-
export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null> {
243+
export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null, HasteChanges | void> {
224244
constructor(options: HasteMapOptions);
225245
assertValid(): void;
226246
computeConflicts(): Array<HasteConflict>;
@@ -232,7 +252,7 @@ export class HastePlugin implements HasteMap, FileMapPlugin<null, string | null>
232252
getWorker(): FileMapPluginWorker;
233253
initialize(opts: FileMapPluginInitOptions<null, string | null>): Promise<void>;
234254
readonly name: 'haste';
235-
onChanged(delta: ReadonlyFileSystemChanges<null | undefined | string>): void;
255+
onChanged(delta: ReadonlyFileSystemChanges<null | undefined | string>): HasteChanges | void;
236256
setModule(id: string, module: HasteMapItemMetadata): void;
237257
}
238258

@@ -258,15 +278,16 @@ export type HealthCheckResult =
258278

259279
export type InputFileMapPlugin = FileMapPlugin<
260280
/**
261-
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
281+
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
262282
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
263283
**/
264284
any,
265285
/**
266-
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
286+
* > | export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
267287
* | ^^^^^ Unsupported feature: Translating "empty type" is currently not supported.
268288
**/
269-
any
289+
any,
290+
unknown
270291
>;
271292

272293
export type InputOptions = Readonly<{

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

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type {
2626
} from '../flow-types';
2727
import type {DependencyPlugin} from '../index';
2828
import type {default as FileMapT} from '../index';
29-
import type {HasteMapOptions} from '../plugins/HastePlugin';
29+
import type HastePluginT, {HasteMapOptions} from '../plugins/HastePlugin';
3030
import type {MockMapOptions} from '../plugins/MockPlugin';
3131
import type {WatcherOptions} from '../watchers/common';
3232
import typeof WorkerModule from '../worker';
@@ -302,7 +302,7 @@ let buildNewFileMap: (
302302
) => Promise<{
303303
...BuildResult,
304304
fileMap: FileMapT,
305-
hasteMap: HasteMap,
305+
hasteMap: HastePluginT,
306306
mockMap: ?MockMap,
307307
dependencyPlugin: ?DependencyPlugin,
308308
}>;
@@ -504,6 +504,24 @@ describe('FileMap', () => {
504504
).toMatchObject({exists: true, type: 'f', data: null});
505505
});
506506

507+
test('throws on plugins that share a name, with or without a worker', () => {
508+
const MockPlugin = require('../plugins/MockPlugin').default;
509+
const mockConfig = {
510+
console: globalThis.console,
511+
rootDir: defaultConfig.rootDir,
512+
mocksPattern: /__mocks__/,
513+
throwOnModuleCollision: false,
514+
};
515+
expect(
516+
() =>
517+
new FileMap({
518+
...defaultConfig,
519+
// MockPlugin has no worker, so holds no plugin data slot
520+
plugins: [new MockPlugin(mockConfig), new MockPlugin(mockConfig)],
521+
}),
522+
).toThrow('metro-file-map: Duplicate plugin name: mocks');
523+
});
524+
507525
test('ignores files given a pattern', async () => {
508526
mockFs[p('/project/fruits/Kiwi.js')] = `
509527
// Kiwi!
@@ -1743,6 +1761,28 @@ describe('FileMap', () => {
17431761
}
17441762
});
17451763

1764+
fm_it(
1765+
'publishes what each plugin reports about a batch on the change event',
1766+
async ({fileMap, hasteMap}) => {
1767+
const getPluginChanges = require('../lib/getPluginChanges').default;
1768+
const fruitsRoot = path.join('/', 'project', 'fruits');
1769+
mockFs[path.join(fruitsRoot, 'Tomato.js')] = '// Tomato!';
1770+
mockEmitters[fruitsRoot].emitFileEvent({
1771+
event: 'touch',
1772+
relativePath: 'Tomato.js',
1773+
metadata: MOCK_CHANGE_FILE,
1774+
});
1775+
mockDeleteFile(fruitsRoot, 'Banana.js');
1776+
const changeEvent = await waitForItToChange(fileMap);
1777+
1778+
expect(getPluginChanges(changeEvent, hasteMap)).toEqual({
1779+
changedNames: new Set(['Tomato', 'Banana']),
1780+
});
1781+
// Only plugins with something to say are present
1782+
expect([...changeEvent.pluginChanges.keys()]).toEqual([hasteMap.name]);
1783+
},
1784+
);
1785+
17461786
function mockDeleteFile(root: string, relativePath: string) {
17471787
const e = mockEmitters[root];
17481788
e.emitFileEvent({event: 'delete', relativePath});
@@ -1757,7 +1797,7 @@ describe('FileMap', () => {
17571797

17581798
function fm_it(
17591799
title: string,
1760-
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HasteMap}>) => unknown,
1800+
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HastePluginT}>) => unknown,
17611801
options?: FileMapTestOptions = {},
17621802
): void {
17631803
options = options || {};
@@ -1780,7 +1820,7 @@ describe('FileMap', () => {
17801820

17811821
fm_it.only = (
17821822
title: string,
1783-
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HasteMap}>) => unknown,
1823+
fn: (fm: Readonly<{fileMap: FileMap, hasteMap: HastePluginT}>) => unknown,
17841824
options?: FileMapTestOptions,
17851825
): void => fm_it(title, fn, {...options, only: true});
17861826

@@ -2620,7 +2660,13 @@ describe('FileMap', () => {
26202660
relativePath: 'tropical',
26212661
});
26222662

2623-
await waitForItToChange(fileMap);
2663+
const changeEvent = await waitForItToChange(fileMap);
2664+
2665+
// Plugins are updated inside the recrawl, and what they report
2666+
// still reaches the event
2667+
expect(
2668+
require('../lib/getPluginChanges').default(changeEvent, hasteMap),
2669+
).toEqual({changedNames: new Set(['Mango', 'Papaya'])});
26242670

26252671
// Verify crawl was called with the correct directory
26262672
expect(mockNodeCrawler).toHaveBeenNthCalledWith(

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export type ChangedFileMetadata = Readonly<{
111111
export type ChangeEvent = Readonly<{
112112
logger?: ?RootPerfLogger,
113113
changes: ReadonlyFileSystemChanges<Readonly<ChangedFileMetadata>>,
114+
/**
115+
* What each plugin reported about this batch of changes, by plugin name. A
116+
* plugin whose `onChanged` returned nothing is absent. Read an entry with
117+
* `getPluginChanges`, which knows its type.
118+
*/
119+
pluginChanges: ReadonlyMap<string, unknown>,
114120
rootDir: string,
115121
}>;
116122

@@ -300,19 +306,29 @@ export type V8Serializable = V8SerializablePrimitive | V8SerializableCollection;
300306
export interface FileMapPlugin<
301307
in SerializableState extends void | V8Serializable = void | V8Serializable,
302308
in PerFileData extends void | V8Serializable = void | V8Serializable,
309+
out ChangeSummary = void,
303310
> {
311+
// Must be unique among the plugins of a file map.
304312
readonly name: string;
305313
initialize(
306314
initOptions: FileMapPluginInitOptions<SerializableState, PerFileData>,
307315
): Promise<void>;
308316
assertValid(): void;
309-
onChanged(changes: ReadonlyFileSystemChanges<?PerFileData>): void;
317+
/**
318+
* Update the plugin's state for a batch of changes. It is called
319+
* synchronously, immediately before the batch is emitted as a `ChangeEvent`.
320+
*
321+
* A plugin may return a summary of what changed in terms only it knows,
322+
* such as the Haste names that were rebound, for the file map to publish as
323+
* `ChangeEvent.pluginChanges`. Return nothing if there is nothing to say.
324+
*/
325+
onChanged(changes: ReadonlyFileSystemChanges<?PerFileData>): ChangeSummary;
310326
getSerializableSnapshot(): void | V8Serializable;
311327
getCacheKey(): string;
312328
getWorker(): ?FileMapPluginWorker;
313329
}
314330

315-
export type InputFileMapPlugin = FileMapPlugin<empty, empty>;
331+
export type InputFileMapPlugin = FileMapPlugin<empty, empty, unknown>;
316332

317333
export interface MetadataWorker {
318334
processFile(

0 commit comments

Comments
 (0)