Skip to content

Commit 8a30188

Browse files
committed
use Set for ModuleReason chunk rewriting
1 parent 3ae782d commit 8a30188

File tree

3 files changed

+122
-25
lines changed

3 files changed

+122
-25
lines changed

lib/Module.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ const DependenciesBlock = require("./DependenciesBlock");
99
const ModuleReason = require("./ModuleReason");
1010
const Template = require("./Template");
1111

12-
function addToSet(set, items) {
13-
for(const item of items) {
14-
if(set.indexOf(item) < 0)
15-
set.push(item);
16-
}
17-
}
18-
1912
function byId(a, b) {
2013
return a.id - b.id;
2114
}
@@ -161,28 +154,17 @@ class Module extends DependenciesBlock {
161154
}
162155

163156
hasReasonForChunk(chunk) {
164-
for(const r of this.reasons) {
165-
if(r.chunks) {
166-
if(r.chunks.indexOf(chunk) >= 0)
167-
return true;
168-
} else if(r.module._chunks.has(chunk))
157+
for(let i = 0; i < this.reasons.length; i++) {
158+
if(this.reasons[i].hasChunk(chunk))
169159
return true;
170160
}
171161
return false;
172162
}
173163

174164
rewriteChunkInReasons(oldChunk, newChunks) {
175-
this.reasons.forEach(r => {
176-
if(!r.chunks) {
177-
if(!r.module._chunks.has(oldChunk))
178-
return;
179-
r.chunks = Array.from(r.module._chunks);
180-
}
181-
r.chunks = r.chunks.reduce((arr, c) => {
182-
addToSet(arr, c !== oldChunk ? [c] : newChunks);
183-
return arr;
184-
}, []);
185-
});
165+
for(let i = 0; i < this.reasons.length; i++) {
166+
this.reasons[i].rewriteChunks(oldChunk, newChunks);
167+
}
186168
}
187169

188170
isUsed(exportName) {

lib/ModuleReason.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,47 @@
44
*/
55
"use strict";
66

7-
module.exports = class ModuleReason {
7+
const util = require("util");
8+
9+
class ModuleReason {
810
constructor(module, dependency) {
911
this.module = module;
1012
this.dependency = dependency;
13+
this._chunks = null;
14+
}
15+
16+
hasChunk(chunk) {
17+
if(this._chunks) {
18+
if(this._chunks.has(chunk))
19+
return true;
20+
} else if(this.module._chunks.has(chunk))
21+
return true;
22+
return false;
23+
}
24+
25+
rewriteChunks(oldChunk, newChunks) {
26+
if(!this._chunks) {
27+
if(!this.module._chunks.has(oldChunk))
28+
return;
29+
this._chunks = new Set(this.module._chunks);
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+
}
37+
}
38+
}
39+
40+
Object.defineProperty(ModuleReason.prototype, "chunks", {
41+
configurable: false,
42+
get: util.deprecate(function() {
43+
return this._chunks ? Array.from(this._chunks) : null;
44+
}, "ModuleReason.chunks: Use ModuleReason.hasChunk/rewriteChunks instead"),
45+
set() {
46+
throw new Error("Readonly. Use ModuleReason.rewriteChunks to modify chunks.");
1147
}
12-
};
48+
});
49+
50+
module.exports = ModuleReason;

test/ModuleReason.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"use strict";
2+
3+
const Module = require("../lib/Module");
4+
const Chunk = require("../lib/Chunk");
5+
const Dependency = require("../lib/Dependency");
6+
const ModuleReason = require("../lib/ModuleReason");
7+
const should = require("should");
8+
9+
describe("ModuleReason", () => {
10+
let myModule;
11+
let myDependency;
12+
let myModuleReason;
13+
let myChunk;
14+
let myChunk2;
15+
16+
beforeEach(() => {
17+
myModule = new Module();
18+
myDependency = new Dependency();
19+
myChunk = new Chunk("chunk-test", "module-test", "loc-test");
20+
myChunk2 = new Chunk("chunk-test", "module-test", "loc-test");
21+
22+
myModuleReason = new ModuleReason(myModule, myDependency);
23+
});
24+
25+
describe("hasChunk", () => {
26+
it("returns false when chunk is not present", () => should(myModuleReason.hasChunk(myChunk)).be.false());
27+
28+
it("returns true when chunk is present", () => {
29+
myModuleReason.module.addChunk(myChunk);
30+
should(myModuleReason.hasChunk(myChunk)).be.true();
31+
});
32+
});
33+
34+
describe("rewriteChunks", () => {
35+
it("if old chunk is present, it is replaced with new chunks", () => {
36+
myModuleReason.module.addChunk(myChunk);
37+
myModuleReason.rewriteChunks(myChunk, [myChunk2]);
38+
39+
should(myModuleReason.hasChunk(myChunk)).be.false();
40+
should(myModuleReason.hasChunk(myChunk2)).be.true();
41+
});
42+
43+
it("if old chunk is not present, new chunks are not added", () => {
44+
myModuleReason.rewriteChunks(myChunk, [myChunk2]);
45+
46+
should(myModuleReason.hasChunk(myChunk)).be.false();
47+
should(myModuleReason.hasChunk(myChunk2)).be.false();
48+
});
49+
50+
it("if already rewritten chunk is present, it is replaced with new chunks", () => {
51+
myModuleReason.module.addChunk(myChunk);
52+
myModuleReason.rewriteChunks(myChunk, [myChunk2]);
53+
myModuleReason.rewriteChunks(myChunk2, [myChunk]);
54+
55+
should(myModuleReason.hasChunk(myChunk)).be.true();
56+
should(myModuleReason.hasChunk(myChunk2)).be.false();
57+
});
58+
});
59+
60+
describe(".chunks", () => {
61+
it("is null if no rewrites happen first", () => {
62+
should(myModuleReason.chunks).be.Null();
63+
});
64+
65+
it("is null if only invalid rewrites happen first", () => {
66+
myModuleReason.rewriteChunks(myChunk, [myChunk2]);
67+
should(myModuleReason.chunks).be.Null();
68+
});
69+
70+
it("is an array of chunks if a valid rewrite happens", () => {
71+
myModuleReason.module.addChunk(myChunk);
72+
myModuleReason.rewriteChunks(myChunk, [myChunk2]);
73+
74+
should(myModuleReason.chunks).be.eql([myChunk2]);
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy