Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine webpack 3 PRs #4770

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 33 additions & 34 deletions bin/webpack.js 100755 → 100644
Expand Up @@ -129,6 +129,11 @@ yargs.options({
group: DISPLAY_GROUP,
describe: "Display details about errors"
},
"display": {
type: "string",
group: DISPLAY_GROUP,
describe: "Select display preset (verbose, detailed, normal, minimal, errors-only, none)"
},
"verbose": {
type: "boolean",
group: DISPLAY_GROUP,
Expand All @@ -139,15 +144,7 @@ yargs.options({
var argv = yargs.argv;

if(argv.verbose) {
argv["display-reasons"] = true;
argv["display-depth"] = true;
argv["display-entrypoints"] = true;
argv["display-used-exports"] = true;
argv["display-provided-exports"] = true;
argv["display-error-details"] = true;
argv["display-modules"] = true;
argv["display-cached"] = true;
argv["display-cached-assets"] = true;
argv["display"] = "verbose";
}

var options = require("./convert-argv")(yargs, argv);
Expand Down Expand Up @@ -181,6 +178,11 @@ function processOptions(options) {
} else if(!outputOptions) {
outputOptions = {};
}

ifArg("display", function(preset) {
outputOptions = statsPresetToOptions(preset);
});

outputOptions = Object.create(outputOptions);
if(Array.isArray(options) && !outputOptions.children) {
outputOptions.children = options.map(o => o.stats);
Expand Down Expand Up @@ -219,36 +221,46 @@ function processOptions(options) {
outputOptions.cachedAssets = false;

ifArg("display-chunks", function(bool) {
outputOptions.modules = !bool;
outputOptions.chunks = bool;
if(bool) {
outputOptions.modules = false;
outputOptions.chunks = true;
outputOptions.chunkModules = true;
}
});

ifArg("display-entrypoints", function(bool) {
outputOptions.entrypoints = bool;
if(bool)
outputOptions.entrypoints = true;
});

ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
if(bool)
outputOptions.reasons = true;
});

ifArg("display-depth", function(bool) {
outputOptions.depth = bool;
if(bool)
outputOptions.depth = true;
});

ifArg("display-used-exports", function(bool) {
outputOptions.usedExports = bool;
if(bool)
outputOptions.usedExports = true;
});

ifArg("display-provided-exports", function(bool) {
outputOptions.providedExports = bool;
if(bool)
outputOptions.providedExports = true;
});

ifArg("display-error-details", function(bool) {
outputOptions.errorDetails = bool;
if(bool)
outputOptions.errorDetails = true;
});

ifArg("display-origins", function(bool) {
outputOptions.chunkOrigins = bool;
if(bool)
outputOptions.chunkOrigins = true;
});

ifArg("display-max-modules", function(value) {
Expand All @@ -272,21 +284,6 @@ function processOptions(options) {
outputOptions.maxModules = Infinity;
outputOptions.exclude = undefined;
}
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.entrypoints === "undefined")
outputOptions.entrypoints = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
outputOptions.chunkModules = true;
if(typeof outputOptions.reasons === "undefined")
outputOptions.reasons = true;
if(typeof outputOptions.cached === "undefined")
outputOptions.cached = true;
if(typeof outputOptions.cachedAssets === "undefined")
outputOptions.cachedAssets = true;
}

ifArg("hide-modules", function(bool) {
Expand Down Expand Up @@ -337,7 +334,9 @@ function processOptions(options) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
} else if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString(outputOptions) + "\n");
var statsString = stats.toString(outputOptions);
if(statsString)
process.stdout.write(statsString + "\n");
}
if(!options.watch && stats.hasErrors()) {
process.on("exit", function() {
Expand Down
12 changes: 1 addition & 11 deletions lib/AsyncDependenciesBlock.js
Expand Up @@ -37,17 +37,7 @@ module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
sortItems() {
super.sortItems();
if(this.chunks) {
this.chunks.sort((a, b) => {
let i = 0;
while(true) { // eslint-disable-line no-constant-condition
if(!a.modules[i] && !b.modules[i]) return 0;
if(!a.modules[i]) return -1;
if(!b.modules[i]) return 1;
if(a.modules[i].id > b.modules[i].id) return 1;
if(a.modules[i].id < b.modules[i].id) return -1;
i++;
}
});
this.chunks.sort((a, b) => a.compareTo(b));
}
}
};
111 changes: 94 additions & 17 deletions lib/Chunk.js
Expand Up @@ -4,6 +4,7 @@
*/
"use strict";

const util = require("util");
const compareLocations = require("./compareLocations");
let debugId = 1000;

Expand All @@ -20,7 +21,8 @@ class Chunk {
this.ids = null;
this.debugId = debugId++;
this.name = name;
this.modules = [];
this._modules = new Set();
this._modulesIsSorted = true;
this.entrypoints = [];
this.chunks = [];
this.parents = [];
Expand Down Expand Up @@ -88,17 +90,20 @@ class Chunk {
}

addModule(module) {
return this.addToCollection(this.modules, module);
if(!this._modules.has(module)) {
this._modules.add(module);
this._modulesIsSorted = false;
return true;
}
return false;
}

addBlock(block) {
return this.addToCollection(this.blocks, block);
}

removeModule(module) {
const idx = this.modules.indexOf(module);
if(idx >= 0) {
this.modules.splice(idx, 1);
if(this._modules.delete(module)) {
module.removeChunk(this);
return true;
}
Expand Down Expand Up @@ -133,9 +138,72 @@ class Chunk {
});
}

setModules(modules) {
this._modules = new Set(modules);
this._modulesIsSorted = false;
}

getNumberOfModules() {
return this._modules.size;
}

get modulesIterable() {
return this._modules;
}

forEachModule(fn) {
this._modules.forEach(fn);
}

mapModules(fn) {
const modules = this._modules;
const array = new Array(modules.size);
let idx = 0;
for(let module of modules) {
array[idx++] = fn(module, idx, modules);
}
return array;
}

_ensureModulesSorted() {
if(this._modulesIsSorted) return;
this._modules = new Set(Array.from(this._modules).sort((a, b) => {
if(a.identifier() > b.identifier()) return 1;
if(a.identifier() < b.identifier()) return -1;
return 0;
}));
this._modulesIsSorted = true;
}

compareTo(otherChunk) {
this._ensureModulesSorted();
otherChunk._ensureModulesSorted();
if(this._modules.size > otherChunk._modules.size) return -1;
if(this._modules.size < otherChunk._modules.size) return 1;
const a = this._modules[Symbol.iterator]();
const b = otherChunk._modules[Symbol.iterator]();
while(true) { // eslint-disable-line
const aItem = a.next();
const bItem = b.next();
if(aItem.done) return 0;
const aModuleIdentifier = aItem.value.identifier();
const bModuleIdentifier = bItem.value.identifier();
if(aModuleIdentifier > bModuleIdentifier) return -1;
if(aModuleIdentifier < bModuleIdentifier) return 1;
}
}

containsModule(module) {
return this._modules.has(module);
}

getModules() {
return Array.from(this._modules);
}

remove(reason) {
// cleanup modules
this.modules.slice().forEach(module => {
Array.from(this._modules).forEach(module => {
module.removeChunk(this);
});

Expand Down Expand Up @@ -219,9 +287,9 @@ class Chunk {
return false;
}

const otherChunkModules = otherChunk.modules.slice();
const otherChunkModules = Array.from(otherChunk._modules);
otherChunkModules.forEach(module => otherChunk.moveModule(module, this));
otherChunk.modules.length = 0;
otherChunk._modules.clear();

otherChunk.parents.forEach(parentChunk => parentChunk.replaceChunk(otherChunk, this));
otherChunk.parents.length = 0;
Expand Down Expand Up @@ -276,14 +344,14 @@ class Chunk {
}

isEmpty() {
return this.modules.length === 0;
return this._modules.size === 0;
}

updateHash(hash) {
hash.update(`${this.id} `);
hash.update(this.ids ? this.ids.join(",") : "");
hash.update(`${this.name || ""} `);
this.modules.forEach(m => m.updateHash(hash));
this._modules.forEach(m => m.updateHash(hash));
}

canBeIntegrated(otherChunk) {
Expand All @@ -307,8 +375,8 @@ class Chunk {

modulesSize() {
let count = 0;
for(let i = 0; i < this.modules.length; i++) {
count += this.modules[i].size();
for(let module of this._modules) {
count += module.size();
}
return count;
}
Expand All @@ -325,9 +393,8 @@ class Chunk {

let integratedModulesSize = this.modulesSize();
// only count modules that do not exist in this chunk!
for(let i = 0; i < otherChunk.modules.length; i++) {
const otherModule = otherChunk.modules[i];
if(this.modules.indexOf(otherModule) === -1) {
for(let otherModule of otherChunk._modules) {
if(!this._modules.has(otherModule)) {
integratedModulesSize += otherModule.size();
}
}
Expand Down Expand Up @@ -356,7 +423,7 @@ class Chunk {
}

sortItems() {
this.modules.sort(byId);
this._modules = new Set(Array.from(this._modules).sort(byId));
this.origins.sort((a, b) => {
const aIdent = a.module.identifier();
const bIdent = b.module.identifier();
Expand All @@ -373,7 +440,7 @@ class Chunk {
}

toString() {
return `Chunk[${this.modules.join()}]`;
return `Chunk[${Array.from(this._modules).join()}]`;
}

checkConstraints() {
Expand All @@ -393,4 +460,14 @@ class Chunk {
}
}

Object.defineProperty(Chunk.prototype, "modules", {
configurable: false,
get: util.deprecate(function() {
return Array.from(this._modules);
}, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
set: util.deprecate(function(value) {
this.setModules(value);
}, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")
});

module.exports = Chunk;
13 changes: 8 additions & 5 deletions lib/Compilation.js
Expand Up @@ -80,6 +80,7 @@ class Compilation extends Tapable {
this.children = [];
this.dependencyFactories = new Map();
this.dependencyTemplates = new Map();
this.childrenCounters = {};
}

getStats() {
Expand Down Expand Up @@ -511,7 +512,7 @@ class Compilation extends Tapable {
if(err) return callback(err);
deps.forEach(d => {
if(d.module && d.module.removeReason(module, d)) {
module.chunks.forEach(chunk => {
module.forEachChunk(chunk => {
if(!d.module.hasReasonForChunk(chunk)) {
if(d.module.removeChunk(chunk)) {
this.removeChunkFromDependencies(d.module, chunk);
Expand Down Expand Up @@ -1017,7 +1018,7 @@ class Compilation extends Tapable {

const modules = this.modules;
for(let indexModule = 0; indexModule < modules.length; indexModule++) {
modules[indexModule].sortItems();
modules[indexModule].sortItems(false);
}

const chunks = this.chunks;
Expand All @@ -1031,7 +1032,7 @@ class Compilation extends Tapable {

const modules = this.modules;
for(let indexModule = 0; indexModule < modules.length; indexModule++) {
modules[indexModule].sortItems();
modules[indexModule].sortItems(true);
}

const chunks = this.chunks;
Expand Down Expand Up @@ -1218,8 +1219,10 @@ class Compilation extends Tapable {
return this.mainTemplate.applyPluginsWaterfall("asset-path", filename, data);
}

createChildCompiler(name, outputOptions) {
return this.compiler.createChildCompiler(this, name, outputOptions);
createChildCompiler(name, outputOptions, plugins) {
var idx = (this.childrenCounters[name] || 0);
this.childrenCounters[name] = idx + 1;
return this.compiler.createChildCompiler(this, name, idx, outputOptions, plugins);
}

checkConstraints() {
Expand Down