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

Function load

packages/vite/src/node/plugins/optimizedDeps.ts:42–113  ·  view source on GitHub ↗
(id)

Source from the content-addressed store, hash-verified

40 // is in importAnalysis, see call to delayDepsOptimizerUntil
41
42 async load(id) {
43 const environment = this.environment as DevEnvironment
44 const depsOptimizer = environment.depsOptimizer
45 if (depsOptimizer?.isOptimizedDepFile(id)) {
46 const metadata = depsOptimizer.metadata
47 const file = cleanUrl(id)
48 const versionMatch = DEP_VERSION_RE.exec(id)
49 const browserHash = versionMatch
50 ? versionMatch[1].split('=')[1]
51 : undefined
52
53 // Search in both the currently optimized and newly discovered deps
54 const info = optimizedDepInfoFromFile(metadata, file)
55 if (info) {
56 if (
57 browserHash &&
58 info.browserHash !== browserHash &&
59 !environment.config.optimizeDeps.ignoreOutdatedRequests
60 ) {
61 throwOutdatedRequest(id)
62 }
63 try {
64 // This is an entry point, it may still not be bundled
65 await info.processing
66 } catch {
67 // If the refresh has not happened after timeout, Vite considers
68 // something unexpected has happened. In this case, Vite
69 // returns an empty response that will error.
70 throwProcessingError(id)
71 }
72 const newMetadata = depsOptimizer.metadata
73 if (metadata !== newMetadata) {
74 const currentInfo = optimizedDepInfoFromFile(newMetadata!, file)
75 if (
76 info.browserHash !== currentInfo?.browserHash &&
77 !environment.config.optimizeDeps.ignoreOutdatedRequests
78 ) {
79 throwOutdatedRequest(id)
80 }
81 }
82 }
83 debug?.(`load ${colors.cyan(file)}`)
84 // Load the file from the cache instead of waiting for other plugin
85 // load hooks to avoid race conditions, once processing is resolved,
86 // we are sure that the file has been properly save to disk
87 try {
88 const [code, map] = await Promise.all([
89 fsp.readFile(file, 'utf-8'),
90 fsp
91 .readFile(`${file}.map`, 'utf-8')
92 .then((map) => JSON.parse(map))
93 .catch(() => null),
94 ])
95 if (map) {
96 return {
97 code,
98 map,
99 }

Callers

nothing calls this directly

Calls 6

cleanUrlFunction · 0.90
optimizedDepInfoFromFileFunction · 0.90
throwOutdatedRequestFunction · 0.85
throwProcessingErrorFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected