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 {
class UpdateApplyError extends Error {
name = 'UpdateApplyError'
dependencyChain: string[]
dependencyChain: ModuleId[]
constructor(message: string, dependencyChain: string[]) {
constructor(message: string, dependencyChain: ModuleId[]) {
super(message)
this.dependencyChain = dependencyChain
}
......@@ -129,8 +129,10 @@ const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent<
})
}
// @ts-ignore Defined in `runtime-base.ts`
function instantiateModule(id: ModuleId, source: SourceInfo): Module {
function instantiateModule(moduleId: ModuleId, source: SourceInfo): Module {
// We are in development, this is always a string.
let id = moduleId as string
const moduleFactory = moduleFactories[id]
if (typeof moduleFactory !== 'function') {
// This can happen if modules incorrectly handle HMR disposes/updates,
......@@ -180,7 +182,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
exports: {},
error: undefined,
loaded: false,
id,
id: id,
parents,
children: [],
namespaceObject: undefined,
......@@ -245,7 +247,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
* refresh registry.
*/
function runModuleExecutionHooks(
module: Module,
module: HotModule,
executeModule: (ctx: RefreshContext) => void
) {
if (typeof globalThis.$RefreshInterceptModuleExecution$ === 'function') {
......@@ -266,9 +268,9 @@ function runModuleExecutionHooks(
// This is expected when running in a Web Worker. It is also common in some of
// our test environments.
executeModule({
register: (type, id) => {},
signature: () => (type) => {},
registerExports: (module, helpers) => {},
register: (_type, _id) => {},
signature: () => (_type) => {},
registerExports: (_module, _helpers) => {},
})
}
}
......
......@@ -67,5 +67,7 @@ interface Hot {
// Used through reference comments
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface HotModule extends ModuleWithDirection {
// In development, ModuleId is always a string
id: string
hot: Hot
}
......@@ -20,11 +20,13 @@ type ChunkRunner = {
let BACKEND: RuntimeBackend
type ExternalRequire = (
id: ModuleId,
id: DependencySpecifier,
thunk: () => any,
esm?: boolean
) => Exports | EsmNamespaceObject
type ExternalImport = (id: ModuleId) => Promise<Exports | EsmNamespaceObject>
type ExternalImport = (
id: DependencySpecifier
) => Promise<Exports | EsmNamespaceObject>
interface TurbopackEdgeContext extends TurbopackBaseContext<Module> {
x: ExternalRequire
......
......@@ -6,7 +6,7 @@
/// 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.
async function externalImport(id: ModuleId) {
async function externalImport(id: DependencySpecifier) {
let raw
try {
raw = await import(id)
......
......@@ -13,8 +13,10 @@ type ChunkListScript = CurrentScript & { readonly brand: unique symbol }
type ChunkPath = string & { readonly brand: unique symbol }
type ChunkScript = CurrentScript & { readonly brand: unique symbol }
type ChunkUrl = string & { readonly brand: unique symbol }
// TODO this should actually be `string | number`
type ModuleId = string
// The dependency specifier when importing externals
type DependencySpecifier = string
// This is a string in development and a number in production (both arbitrary, implementation defined)
type ModuleId = string | number
interface Exports {
__esModule?: boolean
......
......@@ -38,8 +38,8 @@ interface ModuleContext {
resolve(moduleId: ModuleId): ModuleId
}
type GetOrInstantiateModuleFromParent<M> = (
moduleId: ModuleId,
type GetOrInstantiateModuleFromParent<M extends Module> = (
moduleId: M['id'],
parentModule: M
) => M
......
......@@ -698,8 +698,9 @@ const getOrInstantiateModuleFromParent = (id, sourceModule)=>{
parentId: sourceModule.id
});
};
// @ts-ignore Defined in `runtime-base.ts`
function instantiateModule(id, source) {
function instantiateModule(moduleId, source) {
// We are in development, this is always a string.
let id = moduleId;
const moduleFactory = moduleFactories[id];
if (typeof moduleFactory !== 'function') {
// This can happen if modules incorrectly handle HMR disposes/updates,
......@@ -746,7 +747,7 @@ function instantiateModule(id, source) {
exports: {},
error: undefined,
loaded: false,
id,
id: id,
parents,
children: [],
namespaceObject: undefined,
......@@ -821,9 +822,9 @@ function instantiateModule(id, source) {
// This is expected when running in a Web Worker. It is also common in some of
// our test environments.
executeModule({
register: (type, id)=>{},
signature: ()=>(type)=>{},
registerExports: (module, helpers)=>{}
register: (_type, _id)=>{},
signature: ()=>(_type)=>{},
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