@@ -22,6 +22,7 @@ import {
2222import { transformWithEsbuild } from '../plugins/esbuild'
2323import { esbuildDepPlugin } from './esbuildDepPlugin'
2424import { scanImports } from './scan'
25+ export { initDepsOptimizer , getDepsOptimizer } from './optimizer'
2526
2627export const debuggerViteDeps = createDebugger ( 'vite:deps' )
2728const debug = debuggerViteDeps
@@ -38,10 +39,15 @@ export type ExportsData = ReturnType<typeof parse> & {
3839 jsxLoader ?: true
3940}
4041
41- export interface OptimizedDeps {
42+ export interface DepsOptimizer {
4243 metadata : DepOptimizationMetadata
4344 scanProcessing ?: Promise < void >
4445 registerMissingImport : ( id : string , resolved : string ) => OptimizedDepInfo
46+ run : ( ) => void
47+ isOptimizedDepFile : ( id : string ) => boolean
48+ isOptimizedDepUrl : ( url : string ) => boolean
49+ getOptimizedDepId : ( depInfo : OptimizedDepInfo ) => string
50+ options : DepOptimizationOptions
4551}
4652
4753export interface DepOptimizationOptions {
@@ -107,11 +113,13 @@ export interface DepOptimizationOptions {
107113 */
108114 extensions ?: string [ ]
109115 /**
110- * Disables dependencies optimizations
116+ * Disables dependencies optimizations, true disables the optimizer during
117+ * build and dev. Pass 'build' or 'dev' to only disable the optimizer in
118+ * one of the modes. Deps optimization is enabled by default in both
111119 * @default false
112120 * @experimental
113121 */
114- disabled ?: boolean
122+ disabled ?: boolean | 'build' | 'dev'
115123}
116124
117125export interface DepOptimizationResult {
@@ -184,7 +192,7 @@ export interface DepOptimizationMetadata {
184192 */
185193export async function optimizeDeps (
186194 config : ResolvedConfig ,
187- force = config . server . force ,
195+ force = config . force ,
188196 asCommand = false
189197) : Promise < DepOptimizationMetadata > {
190198 const log = asCommand ? config . logger . info : debug
@@ -209,7 +217,7 @@ export async function optimizeDeps(
209217 return result . metadata
210218}
211219
212- export function createOptimizedDepsMetadata (
220+ export function initDepsOptimizerMetadata (
213221 config : ResolvedConfig ,
214222 timestamp ?: string
215223) : DepOptimizationMetadata {
@@ -240,7 +248,7 @@ export function addOptimizedDepInfo(
240248 */
241249export function loadCachedDepOptimizationMetadata (
242250 config : ResolvedConfig ,
243- force = config . server . force ,
251+ force = config . force ,
244252 asCommand = false
245253) : DepOptimizationMetadata | undefined {
246254 const log = asCommand ? config . logger . info : debug
@@ -257,7 +265,7 @@ export function loadCachedDepOptimizationMetadata(
257265 let cachedMetadata : DepOptimizationMetadata | undefined
258266 try {
259267 const cachedMetadataPath = path . join ( depsCacheDir , '_metadata.json' )
260- cachedMetadata = parseOptimizedDepsMetadata (
268+ cachedMetadata = parseDepsOptimizerMetadata (
261269 fs . readFileSync ( cachedMetadataPath , 'utf-8' ) ,
262270 depsCacheDir
263271 )
@@ -301,6 +309,21 @@ export async function discoverProjectDependencies(
301309 )
302310 }
303311
312+ return initialProjectDependencies ( config , timestamp , deps )
313+ }
314+
315+ /**
316+ * Create the initial discovered deps list. At build time we only
317+ * have the manually included deps. During dev, a scan phase is
318+ * performed and knownDeps is the list of discovered deps
319+ */
320+ export async function initialProjectDependencies (
321+ config : ResolvedConfig ,
322+ timestamp ?: string ,
323+ knownDeps ?: Record < string , string >
324+ ) : Promise < Record < string , OptimizedDepInfo > > {
325+ const deps : Record < string , string > = knownDeps ?? { }
326+
304327 await addManuallyIncludedOptimizeDeps ( deps , config )
305328
306329 const browserHash = getOptimizedBrowserHash (
@@ -342,16 +365,16 @@ export function depsLogString(qualifiedIds: string[]): string {
342365 * the metadata and start the server without waiting for the optimizeDeps processing to be completed
343366 */
344367export async function runOptimizeDeps (
345- config : ResolvedConfig ,
368+ resolvedConfig : ResolvedConfig ,
346369 depsInfo : Record < string , OptimizedDepInfo >
347370) : Promise < DepOptimizationResult > {
348- config = {
349- ...config ,
371+ const config : ResolvedConfig = {
372+ ...resolvedConfig ,
350373 command : 'build'
351374 }
352375
353- const depsCacheDir = getDepsCacheDir ( config )
354- const processingCacheDir = getProcessingDepsCacheDir ( config )
376+ const depsCacheDir = getDepsCacheDir ( resolvedConfig )
377+ const processingCacheDir = getProcessingDepsCacheDir ( resolvedConfig )
355378
356379 // Create a temporal directory so we don't need to delete optimized deps
357380 // until they have been processed. This also avoids leaving the deps cache
@@ -369,7 +392,7 @@ export async function runOptimizeDeps(
369392 JSON . stringify ( { type : 'module' } )
370393 )
371394
372- const metadata = createOptimizedDepsMetadata ( config )
395+ const metadata = initDepsOptimizerMetadata ( config )
373396
374397 metadata . browserHash = getOptimizedBrowserHash (
375398 metadata . hash ,
@@ -493,7 +516,7 @@ export async function runOptimizeDeps(
493516 const id = path
494517 . relative ( processingCacheDirOutputPath , o )
495518 . replace ( jsExtensionRE , '' )
496- const file = getOptimizedDepPath ( id , config )
519+ const file = getOptimizedDepPath ( id , resolvedConfig )
497520 if (
498521 ! findOptimizedDepInfoInRecord (
499522 metadata . optimized ,
@@ -511,7 +534,7 @@ export async function runOptimizeDeps(
511534 }
512535
513536 const dataPath = path . join ( processingCacheDir , '_metadata.json' )
514- writeFile ( dataPath , stringifyOptimizedDepsMetadata ( metadata , depsCacheDir ) )
537+ writeFile ( dataPath , stringifyDepsOptimizerMetadata ( metadata , depsCacheDir ) )
515538
516539 debug ( `deps bundled in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms` )
517540
@@ -532,7 +555,7 @@ async function addManuallyIncludedOptimizeDeps(
532555) : Promise < void > {
533556 const include = config . optimizeDeps ?. include
534557 if ( include ) {
535- const resolve = config . createResolver ( { asSrc : false } )
558+ const resolve = config . createResolver ( { asSrc : false , scan : true } )
536559 for ( const id of include ) {
537560 // normalize 'foo >bar` as 'foo > bar' to prevent same id being added
538561 // and for pretty printing
@@ -575,11 +598,13 @@ export function getOptimizedDepPath(id: string, config: ResolvedConfig) {
575598}
576599
577600export function getDepsCacheDir ( config : ResolvedConfig ) {
578- return normalizePath ( path . resolve ( config . cacheDir , 'deps' ) )
601+ const dirName = config . command === 'build' ? 'depsBuild' : 'deps'
602+ return normalizePath ( path . resolve ( config . cacheDir , dirName ) )
579603}
580604
581605function getProcessingDepsCacheDir ( config : ResolvedConfig ) {
582- return normalizePath ( path . resolve ( config . cacheDir , 'processing' ) )
606+ const dirName = config . command === 'build' ? 'processingBuild' : 'processing'
607+ return normalizePath ( path . resolve ( config . cacheDir , dirName ) )
583608}
584609
585610export function isOptimizedDepFile ( id : string , config : ResolvedConfig ) {
@@ -605,7 +630,7 @@ export function createIsOptimizedDepUrl(config: ResolvedConfig) {
605630 }
606631}
607632
608- function parseOptimizedDepsMetadata (
633+ function parseDepsOptimizerMetadata (
609634 jsonMetadata : string ,
610635 depsCacheDir : string
611636) : DepOptimizationMetadata | undefined {
@@ -659,7 +684,7 @@ function parseOptimizedDepsMetadata(
659684 * the next time the server start we need to use the global
660685 * browserHash to allow long term caching
661686 */
662- function stringifyOptimizedDepsMetadata (
687+ function stringifyDepsOptimizerMetadata (
663688 metadata : DepOptimizationMetadata ,
664689 depsCacheDir : string
665690) {
0 commit comments