@@ -24,6 +24,9 @@ import { performance } from 'perf_hooks'
24
24
const debug = createDebugger ( 'vite:deps' )
25
25
const isDebugEnabled = _debug ( 'vite:deps' ) . enabled
26
26
27
+ const jsExtensionRE = / \. j s $ / i
28
+ const jsMapExtensionRE = / \. j s \. m a p $ / i
29
+
27
30
export type ExportsData = ReturnType < typeof parse > & {
28
31
// es-module-lexer has a facade detection but isn't always accurate for our
29
32
// use case when the module has default export
@@ -125,7 +128,7 @@ export interface OptimizedDepInfo {
125
128
* During optimization, ids can still be resolved to their final location
126
129
* but the bundles may not yet be saved to disk
127
130
*/
128
- processing : Promise < void >
131
+ processing ? : Promise < void >
129
132
}
130
133
131
134
export interface DepOptimizationMetadata {
@@ -144,6 +147,10 @@ export interface DepOptimizationMetadata {
144
147
* Metadata for each already optimized dependency
145
148
*/
146
149
optimized : Record < string , OptimizedDepInfo >
150
+ /**
151
+ * Metadata for non-entry optimized chunks and dynamic imports
152
+ */
153
+ chunks : Record < string , OptimizedDepInfo >
147
154
/**
148
155
* Metadata for each newly discovered dependency after processing
149
156
*/
@@ -213,6 +220,7 @@ export async function createOptimizeDepsRun(
213
220
hash : mainHash ,
214
221
browserHash : mainHash ,
215
222
optimized : { } ,
223
+ chunks : { } ,
216
224
discovered : { }
217
225
}
218
226
@@ -222,8 +230,7 @@ export async function createOptimizeDepsRun(
222
230
const prevDataPath = path . join ( depsCacheDir , '_metadata.json' )
223
231
prevData = parseOptimizedDepsMetadata (
224
232
fs . readFileSync ( prevDataPath , 'utf-8' ) ,
225
- depsCacheDir ,
226
- processing . promise
233
+ depsCacheDir
227
234
)
228
235
} catch ( e ) { }
229
236
// hash is consistent, no need to re-bundle
@@ -490,7 +497,9 @@ export async function createOptimizeDepsRun(
490
497
processingCacheDirOutputPath
491
498
)
492
499
const output =
493
- meta . outputs [ path . relative ( process . cwd ( ) , optimizedInfo . file ) ]
500
+ meta . outputs [
501
+ path . relative ( process . cwd ( ) , getProcessingDepPath ( id , config ) )
502
+ ]
494
503
if ( output ) {
495
504
// We only need to hash the output.imports in to check for stability, but adding the hash
496
505
// and file path gives us a unique hash that may be useful for other things in the future
@@ -518,6 +527,25 @@ export async function createOptimizeDepsRun(
518
527
debug ( `optimized deps have altered files: ${ alteredFiles } ` )
519
528
}
520
529
530
+ for ( const o of Object . keys ( meta . outputs ) ) {
531
+ if ( ! o . match ( jsMapExtensionRE ) ) {
532
+ const id = path
533
+ . relative ( processingCacheDirOutputPath , o )
534
+ . replace ( jsExtensionRE , '' )
535
+ const file = getOptimizedDepPath ( id , config )
536
+ if ( ! findFileInfo ( metadata . optimized , file ) ) {
537
+ metadata . chunks [ id ] = {
538
+ file,
539
+ src : '' ,
540
+ needsInterop : false ,
541
+ browserHash :
542
+ ( ! alteredFiles && currentData ?. chunks [ id ] ?. browserHash ) ||
543
+ newBrowserHash
544
+ }
545
+ }
546
+ }
547
+ }
548
+
521
549
if ( alteredFiles ) {
522
550
metadata . browserHash = newBrowserHash
523
551
}
@@ -615,19 +643,12 @@ export function depsFromOptimizedDepInfo(
615
643
)
616
644
}
617
645
618
- function getHash ( text : string ) {
646
+ export function getHash ( text : string ) {
619
647
return createHash ( 'sha256' ) . update ( text ) . digest ( 'hex' ) . substring ( 0 , 8 )
620
648
}
621
649
622
- export function getOptimizedBrowserHash (
623
- hash : string ,
624
- deps : Record < string , string > ,
625
- missing ?: Record < string , string >
626
- ) {
627
- // update browser hash
628
- return getHash (
629
- hash + JSON . stringify ( deps ) + ( missing ? JSON . stringify ( missing ) : '' )
630
- )
650
+ function getOptimizedBrowserHash ( hash : string , deps : Record < string , string > ) {
651
+ return getHash ( hash + JSON . stringify ( deps ) )
631
652
}
632
653
633
654
function getCachedDepFilePath ( id : string , depsCacheDir : string ) {
@@ -642,7 +663,15 @@ export function getDepsCacheDir(config: ResolvedConfig) {
642
663
return normalizePath ( path . resolve ( config . cacheDir , 'deps' ) )
643
664
}
644
665
645
- export function getProcessingDepsCacheDir ( config : ResolvedConfig ) {
666
+ function getProcessingDepFilePath ( id : string , processingCacheDir : string ) {
667
+ return normalizePath ( path . resolve ( processingCacheDir , flattenId ( id ) + '.js' ) )
668
+ }
669
+
670
+ function getProcessingDepPath ( id : string , config : ResolvedConfig ) {
671
+ return getProcessingDepFilePath ( id , getProcessingDepsCacheDir ( config ) )
672
+ }
673
+
674
+ function getProcessingDepsCacheDir ( config : ResolvedConfig ) {
646
675
return normalizePath ( path . resolve ( config . cacheDir , 'processing' ) )
647
676
}
648
677
@@ -671,8 +700,7 @@ export function createIsOptimizedDepUrl(config: ResolvedConfig) {
671
700
672
701
function parseOptimizedDepsMetadata (
673
702
jsonMetadata : string ,
674
- depsCacheDir : string ,
675
- processing : Promise < void >
703
+ depsCacheDir : string
676
704
) {
677
705
const metadata = JSON . parse ( jsonMetadata , ( key : string , value : string ) => {
678
706
// Paths can be absolute or relative to the deps cache dir where
@@ -682,25 +710,69 @@ function parseOptimizedDepsMetadata(
682
710
}
683
711
return value
684
712
} )
713
+ const { browserHash } = metadata
685
714
for ( const o of Object . keys ( metadata . optimized ) ) {
686
- metadata . optimized [ o ] . processing = processing
715
+ const depInfo = metadata . optimized [ o ]
716
+ depInfo . browserHash = browserHash
717
+ }
718
+ metadata . chunks ||= { } // Support missing chunks for back compat
719
+ for ( const o of Object . keys ( metadata . chunks ) ) {
720
+ const depInfo = metadata . chunks [ o ]
721
+ depInfo . src = ''
722
+ depInfo . browserHash = browserHash
687
723
}
688
- return { ...metadata , discovered : { } }
724
+ metadata . discovered = { }
725
+ return metadata
689
726
}
690
727
728
+ /**
729
+ * Stringify metadata for deps cache. Remove processing promises
730
+ * and individual dep info browserHash. Once the cache is reload
731
+ * the next time the server start we need to use the global
732
+ * browserHash to allow long term caching
733
+ */
691
734
function stringifyOptimizedDepsMetadata (
692
735
metadata : DepOptimizationMetadata ,
693
736
depsCacheDir : string
694
737
) {
695
738
return JSON . stringify (
696
739
metadata ,
697
740
( key : string , value : any ) => {
698
- if ( key === 'processing ' || key === 'discovered ' ) {
741
+ if ( key === 'discovered ' || key === 'processing ' ) {
699
742
return
700
743
}
701
744
if ( key === 'file' || key === 'src' ) {
702
745
return normalizePath ( path . relative ( depsCacheDir , value ) )
703
746
}
747
+ if ( key === 'optimized' ) {
748
+ // Only remove browserHash for individual dep info
749
+ const cleaned : Record < string , object > = { }
750
+ for ( const dep of Object . keys ( value ) ) {
751
+ const { browserHash, ...c } = value [ dep ]
752
+ cleaned [ dep ] = c
753
+ }
754
+ return cleaned
755
+ }
756
+ if ( key === 'optimized' ) {
757
+ return Object . keys ( value ) . reduce (
758
+ ( cleaned : Record < string , object > , dep : string ) => {
759
+ const { browserHash, ...c } = value [ dep ]
760
+ cleaned [ dep ] = c
761
+ return cleaned
762
+ } ,
763
+ { }
764
+ )
765
+ }
766
+ if ( key === 'chunks' ) {
767
+ return Object . keys ( value ) . reduce (
768
+ ( cleaned : Record < string , object > , dep : string ) => {
769
+ const { browserHash, needsInterop, src, ...c } = value [ dep ]
770
+ cleaned [ dep ] = c
771
+ return cleaned
772
+ } ,
773
+ { }
774
+ )
775
+ }
704
776
return value
705
777
} ,
706
778
2
@@ -797,7 +869,8 @@ export function optimizeDepInfoFromFile(
797
869
) : OptimizedDepInfo | undefined {
798
870
return (
799
871
findFileInfo ( metadata . optimized , file ) ||
800
- findFileInfo ( metadata . discovered , file )
872
+ findFileInfo ( metadata . discovered , file ) ||
873
+ findFileInfo ( metadata . chunks , file )
801
874
)
802
875
}
803
876
0 commit comments