Skip to content

Commit 5361448

Browse files
committed
Pass the graph's transform options to createModuleId
A module ID factory sees only a module's path, so a server that runs bundles for several platforms or environments in one JavaScript environment has no way to give each its own ID space. Expo patches `Server._getSortedModules` and copies `HmrServer._prepareMessage` to pass such a context itself, and its serializer wraps `createModuleId` to do the same. `createModuleId` now takes an optional second argument, `{transformOptions}`, the transform options of the graph the module belongs to. `Server` and `HmrServer` bind it per graph wherever Metro serializes a graph: bundle, source map, symbolication (exploded maps), `build()`, RAM bundle info and HMR updates. Serializers still receive a one-argument `createModuleId` in their options, and calls to it from Metro's serializers, `helpers/js`, `wrapModule` and custom serializers now carry the context. **Semantics** - Factories that ignore the second argument behave exactly as before. The default factory ignores it, so bundles, maps and HMR payloads are unchanged. - The bound function always passes Metro's context. A custom serializer that called `options.createModuleId(path, ownContext)` used to reach the factory with `ownContext`, and now reaches it with `{transformOptions}`. A factory that read its own context shape from a serializer must read `context.transformOptions` instead (Expo's `createStableModuleIdFactory` is the known case). - Metro builds a new context object per bind, so a factory must compare contexts by value. A lazily loaded bundle has its parent's transform options, so a value-deterministic factory gives a module the same ID in both. - `Server.getCreateModuleId()` returns the unbound function. Callers that pass only a path get no context. - `hmrJSBundle` passes only the path when mapping inverse dependencies. It used `.map(createModuleId)`, which passed the array index as a second argument. **Types** `ModuleIdContext` and `CreateModuleIdFn` are exported from `metro` and `metro/private/DeltaBundler/types`, and `serializer.createModuleIdFactory` is typed `() => CreateModuleIdFn`. A one-argument function still satisfies it. A factory whose second parameter has an incompatible type, such as `{platform?: ?string, environment?: ?string}`, no longer type-checks against it. Changelog: [Feature] `serializer.createModuleIdFactory`: the returned function receives the module's graph transform options as an optional second argument, `{transformOptions}`. [Types] Factories typing a different second parameter must accept `ModuleIdContext`.
1 parent ba761ed commit 5361448

13 files changed

Lines changed: 211 additions & 21 deletions

File tree

