@@ -35,6 +35,9 @@ class Collection {
3535 /** @type {Compilation } */
3636 static compilation ;
3737
38+ /** @type {AssetEntry } */
39+ static assetEntry ;
40+
3841 static assets = new Map ( ) ;
3942
4043 /** @type {Map<string, {entry: AssetEntryOptions, assets: Array<{}>} > } Entries data */
@@ -330,10 +333,12 @@ class Collection {
330333
331334 /**
332335 * @param {Compilation } compilation
336+ * @param {AssetEntry } assetEntry
333337 * @param {HtmlBundlerPlugin.Hooks } hooks
334338 */
335- static init ( compilation , hooks ) {
339+ static init ( { compilation, assetEntry , hooks } ) {
336340 this . compilation = compilation ;
341+ this . assetEntry = assetEntry ;
337342 this . hooks = hooks ;
338343 }
339344
@@ -1005,41 +1010,6 @@ class Collection {
10051010 } ) ;
10061011 }
10071012
1008- // TODO: add hook for addHeadTags: beforeStyles, beforeScripts, endHead
1009- // let res = this.hooks.insertHeadTags.call(content);
1010- // if (res && Array.isArray(res.tags)) {
1011- // let headTags = res.tags;
1012- // let pos = res.pos;
1013- // let sep = Option.isMinify() ? '' : '\n';
1014- // let headBlock = headTags.join(sep);
1015- //
1016- // let startPos;
1017- // let headEndPos = content.indexOf('</head>');
1018- // let headEndPosRegExp = /<\/head>/;
1019- // let posRegexp;
1020- //
1021- // if (!isNumber(pos)) {
1022- // switch (pos) {
1023- // case 'beforePreload':
1024- // posRegexp = /<link.+rel="preload"/;
1025- // break;
1026- // case 'beforeStyles':
1027- // posRegexp = /<link.+rel=".*stylesheet.*"/;
1028- // break;
1029- // case 'beforeScripts':
1030- // posRegexp = /<script/;
1031- // break;
1032- // case 'endHead':
1033- // // through
1034- // case 'default':
1035- // startPos;
1036- // break;
1037- // }
1038- //
1039- // const match = posRegexp.exec(content);
1040- // }
1041- // }
1042-
10431013 // 9. beforeEmit hook allows plugins to change the html after chunks and inlined assets are injected
10441014 promise = promise . then ( ( content ) => hooks . beforeEmit . promise ( content , compileEntry ) || content ) ;
10451015
@@ -1118,17 +1088,44 @@ class Collection {
11181088 this . importStyleSources . clear ( ) ;
11191089 }
11201090
1091+ /* istanbul ignore next: test it manual using `cache.type` as `filesystem` after 2nd run the same project */
1092+ /**
1093+ * Called by first start or after changes.
1094+ *
1095+ * @param {Function } write The serialize function.
1096+ */
11211097 static serialize ( { write } ) {
1098+ for ( let [ , { entry } ] of this . data ) {
1099+ // note: set the function properties as null to able the serialization of the entry object,
1100+ // the original functions will be recovered by deserialization from the cached object `AssetEntry`
1101+ entry . filenameFn = null ;
1102+ entry . filenameTemplate = null ;
1103+ }
1104+
11221105 write ( this . assets ) ;
11231106 write ( this . data ) ;
11241107 }
11251108
1109+ /* istanbul ignore next: test it manual using `cache.type` as `filesystem` after 2nd run the same project */
1110+ /**
1111+ * @param {Function } read The deserialize function.
1112+ */
11261113 static deserialize ( { read } ) {
11271114 this . assets = read ( ) ;
11281115 this . data = read ( ) ;
1116+
1117+ for ( let [ , { entry } ] of this . data ) {
1118+ const cachedEntry = this . assetEntry . entriesById . get ( entry . id ) ;
1119+
1120+ // recovery original not serializable functions from the object cached in the memory
1121+ entry . filenameFn = cachedEntry . filenameFn ;
1122+ entry . filenameTemplate = cachedEntry . filenameTemplate ;
1123+ }
1124+
11291125 this . isDeserialized = true ;
11301126 }
11311127
1128+ /* istanbul ignore next: test it manual using `cache.type` as `filesystem` after 2nd run the same project */
11321129 /**
11331130 * Add the script files loaded in the template to the compilation after deserialization.
11341131 *
@@ -1140,45 +1137,13 @@ class Collection {
11401137 */
11411138 static addToCompilationDeserializedFiles ( issuer ) {
11421139 for ( const [ resource , item ] of this . assets ) {
1143- const { isCompiled , type, name, entries } = item ;
1144- if ( ! isCompiled && type === this . type . script && entries . has ( issuer ) ) {
1145- item . isCompiled = true ;
1140+ const { type, name, entries } = item ;
1141+
1142+ if ( type === this . type . script && entries . has ( issuer ) ) {
11461143 this . #addToCompilation( { name, resource, issuer } ) ;
11471144 }
11481145 }
11491146 }
1150-
1151- /**
1152- * TODO: Reserved for debug.
1153- */
1154- // static getResourceIssuers(resource) {
1155- // const item = this.assets.get(resource);
1156- //
1157- // if (!item) return [];
1158- //
1159- // const issuers = Array.from(item.entries.keys());
1160- //
1161- // // extract from all issuer's request only filename, w/o a query
1162- // issuers.forEach((request, index) => {
1163- // let [file] = request.split('?', 1);
1164- // issuers[index] = file;
1165- // });
1166- //
1167- // return issuers;
1168- // }
1169-
1170- /**
1171- * TODO: Reserved for debug.
1172- */
1173- // static getCompilationModule(resource) {
1174- // const { modules } = this.compilation;
1175- // for (const module of modules) {
1176- // if (module.resource === resource) {
1177- // return module;
1178- // }
1179- // }
1180- // return null;
1181- // }
11821147}
11831148
11841149module . exports = Collection ;
0 commit comments