MCPcopy
hub / github.com/vitejs/vite / directRequest

Method directRequest

packages/vite/src/module-runner/runner.ts:324–423  ·  view source on GitHub ↗
(
    url: string,
    mod: EvaluatedModuleNode,
    _callstack: string[],
  )

Source from the content-addressed store, hash-verified

322
323 // override is allowed, consider this a public API
324 protected async directRequest(
325 url: string,
326 mod: EvaluatedModuleNode,
327 _callstack: string[],
328 ): Promise<any> {
329 const fetchResult = mod.meta!
330 const moduleId = fetchResult.id
331 const callstack = [..._callstack, moduleId]
332
333 const request = async (dep: string, metadata?: SSRImportMetadata) => {
334 const importer = ('file' in fetchResult && fetchResult.file) || moduleId
335 const depMod = await this.cachedModule(dep, importer)
336 depMod.importers.add(moduleId)
337 mod.imports.add(depMod.id)
338
339 return this.cachedRequest(dep, depMod, callstack, metadata)
340 }
341
342 const dynamicRequest = async (dep: string) => {
343 // it's possible to provide an object with toString() method inside import()
344 dep = String(dep)
345 if (dep[0] === '.') {
346 dep = posixResolve(posixDirname(url), dep)
347 }
348 return request(dep, { isDynamicImport: true })
349 }
350
351 if ('externalize' in fetchResult) {
352 const { externalize } = fetchResult
353 this.debug?.('[module runner] externalizing', externalize)
354 const exports = await this.evaluator.runExternalModule(externalize)
355 mod.exports = exports
356 return exports
357 }
358
359 const { code, file } = fetchResult
360
361 if (code == null) {
362 const importer = callstack[callstack.length - 2]
363 throw new Error(
364 `[module runner] Failed to load "${url}"${
365 importer ? ` imported from ${importer}` : ''
366 }`,
367 )
368 }
369
370 const createImportMeta =
371 this.options.createImportMeta ?? createDefaultImportMeta
372
373 const modulePath = cleanUrl(file || moduleId)
374 // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
375 const href = posixPathToFileHref(modulePath)
376 const meta = await createImportMeta(modulePath)
377 const exports = Object.create(null)
378 Object.defineProperty(exports, Symbol.toStringTag, {
379 value: 'Module',
380 enumerable: false,
381 configurable: false,

Callers 1

cachedRequestMethod · 0.95

Calls 6

cleanUrlFunction · 0.90
posixPathToFileHrefFunction · 0.90
exportAllFunction · 0.85
debugMethod · 0.65
runExternalModuleMethod · 0.65
runInlinedModuleMethod · 0.65

Tested by

no test coverage detected