Skip to content

Commit 91d3661

Browse files
committed
WIP3
1 parent 2e91a21 commit 91d3661

17 files changed

+321
-254
lines changed

lib/AsyncDependenciesBlock.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,34 @@
66
const DependenciesBlock = require("./DependenciesBlock");
77

88
module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
9-
constructor(name, module, loc) {
9+
constructor(name, module, loc, request) {
1010
super();
1111
this.chunkName = name;
12-
this.chunks = null;
12+
this.chunkGroup = undefined;
1313
this.module = module;
1414
this.loc = loc;
15+
this.request = request;
1516
}
16-
get chunk() {
17-
throw new Error("`chunk` was been renamed to `chunks` and is now an array");
18-
}
19-
set chunk(chunk) {
20-
throw new Error("`chunk` was been renamed to `chunks` and is now an array");
21-
}
17+
2218
updateHash(hash) {
2319
hash.update(this.chunkName || "");
24-
hash.update(this.chunks && this.chunks.map((chunk) => {
20+
hash.update(this.chunkGroup && this.chunkGroup.chunks.map(chunk => {
2521
return chunk.id !== null ? chunk.id : "";
2622
}).join(",") || "");
2723
super.updateHash(hash);
2824
}
25+
2926
disconnect() {
30-
this.chunks = null;
27+
this.chunkGroup = undefined;
3128
super.disconnect();
3229
}
30+
3331
unseal() {
34-
this.chunks = null;
32+
this.chunkGroup = undefined;
3533
super.unseal();
3634
}
35+
3736
sortItems() {
3837
super.sortItems();
39-
if(this.chunks) {
40-
this.chunks.sort((a, b) => a.compareTo(b));
41-
}
4238
}
4339
};

lib/Chunk.js

Lines changed: 107 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -206,42 +206,11 @@ class Chunk {
206206
}
207207
otherChunk._modules.clear();
208208

209-
for(const parentChunk of otherChunk._parents) {
210-
parentChunk.replaceChunk(otherChunk, this);
209+
for(const chunkGroup of otherChunk._groups) {
210+
chunkGroup.replaceChunk(otherChunk, this);
211211
}
212-
otherChunk._parents.clear();
212+
otherChunk._groups.clear();
213213

214-
for(const chunk of otherChunk._chunks) {
215-
chunk.replaceParentChunk(otherChunk, this);
216-
}
217-
otherChunk._chunks.clear();
218-
219-
for(const b of otherChunk._blocks) {
220-
b.chunks = b.chunks ? b.chunks.map(c => {
221-
return c === otherChunk ? this : c;
222-
}) : [this];
223-
b.chunkReason = reason;
224-
this.addBlock(b);
225-
}
226-
otherChunk._blocks.clear();
227-
228-
otherChunk.origins.forEach(origin => {
229-
this.origins.push(origin);
230-
});
231-
for(const b of this._blocks) {
232-
b.chunkReason = reason;
233-
}
234-
this.origins.forEach(origin => {
235-
if(!origin.reasons) {
236-
origin.reasons = [reason];
237-
} else if(origin.reasons[0] !== reason) {
238-
origin.reasons.unshift(reason);
239-
}
240-
});
241-
this._chunks.delete(otherChunk);
242-
this._chunks.delete(this);
243-
this._parents.delete(otherChunk);
244-
this._parents.delete(this);
245214
return true;
246215
}
247216

@@ -314,6 +283,110 @@ class Chunk {
314283
this.sortModules();
315284
}
316285

286+
getChunkMaps(includeInitial, realHash) {
287+
const chunkHashMap = Object.create(null);
288+
const chunkNameMap = Object.create(null);
289+
290+
const queue = new Set(this.groupsIterable);
291+
const chunks = new Set();
292+
293+
for(const chunkGroup of queue) {
294+
if(includeInitial || !chunkGroup.isInitial())
295+
for(const chunk of chunkGroup.chunks)
296+
chunks.add(chunk);
297+
for(const child of chunkGroup.childrenIterable)
298+
queue.add(child);
299+
}
300+
301+
for(const chunk of chunks) {
302+
chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
303+
if(chunk.name)
304+
chunkNameMap[chunk.id] = chunk.name;
305+
}
306+
307+
return {
308+
hash: chunkHashMap,
309+
name: chunkNameMap
310+
};
311+
}
312+
313+
getChunkModuleMaps(includeInitial, filterFn) {
314+
const chunkModuleIdMap = Object.create(null);
315+
const chunkModuleHashMap = Object.create(null);
316+
317+
const queue = new Set(this.groupsIterable);
318+
const chunks = new Set();
319+
320+
for(const chunkGroup of queue) {
321+
if(includeInitial || !chunkGroup.isInitial())
322+
for(const chunk of chunkGroup.chunks)
323+
chunks.add(chunk);
324+
for(const child of chunkGroup.childrenIterable)
325+
queue.add(child);
326+
}
327+
328+
for(const chunk of chunks) {
329+
let array;
330+
for(const module of chunk.modulesIterable) {
331+
if(filterFn(module)) {
332+
if(array === undefined) {
333+
array = [];
334+
chunkModuleIdMap[chunk.id] = array;
335+
}
336+
array.push(module.id);
337+
chunkModuleHashMap[module.id] = module.renderedHash;
338+
}
339+
}
340+
if(array !== undefined) {
341+
array.sort();
342+
}
343+
}
344+
345+
return {
346+
id: chunkModuleIdMap,
347+
hash: chunkModuleHashMap
348+
};
349+
}
350+
351+
hasModuleInGraph(filterFn, filterChunkFn) {
352+
const queue = new Set(this.groupsIterable);
353+
const chunksProcessed = new Set();
354+
355+
for(const chunkGroup of queue) {
356+
for(const chunk of chunkGroup.chunks) {
357+
if(!chunksProcessed.has(chunk)) {
358+
chunksProcessed.add(chunk);
359+
if(!filterChunkFn || filterChunkFn(chunk)) {
360+
for(const module of chunk.modulesIterable)
361+
if(filterFn(module))
362+
return true;
363+
}
364+
}
365+
}
366+
for(const child of chunkGroup.childrenIterable)
367+
queue.add(child);
368+
}
369+
return false;
370+
}
371+
372+
hasChunkInGraph(filterFn) {
373+
const queue = new Set(this.groupsIterable);
374+
const chunksProcessed = new Set();
375+
376+
for(const chunkGroup of queue) {
377+
for(const chunk of chunkGroup.chunks) {
378+
if(!chunksProcessed.has(chunk)) {
379+
chunksProcessed.add(chunk);
380+
if(filterFn(chunk))
381+
return true;
382+
}
383+
}
384+
for(const child of chunkGroup.childrenIterable)
385+
queue.add(child);
386+
}
387+
return false;
388+
}
389+
317390
toString() {
318391
return `Chunk[${Array.from(this._modules).join()}]`;
319392
}

lib/ChunkGroup.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@ const sortById = (a, b) => {
1616
};
1717

1818
class ChunkGroup {
19-
constructor(name, module, loc) {
19+
constructor(name) {
2020
this.name = name;
2121
this._children = new SortableSet(undefined, sortById);
2222
this._parents = new SortableSet(undefined, sortById);
2323
this._blocks = new SortableSet();
2424
this.chunks = [];
2525
this.origins = [];
26-
if(module) {
27-
this.origins.push({
28-
module,
29-
loc,
30-
name
31-
});
32-
}
3326
}
3427

3528
get debugId() {
@@ -77,6 +70,24 @@ class ChunkGroup {
7770
return true;
7871
}
7972

73+
replaceChunk(oldChunk, newChunk) {
74+
const oldIdx = this.chunks.indexOf(oldChunk);
75+
if(oldIdx < 0) return false;
76+
const newIdx = this.chunks.indexOf(newChunk);
77+
if(newIdx < 0) {
78+
this.chunks.splice(oldIdx, 1, newChunk);
79+
return true;
80+
}
81+
if(newIdx < oldIdx) {
82+
this.chunks.splice(oldIdx, 1);
83+
return true;
84+
} else {
85+
this.chunks.splice(oldIdx, 1, newChunk);
86+
this.chunks.splice(newIdx, 1);
87+
return true;
88+
}
89+
}
90+
8091
isInitial() {
8192
return false;
8293
}
@@ -194,11 +205,12 @@ class ChunkGroup {
194205
return false;
195206
}
196207

197-
addOrigin(module, loc) {
208+
addOrigin(module, loc, request) {
209+
if(!module) throw new Error("No origin module");
198210
this.origins.push({
199211
module,
200212
loc,
201-
name: this.name
213+
request
202214
});
203215
}
204216

@@ -269,12 +281,12 @@ class ChunkGroup {
269281

270282
checkConstraints() {
271283
const chunk = this;
272-
for(const child of chunk._chunks) {
284+
for(const child of chunk._children) {
273285
if(!child._parents.has(chunk))
274286
throw new Error(`checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`);
275287
}
276288
for(const parentChunk of chunk._parents) {
277-
if(!parentChunk._chunks.has(chunk))
289+
if(!parentChunk._children.has(chunk))
278290
throw new Error(`checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`);
279291
}
280292
}

0 commit comments

Comments
 (0)