Unverified Commit 6c4ca1cb authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: slightly more correct `ModuleId` type (#80605)

This is slightly better than before, but `Record<ModuleId, ...>` is still odd when you iterate over it (because a number module id will become a string).
parent 96e6d0fe
...@@ -54,9 +54,9 @@ interface DevRuntimeBackend { ...@@ -54,9 +54,9 @@ interface DevRuntimeBackend {
class UpdateApplyError extends Error { class UpdateApplyError extends Error {
name = 'UpdateApplyError' name = 'UpdateApplyError'
dependencyChain: string[] dependencyChain: ModuleId[]
constructor(message: string, dependencyChain: string[]) { constructor(message: string, dependencyChain: ModuleId[]) {
super(message) super(message)
this.dependencyChain = dependencyChain this.dependencyChain = dependencyChain
} }
...@@ -129,8 +129,10 @@ const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent< ...@@ -129,8 +129,10 @@ const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent<
}) })
} }
// @ts-ignore Defined in `runtime-base.ts` function instantiateModule(moduleId: ModuleId, source: SourceInfo): Module {
function instantiateModule(id: ModuleId, source: SourceInfo): Module { // We are in development, this is always a string.
let id = moduleId as string
const moduleFactory = moduleFactories[id] const moduleFactory = moduleFactories[id]
if (typeof moduleFactory !== 'function') { if (typeof moduleFactory !== 'function') {
// This can happen if modules incorrectly handle HMR disposes/updates, // This can happen if modules incorrectly handle HMR disposes/updates,
...@@ -180,7 +182,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module { ...@@ -180,7 +182,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
exports: {}, exports: {},
error: undefined, error: undefined,
loaded: false, loaded: false,
id, id: id,
parents, parents,
children: [], children: [],
namespaceObject: undefined, namespaceObject: undefined,
...@@ -245,7 +247,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module { ...@@ -245,7 +247,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
* refresh registry. * refresh registry.
*/ */
function runModuleExecutionHooks( function runModuleExecutionHooks(
module: Module, module: HotModule,
executeModule: (ctx: RefreshContext) => void executeModule: (ctx: RefreshContext) => void
) { ) {
if (typeof globalThis.$RefreshInterceptModuleExecution$ === 'function') { if (typeof globalThis.$RefreshInterceptModuleExecution$ === 'function') {
...@@ -266,9 +268,9 @@ function runModuleExecutionHooks( ...@@ -266,9 +268,9 @@ function runModuleExecutionHooks(
// This is expected when running in a Web Worker. It is also common in some of // This is expected when running in a Web Worker. It is also common in some of
// our test environments. // our test environments.
executeModule({ executeModule({
register: (type, id) => {}, register: (_type, _id) => {},
signature: () => (type) => {}, signature: () => (_type) => {},
registerExports: (module, helpers) => {}, registerExports: (_module, _helpers) => {},
}) })
} }
} }
......
...@@ -67,5 +67,7 @@ interface Hot { ...@@ -67,5 +67,7 @@ interface Hot {
// Used through reference comments // Used through reference comments
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
interface HotModule extends ModuleWithDirection { interface HotModule extends ModuleWithDirection {
// In development, ModuleId is always a string
id: string
hot: Hot hot: Hot
} }
...@@ -20,11 +20,13 @@ type ChunkRunner = { ...@@ -20,11 +20,13 @@ type ChunkRunner = {
let BACKEND: RuntimeBackend let BACKEND: RuntimeBackend
type ExternalRequire = ( type ExternalRequire = (
id: ModuleId, id: DependencySpecifier,
thunk: () => any, thunk: () => any,
esm?: boolean esm?: boolean
) => Exports | EsmNamespaceObject ) => Exports | EsmNamespaceObject
type ExternalImport = (id: ModuleId) => Promise<Exports | EsmNamespaceObject> type ExternalImport = (
id: DependencySpecifier
) => Promise<Exports | EsmNamespaceObject>
interface TurbopackEdgeContext extends TurbopackBaseContext<Module> { interface TurbopackEdgeContext extends TurbopackBaseContext<Module> {
x: ExternalRequire x: ExternalRequire
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
/// Currently this is for node.js / edge runtime both. /// Currently this is for node.js / edge runtime both.
/// If a fn requires node.js specific behavior, it should be placed in `node-external-utils` instead. /// If a fn requires node.js specific behavior, it should be placed in `node-external-utils` instead.
async function externalImport(id: ModuleId) { async function externalImport(id: DependencySpecifier) {
let raw let raw
try { try {
raw = await import(id) raw = await import(id)
......
...@@ -13,8 +13,10 @@ type ChunkListScript = CurrentScript & { readonly brand: unique symbol } ...@@ -13,8 +13,10 @@ type ChunkListScript = CurrentScript & { readonly brand: unique symbol }
type ChunkPath = string & { readonly brand: unique symbol } type ChunkPath = string & { readonly brand: unique symbol }
type ChunkScript = CurrentScript & { readonly brand: unique symbol } type ChunkScript = CurrentScript & { readonly brand: unique symbol }
type ChunkUrl = string & { readonly brand: unique symbol } type ChunkUrl = string & { readonly brand: unique symbol }
// TODO this should actually be `string | number` // The dependency specifier when importing externals
type ModuleId = string type DependencySpecifier = string
// This is a string in development and a number in production (both arbitrary, implementation defined)
type ModuleId = string | number
interface Exports { interface Exports {
__esModule?: boolean __esModule?: boolean
......
...@@ -38,8 +38,8 @@ interface ModuleContext { ...@@ -38,8 +38,8 @@ interface ModuleContext {
resolve(moduleId: ModuleId): ModuleId resolve(moduleId: ModuleId): ModuleId
} }
type GetOrInstantiateModuleFromParent<M> = ( type GetOrInstantiateModuleFromParent<M extends Module> = (
moduleId: ModuleId, moduleId: M['id'],
parentModule: M parentModule: M
) => M ) => M
......
...@@ -698,8 +698,9 @@ const getOrInstantiateModuleFromParent = (id, sourceModule)=>{ ...@@ -698,8 +698,9 @@ const getOrInstantiateModuleFromParent = (id, sourceModule)=>{
parentId: sourceModule.id parentId: sourceModule.id
}); });
}; };
// @ts-ignore Defined in `runtime-base.ts` function instantiateModule(moduleId, source) {
function instantiateModule(id, source) { // We are in development, this is always a string.
let id = moduleId;
const moduleFactory = moduleFactories[id]; const moduleFactory = moduleFactories[id];
if (typeof moduleFactory !== 'function') { if (typeof moduleFactory !== 'function') {
// This can happen if modules incorrectly handle HMR disposes/updates, // This can happen if modules incorrectly handle HMR disposes/updates,
...@@ -746,7 +747,7 @@ function instantiateModule(id, source) { ...@@ -746,7 +747,7 @@ function instantiateModule(id, source) {
exports: {}, exports: {},
error: undefined, error: undefined,
loaded: false, loaded: false,
id, id: id,
parents, parents,
children: [], children: [],
namespaceObject: undefined, namespaceObject: undefined,
...@@ -821,9 +822,9 @@ function instantiateModule(id, source) { ...@@ -821,9 +822,9 @@ function instantiateModule(id, source) {
// This is expected when running in a Web Worker. It is also common in some of // This is expected when running in a Web Worker. It is also common in some of
// our test environments. // our test environments.
executeModule({ executeModule({
register: (type, id)=>{}, register: (_type, _id)=>{},
signature: ()=>(type)=>{}, signature: ()=>(_type)=>{},
registerExports: (module, helpers)=>{} registerExports: (_module, _helpers)=>{}
}); });
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment