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

Function createLoadHandler

packages/vite/src/node/optimizer/pluginConverter.ts:253–301  ·  view source on GitHub ↗
(
  options: esbuild.OnLoadOptions,
  callback: EsbuildOnLoadCallback,
)

Source from the content-addressed store, hash-verified

251}
252
253function createLoadHandler(
254 options: esbuild.OnLoadOptions,
255 callback: EsbuildOnLoadCallback,
256): LoadHandler {
257 const textDecoder = new TextDecoder()
258 return async function (id) {
259 const [idWithoutNamespace, idNamespace] = idToPathAndNamespace(id)
260 if (
261 options.namespace !== undefined &&
262 options.namespace !== 'file' &&
263 options.namespace !== idNamespace
264 ) {
265 return
266 }
267 if (options.filter !== undefined && !options.filter.test(id)) {
268 return
269 }
270
271 const result = await callback.call(this, {
272 path: idWithoutNamespace,
273 namespace: idNamespace,
274 suffix: '',
275 pluginData: {},
276 with: {},
277 })
278 if (!result) return
279 if (result.errors && result.errors.length > 0) {
280 throw new AggregateError(result.errors)
281 }
282 if (
283 (result.warnings && result.warnings.length > 0) ||
284 (result.watchDirs && result.watchDirs.length > 0) ||
285 result.contents == null
286 ) {
287 throw new Error('not implemented')
288 }
289 for (const file of result.watchFiles ?? []) {
290 this.addWatchFile(file)
291 }
292
293 return {
294 code:
295 typeof result.contents === 'string'
296 ? result.contents
297 : textDecoder.decode(result.contents),
298 moduleType: result.loader,
299 }
300 }
301}
302
303function idToPathAndNamespace(id: string): [path: string, namespace: string]
304function idToPathAndNamespace(

Callers 1

setupFunction · 0.85

Calls 2

idToPathAndNamespaceFunction · 0.85
addWatchFileMethod · 0.45

Tested by

no test coverage detected