You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5361448
Browse filesBrowse the repository at this point in the historyBrowse files
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`.
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.
782
782
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()`.
0 commit comments