Skip to content

Commit f24e389

Browse files
sokraooflorent
authored andcommitted
remove the need to rewrite chunk in reasons
1 parent ae2ae4e commit f24e389

File tree

6 files changed

+48
-134
lines changed

6 files changed

+48
-134
lines changed

lib/Chunk.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ class Chunk {
200200
moveModule(module, otherChunk) {
201201
GraphHelpers.disconnectChunkAndModule(this, module);
202202
GraphHelpers.connectChunkAndModule(otherChunk, module);
203-
module.rewriteChunkInReasons(this, [otherChunk]);
204203
}
205204

206205
integrate(otherChunk, reason) {

lib/Module.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ class Module extends DependenciesBlock {
6464
this.used = null;
6565
this.usedExports = null;
6666
this.optimizationBailout = [];
67-
68-
// delayed operations
69-
this._rewriteChunkInReasons = undefined;
7067
}
7168

7269
get exportsArgument() {
@@ -82,7 +79,6 @@ class Module extends DependenciesBlock {
8279
this.renderedHash = undefined;
8380

8481
this.reasons.length = 0;
85-
this._rewriteChunkInReasons = undefined;
8682
this._chunks.clear();
8783

8884
this.id = null;
@@ -187,36 +183,48 @@ class Module extends DependenciesBlock {
187183
return false;
188184
}
189185

190-
hasReasonForChunk(chunk) {
191-
if (this._rewriteChunkInReasons) {
192-
for (const operation of this._rewriteChunkInReasons)
193-
this._doRewriteChunkInReasons(operation.oldChunk, operation.newChunks);
194-
this._rewriteChunkInReasons = undefined;
195-
}
196-
for (let i = 0; i < this.reasons.length; i++) {
197-
if (this.reasons[i].hasChunk(chunk)) return true;
186+
isAccessibleInChunk(chunk, ignoreChunk) {
187+
// Check if module is accessible in ALL chunk groups
188+
for (const chunkGroup of chunk.groupsIterable) {
189+
if (!this.isAccessibleInChunkGroup(chunkGroup)) return false;
198190
}
199-
return false;
191+
return true;
200192
}
201193

202-
hasReasons() {
203-
return this.reasons.length > 0;
204-
}
194+
isAccessibleInChunkGroup(chunkGroup, ignoreChunk) {
195+
const queue = new Set([chunkGroup]);
205196

206-
rewriteChunkInReasons(oldChunk, newChunks) {
207-
// This is expensive. Delay operation until we really need the data
208-
if (this._rewriteChunkInReasons === undefined)
209-
this._rewriteChunkInReasons = [];
210-
this._rewriteChunkInReasons.push({
211-
oldChunk,
212-
newChunks
213-
});
197+
// Check if module is accessible from all items of the queue
198+
queueFor: for (const cg of queue) {
199+
// 1. If module is in one of the chunks of the group we can continue checking the next items
200+
// because it's accessible.
201+
for (const chunk of cg.chunks) {
202+
if (chunk !== ignoreChunk && chunk.containsModule(this))
203+
continue queueFor;
204+
}
205+
// 2. If the chunk group is initial, we can break here because it's not accessible.
206+
if (chunkGroup.isInitial()) return false;
207+
// 3. Enqueue all parents because it must be accessible from ALL parents
208+
for (const parent of chunkGroup.parentsIterable) queue.add(parent);
209+
}
210+
// When we processed through the whole list and we didn't bailout, the module is accessible
211+
return true;
214212
}
215213

216-
_doRewriteChunkInReasons(oldChunk, newChunks) {
217-
for (let i = 0; i < this.reasons.length; i++) {
218-
this.reasons[i].rewriteChunks(oldChunk, newChunks);
214+
hasReasonForChunk(chunk) {
215+
// check for each reason if we need the chunk
216+
for (const reason of this.reasons) {
217+
const fromModule = reason.module;
218+
for (const originChunk of fromModule.chunksIterable) {
219+
// return true if module this is not reachable from originChunk when ignoring cunk
220+
if (!this.isAccessibleInChunk(originChunk, chunk)) return true;
221+
}
219222
}
223+
return false;
224+
}
225+
226+
hasReasons() {
227+
return this.reasons.length > 0;
220228
}
221229

222230
isUsed(exportName) {

lib/ModuleReason.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,21 @@ class ModuleReason {
99
this.module = module;
1010
this.dependency = dependency;
1111
this.explanation = explanation;
12-
this._chunks = null;
13-
}
14-
15-
hasChunk(chunk) {
16-
if (this._chunks) {
17-
if (this._chunks.has(chunk)) return true;
18-
} else if (this.module && this.module._chunks.has(chunk)) return true;
19-
return false;
2012
}
13+
}
2114

22-
rewriteChunks(oldChunk, newChunks) {
23-
if (!this._chunks) {
24-
if (this.module) {
25-
if (!this.module._chunks.has(oldChunk)) return;
26-
this._chunks = new Set(this.module._chunks);
27-
} else {
28-
this._chunks = new Set();
29-
}
30-
}
31-
if (this._chunks.has(oldChunk)) {
32-
this._chunks.delete(oldChunk);
33-
for (let i = 0; i < newChunks.length; i++) {
34-
this._chunks.add(newChunks[i]);
35-
}
36-
}
15+
Object.defineProperty(ModuleReason.prototype, "chunks", {
16+
configurable: false,
17+
get() {
18+
throw new Error(
19+
"ModuleReason.chunks: This was removed without replacement"
20+
);
21+
},
22+
set() {
23+
throw new Error(
24+
"ModuleReason.chunks: This was removed without replacement"
25+
);
3726
}
38-
}
27+
});
3928

4029
module.exports = ModuleReason;

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,6 @@
77
const Queue = require("../util/Queue");
88
const intersect = require("../util/SetHelpers").intersect;
99

10-
const getParentChunksWithModule = (currentChunk, module) => {
11-
const chunks = [];
12-
const stack = new Set(currentChunk.parentsIterable);
13-
14-
for (const chunk of stack) {
15-
if (chunk.containsModule(module)) {
16-
chunks.push(chunk);
17-
} else {
18-
for (const parent of chunk.parentsIterable) {
19-
stack.add(parent);
20-
}
21-
}
22-
}
23-
24-
return chunks;
25-
};
26-
2710
class RemoveParentModulesPlugin {
2811
apply(compiler) {
2912
compiler.hooks.compilation.tap("RemoveParentModulesPlugin", compilation => {
@@ -92,10 +75,6 @@ class RemoveParentModulesPlugin {
9275
if (chunk.containsModule(m)) toRemove.add(m);
9376
}
9477
for (const module of toRemove) {
95-
module.rewriteChunkInReasons(
96-
chunk,
97-
getParentChunksWithModule(chunk, module)
98-
);
9978
chunk.removeModule(module);
10079
}
10180
}

lib/optimize/SplitChunksPlugin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ module.exports = class SplitChunksPlugin {
421421
// Remove all selected modules from the chunk
422422
for (const module of item.modules) {
423423
chunk.removeModule(module);
424-
module.rewriteChunkInReasons(chunk, [newChunk]);
425424
}
426425
}
427426
// If we successfully created a new chunk or reused one

test/ModuleReason.unittest.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)