Skip to content

Commit 2e91a21

Browse files
committed
WIP2
1 parent 761f212 commit 2e91a21

8 files changed

+170
-130
lines changed

lib/Chunk.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"use strict";
66

77
const util = require("util");
8-
const compareLocations = require("./compareLocations");
9-
const ChunkGroup = require("./ChunkGroup");
108
const SortableSet = require("./util/SortableSet");
119
let debugId = 1000;
1210

@@ -78,15 +76,19 @@ class Chunk {
7876
}
7977

8078
hasRuntime() {
81-
for(const entrypoint of this._entrypoints) {
79+
for(const chunkGroup of this._groups) {
8280
// We only need to check the first one
83-
return entrypoint.getRuntimeChunkGroup() === this;
81+
return chunkGroup.isInitial() && chunkGroup.getRuntimeChunk() === this;
8482
}
8583
return false;
8684
}
8785

8886
isInitial() {
89-
return !!this.entrypoint;
87+
for(const chunkGroup of this._groups) {
88+
// We only need to check the first one
89+
return chunkGroup.isInitial();
90+
}
91+
return false;
9092
}
9193

9294
hasEntryModule() {
@@ -129,6 +131,25 @@ class Chunk {
129131
return Array.from(this._modules, fn);
130132
}
131133

134+
addGroup(chunkGroup) {
135+
if(this._groups.has(chunkGroup))
136+
return false;
137+
this._groups.add(chunkGroup);
138+
return true;
139+
}
140+
141+
isInGroup(chunkGroup) {
142+
return this._groups.has(chunkGroup);
143+
}
144+
145+
getNumberOfGroups() {
146+
return this._groups.siz;
147+
}
148+
149+
get groupsIterable() {
150+
return this._groups;
151+
}
152+
132153
compareTo(otherChunk) {
133154
this._modules.sort();
134155
otherChunk._modules.sort();
@@ -291,21 +312,6 @@ class Chunk {
291312

292313
sortItems(sortChunks) {
293314
this.sortModules();
294-
this.origins.sort((a, b) => {
295-
const aIdent = a.module.identifier();
296-
const bIdent = b.module.identifier();
297-
if(aIdent < bIdent) return -1;
298-
if(aIdent > bIdent) return 1;
299-
return compareLocations(a.loc, b.loc);
300-
});
301-
this.origins.forEach(origin => {
302-
if(origin.reasons)
303-
origin.reasons.sort();
304-
});
305-
if(sortChunks) {
306-
this._parents.sort();
307-
this._chunks.sort();
308-
}
309315
}
310316

311317
toString() {

lib/ChunkGroup.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"use strict";
66

77
const SortableSet = require("./util/SortableSet");
8+
const compareLocations = require("./compareLocations");
89

910
const getArray = set => Array.from(set);
1011

@@ -44,9 +45,11 @@ class ChunkGroup {
4445
if(oldIdx > 0) {
4546
this.chunks.splice(oldIdx, 1);
4647
this.chunks.unshift(chunk);
47-
} else if(oldIdx < 0 && chunk.addEntrypoint(this)) {
48+
} else if(oldIdx < 0) {
4849
this.chunks.unshift(chunk);
50+
return true;
4951
}
52+
return false;
5053
}
5154

5255
insertChunk(chunk, before) {
@@ -60,8 +63,18 @@ class ChunkGroup {
6063
this.chunks.splice(idx, 0, chunk);
6164
} else if(oldIdx < 0) {
6265
this.chunks.splice(idx, 0, chunk);
63-
chunk.addEntrypoint(this);
66+
return true;
67+
}
68+
return false;
69+
}
70+
71+
pushChunk(chunk) {
72+
const oldIdx = this.chunks.indexOf(chunk);
73+
if(oldIdx >= 0) {
74+
return false;
6475
}
76+
this.chunks.push(chunk);
77+
return true;
6578
}
6679

6780
isInitial() {
@@ -189,18 +202,12 @@ class ChunkGroup {
189202
});
190203
}
191204

192-
replaceChild(oldChunkGroup, newChunkGroup) {
193-
this._children.delete(oldChunkGroup);
194-
if(this !== newChunkGroup && newChunkGroup.addParent(this)) {
195-
this.addChild(newChunkGroup);
196-
}
197-
}
198-
199-
replaceParent(oldParentChunkGroup, newParentChunkGroup) {
200-
this._parents.delete(oldParentChunkGroup);
201-
if(this !== newParentChunkGroup && newParentChunkGroup.addChunk(this)) {
202-
this.addParent(newParentChunkGroup);
205+
containsModule(module) {
206+
for(const chunk of this.chunks) {
207+
if(chunk.containsModule(module))
208+
return true;
203209
}
210+
return false;
204211
}
205212

206213
remove(reason) {
@@ -244,6 +251,22 @@ class ChunkGroup {
244251
}
245252
}
246253

254+
sortItems() {
255+
this.origins.sort((a, b) => {
256+
const aIdent = a.module.identifier();
257+
const bIdent = b.module.identifier();
258+
if(aIdent < bIdent) return -1;
259+
if(aIdent > bIdent) return 1;
260+
return compareLocations(a.loc, b.loc);
261+
});
262+
this.origins.forEach(origin => {
263+
if(origin.reasons)
264+
origin.reasons.sort();
265+
});
266+
this._parents.sort();
267+
this._children.sort();
268+
}
269+
247270
checkConstraints() {
248271
const chunk = this;
249272
for(const child of chunk._chunks) {

lib/Compilation.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const Semaphore = require("./util/Semaphore");
3232
const createHash = require("./util/createHash");
3333
const Queue = require("./util/Queue");
3434
const SortableSet = require("./util/SortableSet");
35+
const GraphHelpers = require("./GraphHelpers");
3536

3637
const byId = (a, b) => {
3738
if(a.id < b.id) return -1;
@@ -214,7 +215,7 @@ class Compilation extends Tapable {
214215

215216
this.entries = [];
216217
this._preparedEntrypoints = [];
217-
this.entrypoints = {};
218+
this.entrypoints = new Map();
218219
this.chunks = [];
219220
this.chunkGroups = [];
220221
this.namedChunkGroups = {};
@@ -749,14 +750,16 @@ class Compilation extends Tapable {
749750
this._preparedEntrypoints.forEach(preparedEntrypoint => {
750751
const module = preparedEntrypoint.module;
751752
const name = preparedEntrypoint.name;
752-
const chunkGroup = this.addChunk(name);
753-
const chunk = chunkGroup.chunks[0];
754-
const entrypoint = this.entrypoints[name] = new Entrypoint(name, chunkGroup);
753+
const chunk = this.addChunk(name);
754+
const entrypoint = new Entrypoint(name);
755+
this.entrypoints.set(name, entrypoint);
755756
this.chunkGroups.push(entrypoint);
756757

757-
chunk.addModule(module);
758-
module.addChunk(chunk);
758+
GraphHelpers.connectChunkGroupAndChunk(entrypoint, chunk);
759+
GraphHelpers.connectChunkAndModule(chunk, module);
760+
759761
chunk.entryModule = module;
762+
760763
this.assignIndex(module);
761764
this.assignDepth(module);
762765
});
@@ -904,13 +907,15 @@ class Compilation extends Tapable {
904907
}
905908
const chunkGroup = new ChunkGroup(name, module, loc);
906909
const chunk = new Chunk(name);
907-
chunkGroup.addChunk(chunk);
910+
911+
GraphHelpers.connectChunkGroupAndChunk(chunkGroup, chunk);
912+
908913
this.chunks.push(chunk);
909914
this.chunkGroups.push(chunkGroup);
910915
if(name) {
911916
this.namedChunkGroups[name] = chunkGroup;
912917
}
913-
return chunk;
918+
return chunkGroup;
914919
}
915920

916921
addChunk() {
@@ -1060,7 +1065,7 @@ class Compilation extends Tapable {
10601065
c = this.namedChunkGroups[b.chunkName];
10611066
if(c && c.isInitial()) {
10621067
this.errors.push(new AsyncDependencyToInitialChunkError(b.chunkName, b.module, b.loc));
1063-
c = chunk;
1068+
c = chunkGroup;
10641069
} else {
10651070
c = this.addChunkInGroup(b.chunkName, b.module, b.loc);
10661071
blockChunkGroups.set(b, c);
@@ -1224,9 +1229,7 @@ class Compilation extends Tapable {
12241229
}
12251230

12261231
// 7. Connect chunk with parent
1227-
if(chunkGroup.addChild(depChunkGroup)) {
1228-
depChunkGroup.addParent(chunkGroup);
1229-
}
1232+
GraphHelpers.connectChunkGroupParentAndChild(chunkGroup, depChunkGroup);
12301233

12311234
nextChunkGroups.add(depChunkGroup);
12321235
}
@@ -1422,6 +1425,10 @@ class Compilation extends Tapable {
14221425
}
14231426

14241427
sortItemsWithChunkIds() {
1428+
for(const chunkGroup of this.chunkGroups) {
1429+
chunkGroup.sortItems();
1430+
}
1431+
14251432
this.chunks.sort(byId);
14261433

14271434
for(let indexModule = 0; indexModule < this.modules.length; indexModule++) {

lib/GraphHelpers.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Chunk = require("./Chunk");
2+
const ChunkGroup = require("./ChunkGroup");
3+
const Module = require("./Module");
4+
5+
// TODO remove this function
6+
function assert(value, Type, name) {
7+
if(value instanceof Type) return;
8+
throw new Error(`${name} is not a ${Type.name}`);
9+
}
10+
11+
exports.connectChunkGroupAndChunk = (chunkGroup, chunk) => {
12+
assert(chunkGroup, ChunkGroup, "chunkGroup");
13+
assert(chunk, Chunk, "chunk");
14+
if(chunkGroup.pushChunk(chunk)) {
15+
chunk.addGroup(chunkGroup);
16+
}
17+
};
18+
19+
exports.connectChunkGroupParentAndChild = (parent, child) => {
20+
assert(parent, ChunkGroup, "parent");
21+
assert(child, ChunkGroup, "child");
22+
if(parent.addChild(child)) {
23+
child.addParent(parent);
24+
}
25+
};
26+
27+
exports.connectChunkAndModule = (chunk, module) => {
28+
assert(chunk, Chunk, "chunk");
29+
assert(module, Module, "module");
30+
if(module.addChunk(chunk)) {
31+
chunk.addModule(module);
32+
}
33+
};

lib/Module.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ class Module extends DependenciesBlock {
135135
}
136136

137137
addChunk(chunk) {
138+
if(this._chunks.has(chunk))
139+
return false;
138140
this._chunks.add(chunk);
141+
return true;
139142
}
140143

141144
removeChunk(chunk) {

lib/RecordIdsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class RecordIdsPlugin {
6666
records.chunks.usedIds = {};
6767
chunks.forEach(chunk => {
6868
const name = chunk.name;
69-
const blockIdents = chunk.mapBlocks(getDepBlockIdent.bind(null, chunk)).filter(Boolean);
69+
const blockIdents = Array.from(chunk.groupsIterable, getChunkGroupIdent.bind(null, chunk)).filter(Boolean);
7070
if(name) records.chunks.byName[name] = chunk.id;
7171
blockIdents.forEach((blockIdent) => {
7272
records.chunks.byBlocks[blockIdent] = chunk.id;

lib/optimize/OccurrenceOrderPlugin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,19 @@ class OccurrenceOrderPlugin {
8181

8282
let i = 0;
8383
chunks.forEach(c => {
84-
const result = c.getParents().reduce((sum, p) => {
85-
if(p.isInitial()) return sum + 1;
86-
return sum;
87-
}, 0);
88-
occursInInitialChunksMap.set(c, result);
84+
let occurs = 0;
85+
for(const chunkGroup of c.groupsIterable) {
86+
for(const parent of chunkGroup.parentsIterable) {
87+
if(parent.isInitial())
88+
occurs++;
89+
}
90+
}
91+
occursInInitialChunksMap.set(c, occurs);
8992
originalOrder.set(c, i++);
9093
});
9194

9295
const occurs = c => {
93-
return c.getNumberOfBlocks();
96+
return c.getNumberOfGroups();
9497
};
9598

9699
chunks.sort((a, b) => {

0 commit comments

Comments
 (0)