@@ -22,6 +22,7 @@ import {
22
22
import { transformWithEsbuild } from '../plugins/esbuild'
23
23
import { esbuildDepPlugin } from './esbuildDepPlugin'
24
24
import { scanImports } from './scan'
25
+ export { initDepsOptimizer , getDepsOptimizer } from './optimizer'
25
26
26
27
export const debuggerViteDeps = createDebugger ( 'vite:deps' )
27
28
const debug = debuggerViteDeps
@@ -38,10 +39,15 @@ export type ExportsData = ReturnType<typeof parse> & {
38
39
jsxLoader ?: true
39
40
}
40
41
41
- export interface OptimizedDeps {
42
+ export interface DepsOptimizer {
42
43
metadata : DepOptimizationMetadata
43
44
scanProcessing ?: Promise < void >
44
45
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
45
51
}
46
52
47
53
export interface DepOptimizationOptions {
@@ -107,11 +113,13 @@ export interface DepOptimizationOptions {
107
113
*/
108
114
extensions ?: string [ ]
109
115
/**
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
111
119
* @default false
112
120
* @experimental
113
121
*/
114
- disabled ?: boolean
122
+ disabled ?: boolean | 'build' | 'dev'
115
123
}
116
124
117
125
export interface DepOptimizationResult {
@@ -184,7 +192,7 @@ export interface DepOptimizationMetadata {
184
192
*/
185
193
export async function optimizeDeps (
186
194
config : ResolvedConfig ,
187
- force = config . server . force ,
195
+ force = config . force ,
188
196
asCommand = false
189
197
) : Promise < DepOptimizationMetadata > {
190
198
const log = asCommand ? config . logger . info : debug
@@ -209,7 +217,7 @@ export async function optimizeDeps(
209
217
return result . metadata
210
218
}
211
219
212
- export function createOptimizedDepsMetadata (
220
+ export function initDepsOptimizerMetadata (
213
221
config : ResolvedConfig ,
214
222
timestamp ?: string
215
223
) : DepOptimizationMetadata {
@@ -240,7 +248,7 @@ export function addOptimizedDepInfo(
240
248
*/
241
249
export function loadCachedDepOptimizationMetadata (
242
250
config : ResolvedConfig ,
243
- force = config . server . force ,
251
+ force = config . force ,
244
252
asCommand = false
245
253
) : DepOptimizationMetadata | undefined {
246
254
const log = asCommand ? config . logger . info : debug
@@ -257,7 +265,7 @@ export function loadCachedDepOptimizationMetadata(
257
265
let cachedMetadata : DepOptimizationMetadata | undefined
258
266
try {
259
267
const cachedMetadataPath = path . join ( depsCacheDir , '_metadata.json' )
260
- cachedMetadata = parseOptimizedDepsMetadata (
268
+ cachedMetadata = parseDepsOptimizerMetadata (
261
269
fs . readFileSync ( cachedMetadataPath , 'utf-8' ) ,
262
270
depsCacheDir
263
271
)
@@ -301,6 +309,21 @@ export async function discoverProjectDependencies(
301
309
)
302
310
}
303
311
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
+
304
327
await addManuallyIncludedOptimizeDeps ( deps , config )
305
328
306
329
const browserHash = getOptimizedBrowserHash (
@@ -342,16 +365,16 @@ export function depsLogString(qualifiedIds: string[]): string {
342
365
* the metadata and start the server without waiting for the optimizeDeps processing to be completed
343
366
*/
344
367
export async function runOptimizeDeps (
345
- config : ResolvedConfig ,
368
+ resolvedConfig : ResolvedConfig ,
346
369
depsInfo : Record < string , OptimizedDepInfo >
347
370
) : Promise < DepOptimizationResult > {
348
- config = {
349
- ...config ,
371
+ const config : ResolvedConfig = {
372
+ ...resolvedConfig ,
350
373
command : 'build'
351
374
}
352
375
353
- const depsCacheDir = getDepsCacheDir ( config )
354
- const processingCacheDir = getProcessingDepsCacheDir ( config )
376
+ const depsCacheDir = getDepsCacheDir ( resolvedConfig )
377
+ const processingCacheDir = getProcessingDepsCacheDir ( resolvedConfig )
355
378
356
379
// Create a temporal directory so we don't need to delete optimized deps
357
380
// until they have been processed. This also avoids leaving the deps cache
@@ -369,7 +392,7 @@ export async function runOptimizeDeps(
369
392
JSON . stringify ( { type : 'module' } )
370
393
)
371
394
372
- const metadata = createOptimizedDepsMetadata ( config )
395
+ const metadata = initDepsOptimizerMetadata ( config )
373
396
374
397
metadata . browserHash = getOptimizedBrowserHash (
375
398
metadata . hash ,
@@ -493,7 +516,7 @@ export async function runOptimizeDeps(
493
516
const id = path
494
517
. relative ( processingCacheDirOutputPath , o )
495
518
. replace ( jsExtensionRE , '' )
496
- const file = getOptimizedDepPath ( id , config )
519
+ const file = getOptimizedDepPath ( id , resolvedConfig )
497
520
if (
498
521
! findOptimizedDepInfoInRecord (
499
522
metadata . optimized ,
@@ -511,7 +534,7 @@ export async function runOptimizeDeps(
511
534
}
512
535
513
536
const dataPath = path . join ( processingCacheDir , '_metadata.json' )
514
- writeFile ( dataPath , stringifyOptimizedDepsMetadata ( metadata , depsCacheDir ) )
537
+ writeFile ( dataPath , stringifyDepsOptimizerMetadata ( metadata , depsCacheDir ) )
515
538
516
539
debug ( `deps bundled in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms` )
517
540
@@ -532,7 +555,7 @@ async function addManuallyIncludedOptimizeDeps(
532
555
) : Promise < void > {
533
556
const include = config . optimizeDeps ?. include
534
557
if ( include ) {
535
- const resolve = config . createResolver ( { asSrc : false } )
558
+ const resolve = config . createResolver ( { asSrc : false , scan : true } )
536
559
for ( const id of include ) {
537
560
// normalize 'foo >bar` as 'foo > bar' to prevent same id being added
538
561
// and for pretty printing
@@ -575,11 +598,13 @@ export function getOptimizedDepPath(id: string, config: ResolvedConfig) {
575
598
}
576
599
577
600
export 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 ) )
579
603
}
580
604
581
605
function 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 ) )
583
608
}
584
609
585
610
export function isOptimizedDepFile ( id : string , config : ResolvedConfig ) {
@@ -605,7 +630,7 @@ export function createIsOptimizedDepUrl(config: ResolvedConfig) {
605
630
}
606
631
}
607
632
608
- function parseOptimizedDepsMetadata (
633
+ function parseDepsOptimizerMetadata (
609
634
jsonMetadata : string ,
610
635
depsCacheDir : string
611
636
) : DepOptimizationMetadata | undefined {
@@ -659,7 +684,7 @@ function parseOptimizedDepsMetadata(
659
684
* the next time the server start we need to use the global
660
685
* browserHash to allow long term caching
661
686
*/
662
- function stringifyOptimizedDepsMetadata (
687
+ function stringifyDepsOptimizerMetadata (
663
688
metadata : DepOptimizationMetadata ,
664
689
depsCacheDir : string
665
690
) {
0 commit comments