Skip to content

Commit 814c894

Browse files
committed
Merge branch 'master' into next
# Conflicts: # lib/optimize/CommonsChunkPlugin.js # test/statsCases/limit-chunk-count-plugin/expected.txt # test/statsCases/named-chunks-plugin-async/expected.txt # test/statsCases/optimize-chunks/expected.txt
2 parents 9324616 + 33818bc commit 814c894

67 files changed

Lines changed: 681 additions & 21 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎bin/webpack.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ yargs.parse(process.argv.slice(2), (err, argv, output) => {
211211
if(typeof outputOptions.context === "undefined")
212212
outputOptions.context = firstOptions.context;
213213

214+
ifArg("env", function(value) {
215+
if(outputOptions.env) {
216+
outputOptions._env = value;
217+
}
218+
});
219+
214220
ifArg("json", function(bool) {
215221
if(bool)
216222
outputOptions.json = bool;

‎lib/Chunk.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ class Chunk {
384384
otherChunk.origins.forEach(origin => {
385385
this.origins.push(origin);
386386
});
387+
this.blocks.forEach(b => {
388+
b.chunkReason = reason;
389+
});
387390
this.origins.forEach(origin => {
388391
if(!origin.reasons) {
389392
origin.reasons = [reason];

‎lib/Compilation.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ChunkRenderError = require("./ChunkRenderError");
2323
const CachedSource = require("webpack-sources").CachedSource;
2424
const Stats = require("./Stats");
2525
const Semaphore = require("./util/Semaphore");
26+
const Queue = require("./util/Queue");
2627

2728
function byId(a, b) {
2829
if(a.id < b.id) return -1;
@@ -967,10 +968,10 @@ class Compilation extends Tapable {
967968

968969
let availableModules;
969970
let newAvailableModules;
970-
const queue2 = inputChunks.map(chunk => ({
971+
const queue2 = new Queue(inputChunks.map(chunk => ({
971972
chunk,
972973
availableModules: new Set()
973-
}));
974+
})));
974975

975976
// Helper function to check if all modules of a chunk are available
976977
const areModulesAvailable = (chunk, availableModules) => {
@@ -995,7 +996,7 @@ class Compilation extends Tapable {
995996

996997
// Iterative traversing of the basic chunk graph
997998
while(queue2.length) {
998-
const queueItem = queue2.pop();
999+
const queueItem = queue2.dequeue();
9991000
chunk = queueItem.chunk;
10001001
availableModules = queueItem.availableModules;
10011002

@@ -1016,6 +1017,7 @@ class Compilation extends Tapable {
10161017
}
10171018
if(!deletedModules)
10181019
continue;
1020+
availableModules = minAvailableModules;
10191021
}
10201022

10211023
// 2. Get the edges at this point of the graph
@@ -1053,7 +1055,7 @@ class Compilation extends Tapable {
10531055

10541056
// 8. Enqueue further traversal
10551057
for(const nextChunk of nextChunks) {
1056-
queue2.push({
1058+
queue2.enqueue({
10571059
chunk: nextChunk,
10581060
availableModules: newAvailableModules
10591061
});

‎lib/ContextModuleFactory.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ module.exports = class ContextModuleFactory extends Tapable {
114114
const subResource = path.join(directory, seqment);
115115

116116
fs.stat(subResource, (err, stat) => {
117-
if(err) return callback(err);
117+
if(err) {
118+
if(err.code === "ENOENT") {
119+
// ENOENT is ok here because the file may have been deleted between
120+
// the readdir and stat calls.
121+
return callback();
122+
} else {
123+
return callback(err);
124+
}
125+
}
118126

119127
if(stat.isDirectory()) {
120128

‎lib/Stats.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Stats {
100100
const requestShortener = new RequestShortener(context);
101101
const showPerformance = optionOrLocalFallback(options.performance, true);
102102
const showHash = optionOrLocalFallback(options.hash, true);
103+
const showEnv = optionOrLocalFallback(options.env, false);
103104
const showVersion = optionOrLocalFallback(options.version, true);
104105
const showTimings = optionOrLocalFallback(options.timings, true);
105106
const showAssets = optionOrLocalFallback(options.assets, true);
@@ -240,6 +241,11 @@ class Stats {
240241
if(showTimings && this.startTime && this.endTime) {
241242
obj.time = this.endTime - this.startTime;
242243
}
244+
245+
if(showEnv && options._env) {
246+
obj.env = options._env;
247+
}
248+
243249
if(compilation.needAdditionalPass) {
244250
obj.needAdditionalPass = true;
245251
}
@@ -546,6 +552,11 @@ class Stats {
546552
colors.normal("ms");
547553
newline();
548554
}
555+
if(obj.env) {
556+
colors.normal("Environment (--env): ");
557+
colors.bold(JSON.stringify(obj.env, null, 2));
558+
newline();
559+
}
549560
if(obj.publicPath) {
550561
colors.normal("PublicPath: ");
551562
colors.bold(obj.publicPath);
@@ -900,6 +911,7 @@ class Stats {
900911
chunkModules: true,
901912
chunkOrigins: true,
902913
depth: true,
914+
env: true,
903915
reasons: true,
904916
usedExports: true,
905917
providedExports: true,

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ You can however specify the name of the async chunk by passing the desired strin
107107
/**
108108
* These chunks are subject to get "common" modules extracted and moved to the common chunk
109109
*/
110-
const affectedChunks = this.getAffectedChunks(compilation, chunks, targetChunk, targetChunks, idx, this.selectedChunks, this.async, this.deepChildren, this.children);
110+
const affectedChunks = this.getAffectedChunks(compilation, chunks, targetChunk, targetChunks, idx, this.selectedChunks, this.async, this.children, this.deepChildren);
111111

112112
// bail if no chunk is affected
113113
if(!affectedChunks) {
@@ -214,7 +214,7 @@ You can however specify the name of the async chunk by passing the desired strin
214214
Take a look at the "name"/"names" or async/children option.`);
215215
}
216216

217-
getAffectedUnnamedChunks(affectedChunks, targetChunk, asyncOption, deepChildrenOption) {
217+
getAffectedUnnamedChunks(affectedChunks, targetChunk, rootChunk, asyncOption, deepChildrenOption) {
218218
for(const chunk of targetChunk.chunksIterable) {
219219
if(chunk.isInitial()) {
220220
continue;
@@ -224,11 +224,11 @@ Take a look at the "name"/"names" or async/children option.`);
224224
// b) themselves affected chunks
225225
// we can assume that this chunk is an affected chunk too, as there is no way a chunk that
226226
// isn't only depending on the target chunk is a parent of the chunk tested
227-
if(asyncOption || chunk.getParents().every((parentChunk) => parentChunk === targetChunk || affectedChunks.has(parentChunk))) {
227+
if(asyncOption || chunk.getParents().every((parentChunk) => parentChunk === rootChunk || affectedChunks.has(parentChunk))) {
228228
// This check not only dedupes the affectedChunks but also guarantees we avoid endless loops
229-
if(!affectedChunks.has(chunk) || affectedChunks.values().next().value === chunk) {
229+
if(!affectedChunks.has(chunk)) {
230230
// We mutate the affected chunks before going deeper, so the deeper levels and other branches
231-
// Have the information of this chunk being affected for their assertion if a chunk should
231+
// have the information of this chunk being affected for their assertion if a chunk should
232232
// not be affected
233233
affectedChunks.add(chunk);
234234

@@ -237,15 +237,15 @@ Take a look at the "name"/"names" or async/children option.`);
237237
// at the latest the last connection to the same chunk meets the
238238
// condition to add it to the affected chunks.
239239
if(deepChildrenOption === true) {
240-
this.getAffectedUnnamedChunks(affectedChunks, chunk, asyncOption, deepChildrenOption);
240+
this.getAffectedUnnamedChunks(affectedChunks, chunk, rootChunk, asyncOption, deepChildrenOption);
241241
}
242242
}
243243
}
244244
}
245245
}
246246

247-
getAffectedChunks(compilation, allChunks, targetChunk, targetChunks, currentIndex, selectedChunks, asyncOption, deepChildrenOption, children) {
248-
const asyncOrNoSelectedChunk = children || asyncOption;
247+
getAffectedChunks(compilation, allChunks, targetChunk, targetChunks, currentIndex, selectedChunks, asyncOption, childrenOption, deepChildrenOption) {
248+
const asyncOrNoSelectedChunk = childrenOption || asyncOption;
249249

250250
if(Array.isArray(selectedChunks)) {
251251
return allChunks.filter(chunk => {
@@ -257,7 +257,7 @@ Take a look at the "name"/"names" or async/children option.`);
257257

258258
if(asyncOrNoSelectedChunk) {
259259
let affectedChunks = new Set();
260-
this.getAffectedUnnamedChunks(affectedChunks, targetChunk, asyncOption, deepChildrenOption);
260+
this.getAffectedUnnamedChunks(affectedChunks, targetChunk, targetChunk, asyncOption, deepChildrenOption);
261261
return Array.from(affectedChunks);
262262
}
263263

‎lib/util/Queue.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
3+
module.exports = class Queue {
4+
constructor(items) {
5+
this.first = null;
6+
this.last = null;
7+
this.length = 0;
8+
if(items) {
9+
for(const item of items) {
10+
this.enqueue(item);
11+
}
12+
}
13+
}
14+
15+
enqueue(item) {
16+
const first = this.first;
17+
const node = {
18+
item,
19+
next: null
20+
};
21+
if(first === null) {
22+
this.last = node;
23+
} else {
24+
first.next = node;
25+
}
26+
this.first = node;
27+
this.length++;
28+
}
29+
30+
dequeue() {
31+
const last = this.last;
32+
if(last === null)
33+
return undefined;
34+
const next = last.next;
35+
if(next === null) {
36+
this.first = null;
37+
}
38+
this.last = next;
39+
this.length--;
40+
return last.item;
41+
}
42+
};

‎test/ContextModuleFactory.test.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* globals describe, it, beforeEach */
2+
"use strict";
3+
require("should");
4+
const MemoryFs = require("memory-fs");
5+
const ContextModuleFactory = require("../lib/ContextModuleFactory");
6+
7+
describe("ContextModuleFactory", function() {
8+
describe("resolveDependencies", function() {
9+
let factory, memfs;
10+
beforeEach(function() {
11+
factory = new ContextModuleFactory([]);
12+
memfs = new MemoryFs();
13+
});
14+
it("should not report an error when ENOENT errors happen", function(done) {
15+
memfs.readdir = (dir, callback) => {
16+
setTimeout(() => callback(null, ["/file"]));
17+
};
18+
memfs.stat = (file, callback) => {
19+
let err = new Error("fake ENOENT error");
20+
err.code = "ENOENT";
21+
setTimeout(() => callback(err, null));
22+
};
23+
factory.resolveDependencies(memfs, "/", true, /.*/, (err, res) => {
24+
(!!err).should.be.false();
25+
res.should.be.an.Array();
26+
res.length.should.be.exactly(0);
27+
done();
28+
});
29+
});
30+
it("should report an error when non-ENOENT errors happen", function(done) {
31+
memfs.readdir = (dir, callback) => {
32+
setTimeout(() => callback(null, ["/file"]));
33+
};
34+
memfs.stat = (file, callback) => {
35+
let err = new Error("fake EACCES error");
36+
err.code = "EACCES";
37+
setTimeout(() => callback(err, null));
38+
};
39+
factory.resolveDependencies(memfs, "/", true, /.*/, (err, res) => {
40+
err.should.be.an.Error();
41+
(!!res).should.be.false();
42+
done();
43+
});
44+
});
45+
});
46+
});

‎test/binCases/env/complex/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "index";

‎test/binCases/env/complex/test.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
module.exports = function testAssertions(code, stdout, stderr) {
4+
code.should.be.exactly(0);
5+
6+
stdout.should.be.ok();
7+
stdout[0].should.containEql("Hash: ");
8+
stdout[1].should.containEql("Version: ");
9+
stdout[2].should.containEql("Time: ");
10+
stdout[3].should.containEql("Environment (--env): {");
11+
stdout[4].should.containEql("\"prod\": [");
12+
stdout[7].should.containEql("],");
13+
stdout[8].should.containEql("\"baz\": true");
14+
stdout[9].should.containEql("}");
15+
stdout[11].should.containEql("null.js");
16+
stdout[12].should.containEql("./index.js");
17+
stdout[12].should.containEql("[built]");
18+
19+
stderr.should.be.empty();
20+
};

0 commit comments

Comments
 (0)