importMetaResolver.ts — vite Source File
Architecture documentation for importMetaResolver.ts, a typescript file in the vite codebase. 1 imports, 2 dependents.
Entity Profile
Dependency Diagram
graph LR 87fdb861_d26b_9010_7a55_34478977224c["importMetaResolver.ts"] ad382781_051b_4f17_29a3_3e882464e6c0["node:module"] 87fdb861_d26b_9010_7a55_34478977224c --> ad382781_051b_4f17_29a3_3e882464e6c0 d957f785_adef_de7a_92dc_045f724e6d34["createImportMeta.ts"] d957f785_adef_de7a_92dc_045f724e6d34 --> 87fdb861_d26b_9010_7a55_34478977224c 7da774f9_eca5_d54e_6e01_6bee7d460a2b["config.ts"] 7da774f9_eca5_d54e_6e01_6bee7d460a2b --> 87fdb861_d26b_9010_7a55_34478977224c style 87fdb861_d26b_9010_7a55_34478977224c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type { ResolveFnOutput, ResolveHookContext } from 'node:module'
export type ImportMetaResolver = (specifier: string, importer: string) => string
const customizationHookNamespace = 'vite-module-runner:import-meta-resolve/v1/'
const customizationHooksModule = /* js */ `
export async function resolve(specifier, context, nextResolve) {
if (specifier.startsWith(${JSON.stringify(customizationHookNamespace)})) {
const data = specifier.slice(${JSON.stringify(customizationHookNamespace)}.length)
const [parsedSpecifier, parsedImporter] = JSON.parse(data)
specifier = parsedSpecifier
context.parentURL = parsedImporter
}
return nextResolve(specifier, context)
}
`
function customizationHookResolve(
specifier: string,
context: ResolveHookContext,
nextResolve: (
specifier: string,
context: ResolveHookContext,
) => ResolveFnOutput,
): ResolveFnOutput {
if (specifier.startsWith(customizationHookNamespace)) {
const data = specifier.slice(customizationHookNamespace.length)
const [parsedSpecifier, parsedImporter] = JSON.parse(data)
specifier = parsedSpecifier
context.parentURL = parsedImporter
}
return nextResolve(specifier, context)
}
// Ensure that we only register the hook once
// Otherwise, a hook will be registered for each createImportMetaResolver call
// and eventually cause "Maximum call stack size exceeded" errors
let isHookRegistered = false
export function createImportMetaResolver(): ImportMetaResolver | undefined {
if (isHookRegistered) {
return importMetaResolveWithCustomHook
}
let module: typeof import('node:module') | undefined
try {
module =
typeof process !== 'undefined'
? process.getBuiltinModule('node:module').Module
: undefined
} catch {
return
}
// `module.Module` may be `undefined` when `node:module` is mocked
if (!module) {
return
}
// Use registerHooks if available as it's more performant
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- we check the existence
if (module.registerHooks) {
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- we checked the existence
module.registerHooks({ resolve: customizationHookResolve })
isHookRegistered = true
return importMetaResolveWithCustomHook
}
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- we check the existence
if (!module.register) {
return
}
try {
const hookModuleContent = `data:text/javascript,${encodeURI(customizationHooksModule)}`
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- we checked the existence
module.register(hookModuleContent)
} catch (e) {
// For `--experimental-network-imports` flag that exists in Node before v22
if ('code' in e && e.code === 'ERR_NETWORK_IMPORT_DISALLOWED') {
return
}
throw e
}
isHookRegistered = true
return importMetaResolveWithCustomHook
}
function importMetaResolveWithCustomHook(
specifier: string,
importer: string,
): string {
return import.meta.resolve(
`${customizationHookNamespace}${JSON.stringify([specifier, importer])}`,
)
}
// NOTE: use computed string to avoid `define` replacing `import.meta.resolve` when bundled
export const importMetaResolveWithCustomHookString: string = /* js */ `
(() => {
const resolve = 'resolve'
return (specifier, importer) =>
import.meta[resolve](
\`${customizationHookNamespace}\${JSON.stringify([specifier, importer])}\`,
)
})()
`
Domain
Subdomains
Functions
Types
Dependencies
- node:module
Source
Frequently Asked Questions
What does importMetaResolver.ts do?
importMetaResolver.ts is a source file in the vite codebase, written in typescript. It belongs to the ModuleRunner domain, SSRRuntime subdomain.
What functions are defined in importMetaResolver.ts?
importMetaResolver.ts defines 4 function(s): createImportMetaResolver, customizationHookResolve, importMetaResolveWithCustomHook, specifier.
What does importMetaResolver.ts depend on?
importMetaResolver.ts imports 1 module(s): node:module.
What files import importMetaResolver.ts?
importMetaResolver.ts is imported by 2 file(s): config.ts, createImportMeta.ts.
Where is importMetaResolver.ts in the architecture?
importMetaResolver.ts is located at packages/vite/src/module-runner/importMetaResolver.ts (domain: ModuleRunner, subdomain: SSRRuntime, directory: packages/vite/src/module-runner).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free