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

Function optimizerResolvePlugin

packages/vite/src/node/plugins/resolve.ts:389–491  ·  view source on GitHub ↗
(
  resolveOptions: ResolvePluginOptionsWithOverrides,
)

Source from the content-addressed store, hash-verified

387}
388
389function optimizerResolvePlugin(
390 resolveOptions: ResolvePluginOptionsWithOverrides,
391): Plugin {
392 const { root, asSrc } = resolveOptions
393
394 return {
395 name: 'vite:resolve-dev',
396 applyToEnvironment(environment) {
397 return (
398 !environment.config.isBundled &&
399 !isDepOptimizationDisabled(environment.config.optimizeDeps)
400 )
401 },
402 resolveId: {
403 filter: {
404 id: {
405 exclude: [
406 /^\0/,
407 /^virtual:/,
408 // When injected directly in html/client code
409 /^\/virtual:/,
410 /^__vite-/,
411 ],
412 },
413 },
414 async handler(id, importer, resolveOpts) {
415 // The resolve plugin is used for createIdResolver and the depsOptimizer should be
416 // disabled in that case, so deps optimization is opt-in when creating the plugin.
417 const depsOptimizer =
418 resolveOptions.optimizeDeps && this.environment.mode === 'dev'
419 ? this.environment.depsOptimizer
420 : undefined
421 if (!depsOptimizer) {
422 return
423 }
424
425 const options: InternalResolveOptions = {
426 isRequire: resolveOpts.kind === 'require-call',
427 ...this.environment.config.resolve,
428 ...resolveOptions,
429 scan: resolveOpts.scan ?? resolveOptions.scan,
430 }
431 options.preferRelative ||= importer?.endsWith('.html')
432
433 // resolve pre-bundled deps requests, these could be resolved by
434 // tryFileResolve or /fs/ resolution but these files may not yet
435 // exists if we are in the middle of a deps re-processing
436 if (asSrc && depsOptimizer.isOptimizedDepUrl(id)) {
437 const optimizedPath = id.startsWith(FS_PREFIX)
438 ? fsPathFromId(id)
439 : normalizePath(path.resolve(root, id.slice(1)))
440 return optimizedPath
441 }
442
443 if (!isDataUrl(id) && !isExternalUrl(id)) {
444 if (
445 id[0] === '.' ||
446 (options.preferRelative && startsWithWordCharRE.test(id))

Callers 1

oxcResolvePluginFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected