Skip to content

Commit 52bacce

Browse files
committed
Merge branch 'next' into refactor/cleanup-stuff
2 parents 944fa78 + 49cdb94 commit 52bacce

16 files changed

Lines changed: 527 additions & 275 deletions

File tree

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
}],
5252
"no-console": "off",
5353
"valid-jsdoc": "error",
54-
"node/no-unsupported-features": ["error", { version: 4 }],
54+
"node/no-unsupported-features": "error",
5555
"node/no-deprecated-api": "error",
5656
"node/no-missing-import": "error",
5757
"node/no-missing-require": [

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ matrix:
2424
- os: linux
2525
node_js: "6"
2626
env: NO_WATCH_TESTS=1 JOB_PART=integration
27-
- os: linux
28-
node_js: "4"
29-
env: NO_WATCH_TESTS=1 JOB_PART=integration
3027
- os: osx
3128
node_js: "8"
3229
env: NO_WATCH_TESTS=1 JOB_PART=integration

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ machine:
88

99
dependencies:
1010
pre:
11-
- case $CIRCLE_NODE_INDEX in 0) NODE_VERSION=4 ;; 1) NODE_VERSION=8 ;; esac; nvm install $NODE_VERSION && nvm alias default $NODE_VERSION
11+
- case $CIRCLE_NODE_INDEX in 0) NODE_VERSION=6 ;; 1) NODE_VERSION=8 ;; esac; nvm install $NODE_VERSION && nvm alias default $NODE_VERSION
1212
override:
1313
- yarn
1414
- yarn link || true && yarn link webpack

lib/Chunk.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ const sortByIdentifier = (a, b) => {
2121
return 0;
2222
};
2323

24+
const getFrozenArray = set => Object.freeze(Array.from(set));
25+
26+
const getModulesIdent = set => {
27+
set.sort();
28+
let str = "";
29+
set.forEach(m => {
30+
str += m.identifier() + "#";
31+
});
32+
return str;
33+
};
34+
35+
const getArray = set => Array.from(set);
36+
37+
const getModulesSize = set => {
38+
let count = 0;
39+
for(const module of set) {
40+
count += module.size();
41+
}
42+
return count;
43+
};
44+
2445
class Chunk {
2546

2647
constructor(name, module, loc) {
@@ -70,7 +91,7 @@ class Chunk {
7091
* @return {Array} - an array containing the chunks
7192
*/
7293
getChunks() {
73-
return Array.from(this._chunks);
94+
return this._chunks.getFromCache(getArray);
7495
}
7596

7697
getNumberOfChunks() {
@@ -85,7 +106,7 @@ class Chunk {
85106
* @return {Array} - an array containing the parents
86107
*/
87108
getParents() {
88-
return Array.from(this._parents);
109+
return this._parents.getFromCache(getArray);
89110
}
90111

91112
setParents(newParents) {
@@ -114,7 +135,7 @@ class Chunk {
114135
* @return {Array} - an array containing the blocks
115136
*/
116137
getBlocks() {
117-
return Array.from(this._blocks);
138+
return this._blocks.getFromCache(getArray);
118139
}
119140

120141
setBlocks(newBlocks) {
@@ -274,16 +295,11 @@ class Chunk {
274295
}
275296

276297
getModules() {
277-
return Array.from(this._modules);
298+
return this._modules.getFromCache(getArray);
278299
}
279300

280301
getModulesIdent() {
281-
this._modules.sort();
282-
let str = "";
283-
this._modules.forEach(m => {
284-
str += m.identifier() + "#";
285-
});
286-
return str;
302+
return this._modules.getFromUnorderedCache(getModulesIdent);
287303
}
288304

289305
remove(reason) {
@@ -457,11 +473,7 @@ class Chunk {
457473
}
458474

459475
modulesSize() {
460-
let count = 0;
461-
for(const module of this._modules) {
462-
count += module.size();
463-
}
464-
return count;
476+
return this._modules.getFromUnorderedCache(getModulesSize);
465477
}
466478

467479
size(options) {
@@ -548,7 +560,7 @@ class Chunk {
548560
Object.defineProperty(Chunk.prototype, "modules", {
549561
configurable: false,
550562
get: util.deprecate(function() {
551-
return this._modules.getFrozenArray();
563+
return this._modules.getFromCache(getFrozenArray);
552564
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
553565
set: util.deprecate(function(value) {
554566
this.setModules(value);
@@ -558,7 +570,7 @@ Object.defineProperty(Chunk.prototype, "modules", {
558570
Object.defineProperty(Chunk.prototype, "chunks", {
559571
configurable: false,
560572
get: util.deprecate(function() {
561-
return this._chunks.getFrozenArray();
573+
return this._chunks.getFromCache(getFrozenArray);
562574
}, "Chunk.chunks: Use Chunk.getChunks() instead"),
563575
set() {
564576
throw new Error("Readonly. Use Chunk.addChunk/removeChunk/getChunks to access/modify chunks.");
@@ -568,7 +580,7 @@ Object.defineProperty(Chunk.prototype, "chunks", {
568580
Object.defineProperty(Chunk.prototype, "parents", {
569581
configurable: false,
570582
get: util.deprecate(function() {
571-
return this._parents.getFrozenArray();
583+
return this._parents.getFromCache(getFrozenArray);
572584
}, "Chunk.parents: Use Chunk.getParents() instead"),
573585
set: util.deprecate(function(value) {
574586
this.setParents(value);
@@ -578,7 +590,7 @@ Object.defineProperty(Chunk.prototype, "parents", {
578590
Object.defineProperty(Chunk.prototype, "blocks", {
579591
configurable: false,
580592
get: util.deprecate(function() {
581-
return this._blocks.getFrozenArray();
593+
return this._blocks.getFromCache(getFrozenArray);
582594
}, "Chunk.blocks: Use Chunk.getBlocks() instead"),
583595
set: util.deprecate(function(value) {
584596
this.setBlocks(value);

lib/Module.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ const sortByDebugId = (a, b) => {
2121
return a.debugId - b.debugId;
2222
};
2323

24+
const getFrozenArray = set => Object.freeze(Array.from(set));
25+
26+
const getDebugIdent = set => {
27+
set.sortWith(sortByDebugId);
28+
const chunks = set;
29+
const list = [];
30+
for(const chunk of chunks) {
31+
const debugId = chunk.debugId;
32+
33+
if(typeof debugId !== "number") {
34+
return null;
35+
}
36+
37+
list.push(debugId);
38+
}
39+
40+
return list.join(",");
41+
};
42+
2443
class Module extends DependenciesBlock {
2544

2645
constructor() {
@@ -49,9 +68,6 @@ class Module extends DependenciesBlock {
4968
this.reasons = [];
5069
this._chunks = new SortableSet(undefined, sortById);
5170

52-
// Caching
53-
this._chunksDebugIdent = undefined;
54-
5571
// Info from Compilation (per Compilation)
5672
this.id = null;
5773
this.index = null;
@@ -73,8 +89,6 @@ class Module extends DependenciesBlock {
7389
this.reasons.length = 0;
7490
this._chunks.clear();
7591

76-
this._chunksDebugIdent = undefined;
77-
7892
this.id = null;
7993
this.index = null;
8094
this.index2 = null;
@@ -97,23 +111,19 @@ class Module extends DependenciesBlock {
97111
this.index2 = null;
98112
this.depth = null;
99113
this._chunks.clear();
100-
this._chunksDebugIdent = undefined;
101114
super.unseal();
102115
}
103116

104117
setChunks(chunks) {
105118
this._chunks = new SortableSet(chunks, sortById);
106-
this._chunksDebugIdent = undefined;
107119
}
108120

109121
addChunk(chunk) {
110122
this._chunks.add(chunk);
111-
this._chunksDebugIdent = undefined;
112123
}
113124

114125
removeChunk(chunk) {
115126
if(this._chunks.delete(chunk)) {
116-
this._chunksDebugIdent = undefined;
117127
chunk.removeModule(this);
118128
return true;
119129
}
@@ -125,21 +135,7 @@ class Module extends DependenciesBlock {
125135
}
126136

127137
getChunkIdsIdent() {
128-
if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
129-
this._chunks.sortWith(sortByDebugId);
130-
const chunks = this._chunks;
131-
const list = [];
132-
for(const chunk of chunks) {
133-
const debugId = chunk.debugId;
134-
135-
if(typeof debugId !== "number") {
136-
return this._chunksDebugIdent = null;
137-
}
138-
139-
list.push(debugId);
140-
}
141-
142-
return this._chunksDebugIdent = list.join(",");
138+
return this._chunks.getFromUnorderedCache(getDebugIdent);
143139
}
144140

145141
get optional() {
@@ -279,7 +275,7 @@ Object.defineProperty(Module.prototype, "entry", {
279275
Object.defineProperty(Module.prototype, "chunks", {
280276
configurable: false,
281277
get: util.deprecate(function() {
282-
return this._chunks.getFrozenArray();
278+
return this._chunks.getFromCache(getFrozenArray);
283279
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
284280
set() {
285281
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");

lib/NormalModule.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ class NormalModule extends Module {
203203

204204
const resourceBuffer = result.resourceBuffer;
205205
const source = result.result[0];
206-
const sourceMap = result.result[1];
206+
const sourceMap = result.result.length >= 1 ? result.result[1] : null;
207+
const extraInfo = result.result.length >= 2 ? result.result[2] : null;
207208

208209
if(!Buffer.isBuffer(source) && typeof source !== "string") {
209210
const error = new ModuleBuildError(this, new Error("Final loader didn't return a Buffer or String"));
210211
return callback(error);
211212
}
212213

213214
this._source = this.createSource(asString(source), resourceBuffer, sourceMap);
215+
this._ast = typeof extraInfo === "object" && extraInfo !== null && extraInfo.webpackAST !== undefined ? extraInfo.webpackAST : null;
214216
return callback();
215217
});
216218
}
@@ -220,6 +222,7 @@ class NormalModule extends Module {
220222
this.error = error;
221223
this.errors.push(this.error);
222224
this._source = new RawSource("throw new Error(" + JSON.stringify(this.error.message) + ");");
225+
this._ast = null;
223226
}
224227

225228
applyNoParseRule(rule, content) {
@@ -267,6 +270,7 @@ class NormalModule extends Module {
267270
this.buildTimestamp = Date.now();
268271
this.built = true;
269272
this._source = null;
273+
this._ast = null;
270274
this.error = null;
271275
this.errors.length = 0;
272276
this.warnings.length = 0;
@@ -292,7 +296,7 @@ class NormalModule extends Module {
292296
}
293297

294298
try {
295-
this.parser.parse(this._source.source(), {
299+
this.parser.parse(this._ast || this._source.source(), {
296300
current: this,
297301
module: this,
298302
compilation: compilation,

lib/Parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,11 @@ class Parser extends Tapable {
13761376

13771377
parse(source, initialState) {
13781378
let ast;
1379-
const comments = [];
1379+
let comments = [];
1380+
if(typeof source === "object" && source !== null) {
1381+
ast = source;
1382+
comments = source.comments;
1383+
}
13801384
for(let i = 0, len = POSSIBLE_AST_OPTIONS.length; i < len; i++) {
13811385
if(!ast) {
13821386
try {

lib/util/SortableSet.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = class SortableSet extends Set {
66
super(initialIterable);
77
this._sortFn = defaultSort;
88
this._lastActiveSortFn = null;
9-
this._frozenArray = null;
9+
this._cache = null;
10+
this._cacheOrderIndependent = null;
1011
}
1112

1213
/**
@@ -15,18 +16,21 @@ module.exports = class SortableSet extends Set {
1516
*/
1617
add(value) {
1718
this._lastActiveSortFn = null;
18-
this._frozenArray = null;
19+
this._cache = null;
20+
this._cacheOrderIndependent = null;
1921
super.add(value);
2022
return this;
2123
}
2224

2325
delete(value) {
24-
this._frozenArray = null;
26+
this._cache = null;
27+
this._cacheOrderIndependent = null;
2528
return super.delete(value);
2629
}
2730

2831
clear() {
29-
this._frozenArray = null;
32+
this._cache = null;
33+
this._cacheOrderIndependent = null;
3034
return super.clear();
3135
}
3236

@@ -46,7 +50,7 @@ module.exports = class SortableSet extends Set {
4650
super.add(sortedArray[i]);
4751
}
4852
this._lastActiveSortFn = sortFn;
49-
this._frozenArray = null;
53+
this._cache = null;
5054
}
5155

5256
/**
@@ -57,12 +61,37 @@ module.exports = class SortableSet extends Set {
5761
}
5862

5963
/**
60-
* @returns {any[]} - returns content as frozen array
64+
* @param {Function} fn - function to calculate value
65+
* @returns {any} - returns result of fn(this), cached until set changes
6166
*/
62-
getFrozenArray() {
63-
if(this._frozenArray === null) {
64-
this._frozenArray = Object.freeze(Array.from(this));
67+
getFromCache(fn) {
68+
if(this._cache === null) {
69+
this._cache = new Map();
70+
} else {
71+
const data = this._cache.get(fn);
72+
if(data !== undefined) {
73+
return data;
74+
}
6575
}
66-
return this._frozenArray;
76+
const newData = fn(this);
77+
this._cache.set(fn, newData);
78+
return newData;
79+
}
80+
/**
81+
* @param {Function} fn - function to calculate value
82+
* @returns {any} - returns result of fn(this), cached until set changes
83+
*/
84+
getFromUnorderedCache(fn) {
85+
if(this._cacheOrderIndependent === null) {
86+
this._cacheOrderIndependent = new Map();
87+
} else {
88+
const data = this._cacheOrderIndependent.get(fn);
89+
if(data !== undefined) {
90+
return data;
91+
}
92+
}
93+
const newData = fn(this);
94+
this._cacheOrderIndependent.set(fn, newData);
95+
return newData;
6796
}
6897
};

0 commit comments

Comments
 (0)