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

Function loadAndTransform

packages/vite/src/node/server/transformRequest.ts:237–444  ·  packages/vite/src/node/server/transformRequest.ts::loadAndTransform
(
  environment: DevEnvironment,
  id: string,
  url: string,
  options: TransformOptionsInternal,
  timestamp: number,
  mod?: EnvironmentModuleNode,
  resolved?: PartialResolvedId,
)

Source from the content-addressed store, hash-verified

235}
236
237async function loadAndTransform(
238 environment: DevEnvironment,
239 id: string,
240 url: string,
241 options: TransformOptionsInternal,
242 timestamp: number,
243 mod?: EnvironmentModuleNode,
244 resolved?: PartialResolvedId,
245) {
246 const { config, pluginContainer, logger } = environment
247 const prettyUrl =
248 debugLoad || debugTransform ? prettifyUrl(url, config.root) : class="st">''
249
250 const moduleGraph = environment.moduleGraph
251
252 if (
253 !options.skipFsCheck &&
254 id[0] !== class="st">'\0' &&
255 isServerAccessDeniedForTransform(config, id)
256 ) {
257 const err: any = new Error(`Denied ID ${id}`)
258 err.code = ERR_DENIED_ID
259 err.id = id
260 throw err
261 }
262
263 let code: string | null = null
264 let map: SourceDescription[class="st">'map'] = null
265 let moduleType: ModuleType | undefined
266
267 class="cm">// load
268 const loadStart = debugLoad ? performance.now() : 0
269 const loadResult = await pluginContainer.load(id)
270
271 if (loadResult == null) {
272 const file = cleanUrl(id)
273
274 class="cm">// try fallback loading it from fs as string
275 class="cm">// if the file is a binary, there should be a plugin that already loaded it
276 class="cm">// as string
277 class="cm">// only try the fallback if access is allowed, skip for out of root url
278 class="cm">// like /service-worker.js or /api/users
279 if (
280 options.skipFsCheck ||
281 isFileLoadingAllowed(environment.getTopLevelConfig(), slash(file))
282 ) {
283 try {
284 code = await fsp.readFile(file, class="st">'utf-8')
285 debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`)
286 } catch (e) {
287 if (e.code !== class="st">'ENOENT' && e.code !== class="st">'EISDIR') {
288 throw e
289 }
290 }
291 if (code != null && environment.pluginContainer.watcher) {
292 ensureWatchedFile(
293 environment.pluginContainer.watcher,
294 file,

Callers 1

doTransformFunction · 0.85

Calls 15

prettifyUrlFunction · 0.90
cleanUrlFunction · 0.90
isFileLoadingAllowedFunction · 0.90
slashFunction · 0.90
timeFromFunction · 0.90
ensureWatchedFileFunction · 0.90
extractSourcemapFromFileFunction · 0.90
isObjectFunction · 0.90
checkPublicFileFunction · 0.90
throwClosedServerErrorFunction · 0.90
injectSourcesContentFunction · 0.90

Tested by

no test coverage detected