@@ -30,69 +30,49 @@ const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
3030/** @typedef {{ etag: string, data: InstanceType<RestoreProvidedData> } } MemCacheEntry */
3131
3232/**
33+ * Cache key fragment for what changes provided exports without changing the
34+ * module's source: still-lazy barrel requests and DefinePlugin value versions.
3335 * @param {Module } module the module
3436 * @param {LazyBarrelController } lazyBarrelController the compilation's lazy barrel controller
3537 * @param {HashFunction } hashFunction hash function for long keys
36- * @returns {string } the hash
38+ * @returns {string } etag for provided-exports mem/persistent cache, "" when neither applies
3739 */
38- const getLazyRequestsHash = ( module , lazyBarrelController , hashFunction ) => {
39- const lazyKeys = lazyBarrelController . getLazyRequests ( module ) ;
40- if ( ! lazyKeys ) return "" ;
41- const joined = [ ...lazyKeys ] . join ( "|" ) ;
42- if ( joined . length < 100 ) return joined ;
43- const hash = createHash ( hashFunction ) ;
44- hash . update ( joined ) ;
45- return /** @type {string } */ ( hash . digest ( "hex" ) ) ;
46- } ;
47-
48- /**
49- * DefinePlugin (and similar) can change provided exports without changing the
50- * source hash; fold valueDependencies into the provided-exports cache key.
51- * @param {Module } module the module
52- * @param {HashFunction } hashFunction hash function for long keys
53- * @returns {string } key fragment, or "" when absent
54- */
55- const getValueDependenciesHash = ( module , hashFunction ) => {
56- const valueDependencies =
57- /** @type {BuildInfo | undefined } */
58- ( module . buildInfo ) &&
59- /** @type {BuildInfo } */
60- ( module . buildInfo ) . valueDependencies ;
61- if ( ! valueDependencies || valueDependencies . size === 0 ) return "" ;
62- const parts = [ ] ;
63- for ( const key of [ ...valueDependencies . keys ( ) ] . sort ( ) ) {
64- const value = valueDependencies . get ( key ) ;
65- parts . push (
66- `${ key } =${
67- typeof value === "string"
68- ? value
69- : value === undefined
70- ? ""
71- : `[${ [ ...value ] . sort ( ) . join ( "," ) } ]`
72- } `
73- ) ;
40+ const getProvidedExportsEtag = ( module , lazyBarrelController , hashFunction ) => {
41+ const lazyRequests = lazyBarrelController . getLazyRequests ( module ) ;
42+ const buildInfo = /** @type {BuildInfo | undefined } */ ( module . buildInfo ) ;
43+ const valueDependencies = buildInfo && buildInfo . valueDependencies ;
44+ const hasValueDependencies =
45+ valueDependencies !== undefined && valueDependencies . size > 0 ;
46+ if ( ! lazyRequests && ! hasValueDependencies ) return "" ;
47+ /** @type {(string | string[] | null)[][] } */
48+ const valueEntries = [ ] ;
49+ if ( hasValueDependencies ) {
50+ const versions =
51+ /** @type {NonNullable<BuildInfo["valueDependencies"]> } */
52+ ( valueDependencies ) ;
53+ for ( const key of [ ...versions . keys ( ) ] . sort ( ) ) {
54+ const value = versions . get ( key ) ;
55+ valueEntries . push ( [
56+ key ,
57+ value === undefined
58+ ? null
59+ : typeof value === "string"
60+ ? value
61+ : [ ...value ] . sort ( )
62+ ] ) ;
63+ }
7464 }
75- const joined = parts . join ( "|" ) ;
65+ // JSON, so a separator inside a request or a version cannot forge another entry
66+ const joined = JSON . stringify ( [
67+ lazyRequests ? [ ...lazyRequests ] : [ ] ,
68+ valueEntries
69+ ] ) ;
7670 if ( joined . length < 100 ) return joined ;
7771 const hash = createHash ( hashFunction ) ;
7872 hash . update ( joined ) ;
7973 return /** @type {string } */ ( hash . digest ( "hex" ) ) ;
8074} ;
8175
82- /**
83- * @param {Module } module the module
84- * @param {LazyBarrelController } lazyBarrelController the compilation's lazy barrel controller
85- * @param {HashFunction } hashFunction hash function for long keys
86- * @returns {string } etag for provided-exports mem/persistent cache
87- */
88- const getProvidedExportsEtag = ( module , lazyBarrelController , hashFunction ) => {
89- const lazy = getLazyRequestsHash ( module , lazyBarrelController , hashFunction ) ;
90- const valueDeps = getValueDependenciesHash ( module , hashFunction ) ;
91- if ( ! lazy ) return valueDeps ;
92- if ( ! valueDeps ) return lazy ;
93- return `${ lazy } |${ valueDeps } ` ;
94- } ;
95-
9676class FlagDependencyExportsPlugin {
9777 /**
9878 * Applies the plugin by registering its hooks on the compiler.
0 commit comments