MCPcopy Create free account
hub / github.com/vitest-dev/vitest / fetch

Method fetch

packages/vitest/src/node/environments/fetchModule.ts:33–140  ·  view source on GitHub ↗
(
    trace: Span,
    url: string,
    importer: string | undefined,
    environment: DevEnvironment,
    makeTmpCopies?: boolean,
    options?: FetchFunctionOptions,
  )

Source from the content-addressed store, hash-verified

31 }
32
33 async fetch(
34 trace: Span,
35 url: string,
36 importer: string | undefined,
37 environment: DevEnvironment,
38 makeTmpCopies?: boolean,
39 options?: FetchFunctionOptions,
40 ): Promise<FetchResult | FetchCachedFileSystemResult> {
41 if (url.startsWith('data:')) {
42 trace.setAttribute('vitest.module.external', url)
43 return { externalize: url, type: 'builtin' }
44 }
45
46 if (url === '/@vite/client' || url === '@vite/client') {
47 trace.setAttribute('vitest.module.external', url)
48 return { externalize: '/@vite/client', type: 'module' }
49 }
50
51 const isFileUrl = url.startsWith('file://')
52
53 if (isExternalUrl(url) && !isFileUrl) {
54 trace.setAttribute('vitest.module.external', url)
55 return { externalize: url, type: 'network' }
56 }
57
58 // handle unresolved id of dynamic import skipped by Vite import analysis
59 if (url[0] !== '/') {
60 const resolved = await environment.pluginContainer.resolveId(url, importer)
61 if (resolved) {
62 url = normalizeResolvedIdToUrl(environment, resolved.id)
63 }
64 }
65
66 const moduleGraphModule = await environment.moduleGraph.ensureEntryFromUrl(unwrapId(url))
67 const cached = !!moduleGraphModule.transformResult
68
69 if (moduleGraphModule.file) {
70 trace.setAttribute('code.file.path', moduleGraphModule.file)
71 }
72
73 if (options?.cached && cached) {
74 return { cache: true }
75 }
76
77 const externalize = await this.resolver.shouldExternalize(moduleGraphModule.id!)
78 if (externalize) {
79 return { externalize, type: 'module' }
80 }
81
82 const cachePath = await this.getCachePath(
83 environment,
84 moduleGraphModule,
85 )
86
87 // full fs caching is disabled, but we still want to keep tmp files if makeTmpCopies is enabled
88 // this is primarily used by the forks pool to avoid using process.send(bigBuffer)
89 if (cachePath == null) {
90 const result = await this.fetchAndProcess(environment, url, importer, moduleGraphModule, options)

Callers 3

fetchModuleFunction · 0.80
fetchModuleFunction · 0.80

Calls 15

getCachePathMethod · 0.95
fetchAndProcessMethod · 0.95
recordResultMethod · 0.95
cacheResultMethod · 0.95
getCachedModuleMethod · 0.95
getSerializedImportsMethod · 0.95
isExternalUrlFunction · 0.90
normalizeResolvedIdToUrlFunction · 0.90
unwrapIdFunction · 0.90
getCachedResultFunction · 0.85
shouldExternalizeMethod · 0.80
joinFunction · 0.50

Tested by

no test coverage detected