‎docs/Configuration.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,14 @@ Formats the require statements appended to the end of the bundle: one for each m
776776
777777
#### `createModuleIdFactory`
778778
779-
Type: `() => (path: string) => number`
779+
Type: `() => (path: string, context?: {transformOptions: TransformInputOptions}) => number`
780780
781781
Returns a function mapping a [module](./Concepts.md#module)'s absolute path to the ID used in `require` statements. By default, IDs are sequential numbers in the order modules are first seen.
782782
783+
When Metro serializes a graph (bundle, source map, symbolication, RAM bundle, HMR and `build` requests, including the `createModuleId` it passes to a custom serializer), it also passes a `context` with the graph's transform options (`platform`, `dev`, `customTransformOptions` and so on). A factory can use it to give a path a different ID per platform or environment, for example when bundles for several of them run in one JavaScript environment.
784+
785+
Compare `context` by value: Metro doesn't reuse context objects between calls. A lazily loaded bundle has its parent's transform options, so a value-deterministic factory gives a module the same ID in both. `context` is absent when the function is called directly, such as through `Server.getCreateModuleId()`.
786+
783787
#### `getPolyfills`
784788
785789
Type: `({platform: ?string}) => $ReadOnlyArray<string>`

‎packages/metro-config/API.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export interface RootPerfLogger extends PerfLogger {
172172
}
173173

174174
export type SerializerConfigT = {
175-
createModuleIdFactory: () => (path: string) => number;
175+
createModuleIdFactory: () => CreateModuleIdFn;
176176
customSerializer: null | undefined | ((entryPoint: string, preModules: ReadonlyArray<Module>, graph: ReadOnlyGraph, options: SerializerOptions) => Promise<string | {code: string; map: string}>);
177177
experimentalSerializerHook: (graph: ReadOnlyGraph, delta: DeltaResult) => unknown;
178178
getModulesRunBeforeMainModule: (entryFilePath: string) => Array<string>;

‎packages/metro-config/src/types.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {CacheManagerFactory, InputFileMapPlugin} from 'metro-file-map';
1515
import type {CustomResolver} from 'metro-resolver';
1616
import type {JsTransformerConfig} from 'metro-transform-worker';
1717
import type {
18+
CreateModuleIdFn,
1819
DeltaResult,
1920
Module,
2021
ReadOnlyGraph,
@@ -131,7 +132,7 @@ type ResolverConfigT = {
131132
};
132133

133134
type SerializerConfigT = {
134-
createModuleIdFactory: () => (path: string) => number,
135+
createModuleIdFactory: () => CreateModuleIdFn,
135136
customSerializer: ?(
136137
entryPoint: string,
137138
preModules: ReadonlyArray<Module<>>,

‎packages/metro/API.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export const createConnectMiddleware: (config: ConfigT, options?: RunMetroOption
4747

4848
export type createConnectMiddleware = typeof createConnectMiddleware;
4949

50+
export type CreateModuleIdFn = (path: string, context?: ModuleIdContext) => number;
51+
5052
export type DeltaResult<T = MixedOutput> = {
5153
readonly added: Map<string, Module<T>>;
5254
readonly modified: Map<string, Module<T>>;
@@ -136,7 +138,7 @@ export class MetroServer {
136138
end(): Promise<void>;
137139
getAssets(options: BundleOptions): Promise<ReadonlyArray<AssetData>>;
138140
getBundler(): IncrementalBundler;
139-
getCreateModuleId(): (path: string) => number;
141+
getCreateModuleId(): CreateModuleIdFn;
140142
getNewBuildNumber(): number;
141143
getOrderedDependencyPaths(options: {readonly dev: boolean; readonly entryFile: string; readonly minify: boolean; readonly platform: null | undefined | string}): Promise<Array<string>>;
142144
getPlatforms(): ReadonlyArray<string>;
@@ -157,6 +159,10 @@ export type Module<T = MixedOutput> = Readonly<{
157159
unstable_transformResultKey?: null | undefined | string;
158160
}>;
159161

162+
export type ModuleIdContext = Readonly<{
163+
transformOptions: Readonly<TransformInputOptions>;
164+
}>;
165+
160166
export type ReadOnlyDependencies<T = MixedOutput> = ReadonlyMap<string, Module<T>>;
161167

162168
export interface ReadOnlyGraph<T = MixedOutput> {

‎packages/metro/src/DeltaBundler/Serializers/__tests__/hmrJSBundle-test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,30 @@ test('async paths in updated modules carry the platform of the bundle', () => {
296296
'"paths":{"0":"/chunk.ios.js.bundle?lazy=true&platform=ios&modulesOnly=true&runModule=false"}',
297297
);
298298
});
299+
300+
test('passes only the path to createModuleId for inverse dependencies', () => {
301+
const createModuleId = jest.fn((path: string) =>
302+
path === '/root/foo' ? 1 : 0,
303+
);
304+
hmrJSBundle(
305+
{
306+
added: new Map(),
307+
modified: new Map([['/root/bar', barModule]]),
308+
deleted: new Set(),
309+
reset: false,
310+
},
311+
{
312+
entryPoints: new Set(['/root/foo']),
313+
transformOptions,
314+
dependencies: new Map([
315+
['/root/foo', fooModule],
316+
['/root/bar', barModule],
317+
]),
318+
},
319+
{...options, createModuleId},
320+
);
321+
expect(createModuleId.mock.calls).toContainEqual(['/root/foo']);
322+
for (const call of createModuleId.mock.calls) {
323+
expect(call).toHaveLength(1);
324+
}
325+
});

‎packages/metro/src/DeltaBundler/Serializers/hmrJSBundle.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function prepareModule(
117117
// $FlowFixMe[invalid-computed-prop]
118118
inverseDependenciesById[options.createModuleId(path)] = inverseDependencies[
119119
path
120-
].map(options.createModuleId);
120+
].map(inverseDependency => options.createModuleId(inverseDependency));
121121
});
122122
return addParamsToDefineCall(code, inverseDependenciesById);
123123
}

‎packages/metro/src/DeltaBundler/types.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,11 @@ export type TransformInputOptions = Omit<
133133

134134
export type GraphInputOptions = Readonly<{
135135
entryPoints: ReadonlySet<string>,
136-
// Unused in core but useful for custom serializers / experimentalSerializerHook
137136
transformOptions: TransformInputOptions,
138137
}>;
139138

140139
export interface ReadOnlyGraph<T = MixedOutput> {
141140
readonly entryPoints: ReadonlySet<string>;
142-
// Unused in core but useful for custom serializers / experimentalSerializerHook
143141
readonly transformOptions: Readonly<TransformInputOptions>;
144142
readonly dependencies: ReadOnlyDependencies<T>;
145143
}
@@ -221,6 +219,23 @@ export type ReleasedModules<T = MixedOutput> = Readonly<{
221219
heldOutputs: ReadonlySet<ReadonlyArray<T>>,
222220
}>;
223221

222+
/**
223+
* What `createModuleId` is told about the graph a module belongs to, besides
224+
* the module's path.
225+
*/
226+
export type ModuleIdContext = Readonly<{
227+
transformOptions: Readonly<TransformInputOptions>,
228+
}>;
229+
230+
/**
231+
* Maps a module's path to its ID. Metro passes `context` wherever the module's
232+
* graph is known.
233+
*/
234+
export type CreateModuleIdFn = (
235+
path: string,
236+
context?: ModuleIdContext,
237+
) => number;
238+
224239
export type SerializerOptions = Readonly<{
225240
asyncRequireModulePath: string,
226241
createModuleId: (absolutePath: string) => number,

‎packages/metro/src/HmrServer.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
import type {CreateModuleIdFn} from './DeltaBundler/types';
1112
import type {
1213
RevisionId,
1314
default as IncrementalBundler,
@@ -73,13 +74,13 @@ function send(
7374
export default class HmrServer<TClient extends Client> {
7475
_config: ConfigT;
7576
_bundler: IncrementalBundler;
76-
_createModuleId: (path: string) => number;
77+
_createModuleId: CreateModuleIdFn;
7778
_clientGroups: Map<RevisionId, ClientGroup>;
7879
_rootUrlMap: RootUrlMap;
7980

8081
constructor(
8182
bundler: IncrementalBundler,
82-
createModuleId: (path: string) => number,
83+
createModuleId: CreateModuleIdFn,
8384
config: ConfigT,
8485
) {
8586
this._config = config;
@@ -382,9 +383,13 @@ export default class HmrServer<TClient extends Client> {
382383

383384
logger?.point('serialize_start');
384385

386+
const moduleIdContext = {
387+
transformOptions: revision.graph.transformOptions,
388+
};
385389
const hmrUpdate = hmrJSBundle(delta, revision.graph, {
386390
clientUrl: new URL(group.clientUrl),
387-
createModuleId: this._createModuleId,
391+
createModuleId: (path: string) =>
392+
this._createModuleId(path, moduleIdContext),
388393
includeAsyncPaths: group.graphOptions.lazy,
389394
projectRoot: this._config.projectRoot,
390395
serverRoot: this._rootUrlMap.serverRootDir,

‎packages/metro/src/Server.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {AssetData} from './Assets';
1313
import type {ExplodedSourceMap} from './DeltaBundler/Serializers/getExplodedSourceMap';
1414
import type {RamBundleInfo} from './DeltaBundler/Serializers/getRamBundleInfo';
1515
import type {
16+
CreateModuleIdFn,
1617
MixedOutput,
1718
Module,
1819
ReadOnlyDependencies,
@@ -149,7 +150,7 @@ type FetchTiming = {
149150
export default class Server {
150151
_bundler: IncrementalBundler;
151152
_config: ConfigT;
152-
_createModuleId: (path: string) => number;
153+
_createModuleId: CreateModuleIdFn;
153154
_isEnded: boolean;
154155
_logger: typeof Logger;
155156
_nextBundleBuildNumber: number;
@@ -214,10 +215,16 @@ export default class Server {
214215
return this._bundler;
215216
}
216217

217-
getCreateModuleId(): (path: string) => number {
218+
getCreateModuleId(): CreateModuleIdFn {
218219
return this._createModuleId;
219220
}
220221

222+
// Binds `createModuleId` to the graph's transform options.
223+
_createModuleIdForGraph(graph: ReadOnlyGraph<>): (path: string) => number {
224+
const context = {transformOptions: graph.transformOptions};
225+
return (path: string) => this._createModuleId(path, context);
226+
}
227+
221228
async _serializeGraph({
222229
splitOptions,
223230
prepend,
@@ -247,7 +254,7 @@ export default class Server {
247254
},
248255
),
249256
processModuleFilter: this._config.serializer.processModuleFilter,
250-
createModuleId: this._createModuleId,
257+
createModuleId: this._createModuleIdForGraph(graph),
251258
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
252259
globalPrefix: this._config.transformer.globalPrefix,
253260
dev: transformOptions.dev,
@@ -395,7 +402,7 @@ export default class Server {
395402
},
396403
),
397404
processModuleFilter: this._config.serializer.processModuleFilter,
398-
createModuleId: this._createModuleId,
405+
createModuleId: this._createModuleIdForGraph(graph),
399406
dev: transformOptions.dev,
400407
excludeSource: serializerOptions.excludeSource,
401408
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
@@ -1144,7 +1151,7 @@ export default class Server {
11441151
},
11451152
),
11461153
processModuleFilter: this._config.serializer.processModuleFilter,
1147-
createModuleId: this._createModuleId,
1154+
createModuleId: this._createModuleIdForGraph(revision.graph),
11481155
getRunModuleStatement: this._config.serializer.getRunModuleStatement,
11491156
globalPrefix: this._config.transformer.globalPrefix,
11501157
includeAsyncPaths: graphOptions.lazy,
@@ -1243,15 +1250,16 @@ export default class Server {
12431250
// This function ensures that modules in source maps are sorted in the same
12441251
// order as in a plain JS bundle.
12451252
_getSortedModules(graph: ReadOnlyGraph<>): ReadonlyArray<Module<>> {
1253+
const createModuleId = this._createModuleIdForGraph(graph);
12461254
const modules = [...graph.dependencies.values()];
12471255
// Assign IDs to modules in a consistent order
12481256
for (const module of modules) {
1249-
this._createModuleId(module.path);
1257+
createModuleId(module.path);
12501258
}
12511259
// Sort by IDs
12521260
return modules.sort(
12531261
(a: Module<MixedOutput>, b: Module<MixedOutput>) =>
1254-
this._createModuleId(a.path) - this._createModuleId(b.path),
1262+
createModuleId(a.path) - createModuleId(b.path),
12551263
);
12561264
}
12571265

‎packages/metro/src/Server/__tests__/Server-test.js‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import type {Graph} from '../../DeltaBundler/Graph';
1313
import type {
1414
Dependency,
1515
Module,
16+
ModuleIdContext,
1617
Options,
1718
ReadOnlyGraph,
1819
TransformResultDependency,
1920
} from '../../DeltaBundler/types';
21+
import type {BundleOptions} from '../../shared/types';
2022
import type {InputConfigT} from 'metro-config';
2123

2224
import ResourceNotFoundError from '../../IncrementalBundler/ResourceNotFoundError';
@@ -1658,6 +1660,89 @@ describe('processRequest', () => {
16581660
);
16591661
});
16601662

1663+
describe('createModuleId context', () => {
1664+
let contexts: Array<?ModuleIdContext>;
1665+
1666+
beforeEach(() => {
1667+
contexts = [];
1668+
server = new Server(
1669+
mergeConfig(config, {
1670+
serializer: {
1671+
createModuleIdFactory: () => {
1672+
const ids = new Map<string, number>();
1673+
return (path: string, context?: ModuleIdContext) => {
1674+
contexts.push(context);
1675+
let id = ids.get(path);
1676+
if (id == null) {
1677+
id = ids.size;
1678+
ids.set(path, id);
1679+
}
1680+
return id;
1681+
};
1682+
},
1683+
},
1684+
} as InputConfigT),
1685+
);
1686+
});
1687+
1688+
const expectGraphContext = () => {
1689+
expect(contexts.length).toBeGreaterThan(0);
1690+
for (const context of contexts) {
1691+
expect(context?.transformOptions).toEqual(
1692+
expect.objectContaining({
1693+
platform: 'ios',
1694+
customTransformOptions: {env: 'test'},
1695+
}),
1696+
);
1697+
}
1698+
};
1699+
1700+
test.each([
1701+
['bundle', 'mybundle.bundle?platform=ios&transform.env=test'],
1702+
['source map', 'mybundle.map?platform=ios&transform.env=test'],
1703+
])("is the graph's transform options for a %s request", async (_, url) => {
1704+
await makeRequest(url);
1705+
expectGraphContext();
1706+
});
1707+
1708+
test("is the graph's transform options for symbolication", async () => {
1709+
fs.mkdirSync(p('/root'));
1710+
fs.writeFileSync(p('/root/mybundle.js'), 'mock data');
1711+
fs.writeFileSync(p('/root/foo.js'), 'mock data');
1712+
1713+
await makeRequest('/symbolicate', {
1714+
headers: {'content-type': 'application/json'},
1715+
data: JSON.stringify({
1716+
stack: [
1717+
{
1718+
file: 'http://localhost:8081/mybundle.bundle?platform=ios&transform.env=test',
1719+
lineNumber: 2,
1720+
column: 18,
1721+
methodName: 'clientSideMethodName',
1722+
},
1723+
],
1724+
}),
1725+
});
1726+
expectGraphContext();
1727+
});
1728+
1729+
test.each([
1730+
['build()', (options: BundleOptions) => server.build(options)],
1731+
[
1732+
'getRamBundleInfo()',
1733+
(options: BundleOptions) => server.getRamBundleInfo(options),
1734+
],
1735+
])("is the graph's transform options for %s", async (_, run) => {
1736+
await run({
1737+
...Server.DEFAULT_BUNDLE_OPTIONS,
1738+
customTransformOptions: {env: 'test'},
1739+
entryFile: 'mybundle.js',
1740+
platform: 'ios',
1741+
});
1742+
expectGraphContext();
1743+
});
1744+
});
1745+
16611746
describe('asset URL roots', () => {
16621747
test('anchors asset URLs on projectRoot, not unstable_serverRoot', async () => {
16631748
// $FlowFixMe[unclear-type] - reaching for a private method under test.

0 commit comments

Comments
 (0)