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

Function fetchModule

packages/vite/src/node/ssr/fetchModule.ts:24–116  ·  view source on GitHub ↗
(
  environment: DevEnvironment,
  url: string,
  importer?: string,
  options: FetchModuleOptions = {},
)

Source from the content-addressed store, hash-verified

22 * @experimental
23 */
24export async function fetchModule(
25 environment: DevEnvironment,
26 url: string,
27 importer?: string,
28 options: FetchModuleOptions = {},
29): Promise<FetchResult> {
30 if (
31 url.startsWith('data:') ||
32 isBuiltin(environment.config.resolve.builtins, url)
33 ) {
34 return { externalize: url, type: 'builtin' }
35 }
36
37 // handle file urls from not statically analyzable dynamic import
38 const isFileUrl = url.startsWith('file://')
39
40 if (isExternalUrl(url) && !isFileUrl) {
41 return { externalize: url, type: 'network' }
42 }
43
44 // if there is no importer, the file is an entry point
45 // entry points are always internalized
46 if (!isFileUrl && importer && url[0] !== '.' && url[0] !== '/') {
47 const { isProduction, root } = environment.config
48 const { externalConditions, dedupe, preserveSymlinks } =
49 environment.config.resolve
50
51 const resolved = tryNodeResolve(url, importer, {
52 mainFields: ['main'],
53 conditions: externalConditions,
54 externalConditions,
55 external: [],
56 noExternal: [],
57 extensions: ['.js', '.cjs', '.json'],
58 dedupe,
59 preserveSymlinks,
60 tsconfigPaths: false,
61 isBuild: false,
62 isProduction,
63 root,
64 packageCache: environment.config.packageCache,
65 builtins: environment.config.resolve.builtins,
66 })
67 if (!resolved) {
68 const err: any = new Error(
69 `Cannot find module '${url}' imported from '${importer}'`,
70 )
71 err.code = 'ERR_MODULE_NOT_FOUND'
72 throw err
73 }
74 const file = pathToFileURL(resolved.id).toString()
75 const type = isFilePathESM(resolved.id, environment.config.packageCache)
76 ? 'module'
77 : 'commonjs'
78 return { externalize: file, type }
79 }
80
81 url = unwrapId(url)

Callers 1

fetchModuleMethod · 0.90

Calls 8

isBuiltinFunction · 0.90
isExternalUrlFunction · 0.90
tryNodeResolveFunction · 0.90
isFilePathESMFunction · 0.90
unwrapIdFunction · 0.90
inlineSourceMapFunction · 0.85
transformRequestMethod · 0.65
ensureEntryFromUrlMethod · 0.45

Tested by

no test coverage detected