better code style

This commit is contained in:
Tobias Koppers 2014-06-03 21:03:21 +02:00
parent 560762a932
commit 80f3c48d99
3 changed files with 251 additions and 105 deletions

View File

@ -17,7 +17,9 @@ module.exports = function(optimist, argv, convertOptions) {
if(argv.d) {
argv.debug = true;
argv["output-pathinfo"] = true;
if(!argv.devtool) argv.devtool = "sourcemap";
if(!argv.devtool) {
argv.devtool = "sourcemap";
}
}
if(argv.p) {
argv["optimize-minimize"] = true;
@ -26,27 +28,40 @@ module.exports = function(optimist, argv, convertOptions) {
function ifArg(name, fn, init, finalize) {
if(Array.isArray(argv[name])) {
if(init) init();
if(init) {
init();
}
argv[name].forEach(fn);
if(finalize) finalize();
if(finalize) {
finalize();
}
} else if(typeof argv[name] != "undefined") {
if(init) init();
if(init) {
init();
}
fn(argv[name], -1);
if(finalize) finalize();
if(finalize) {
finalize();
}
}
}
function ifArgPair(name, fn, init, finalize) {
ifArg(name, function(content, idx) {
var i = content.indexOf("=");
if(i < 0) return fn(null, content, idx);
else return fn(content.substr(0, i), content.substr(i+1), idx);
if(i < 0) {
return fn(null, content, idx);
} else {
return fn(content.substr(0, i), content.substr(i+1), idx);
}
}, init, finalize);
}
function ifBooleanArg(name, fn) {
ifArg(name, function(bool) {
if(bool) fn();
if(bool) {
fn();
}
});
}
@ -58,7 +73,9 @@ module.exports = function(optimist, argv, convertOptions) {
function mapArgToBooleanInverse(name, optionName) {
ifArg(name, function(bool) {
if(!bool) options[optionName || name] = false;
if(!bool) {
options[optionName || name] = false;
}
});
}
@ -69,14 +86,16 @@ module.exports = function(optimist, argv, convertOptions) {
}
function loadPlugin(name) {
var path;
try {
var path = resolve.sync(process.cwd(), name);
path = resolve.sync(process.cwd(), name);
} catch(e) {
console.log("Cannot resolve plugin " + name + ".");
process.exit(-1);
}
var Plugin;
try {
var Plugin = require(path);
Plugin = require(path);
} catch(e) {
console.log("Cannot load plugin " + name + ". (" + path + ")");
throw e;
@ -90,29 +109,34 @@ module.exports = function(optimist, argv, convertOptions) {
}
function ensureObject(parent, name) {
if(typeof parent[name] != "object" || parent[name] === null)
if(typeof parent[name] !== "object" || parent[name] === null) {
parent[name] = {};
}
}
function ensureArray(parent, name) {
if(!Array.isArray(parent[name]))
if(!Array.isArray(parent[name])) {
parent[name] = [];
}
}
if(argv.config) {
options = require(path.resolve(argv.config));
} else {
var configPath = path.resolve("webpack.config.js");
if(fs.existsSync(configPath))
if(fs.existsSync(configPath)) {
options = require(configPath);
}
}
if(typeof options != "object" || options === null) {
if(typeof options !== "object" || options === null) {
console.log("Config did not export a object.");
process.exit(-1);
}
mapArgToPath("context", "context");
if(!options.context) options.context = process.cwd();
if(!options.context) {
options.context = process.cwd();
}
ifArgPair("entry", function(name, entry) {
options.entry[name] = entry;
@ -121,7 +145,9 @@ module.exports = function(optimist, argv, convertOptions) {
});
ifArgPair("module-bind", function(name, binding) {
if(name === null) name = binding;
if(name === null) {
name = binding;
}
options.module.loaders.push({
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
loader: binding
@ -132,7 +158,9 @@ module.exports = function(optimist, argv, convertOptions) {
});
ifArgPair("module-bind-pre", function(name, binding) {
if(name === null) name = binding;
if(name === null) {
name = binding;
}
options.module.preLoaders.push({
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
loader: binding
@ -143,7 +171,9 @@ module.exports = function(optimist, argv, convertOptions) {
});
ifArgPair("module-bind-post", function(name, binding) {
if(name === null) name = binding;
if(name === null) {
name = binding;
}
options.module.postLoaders.push({
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
loader: binding
@ -161,7 +191,7 @@ module.exports = function(optimist, argv, convertOptions) {
}
defineObject[name] = value;
}, function() {
defineObject = {}
defineObject = {};
}, function() {
ensureArray(options, "plugins");
var DefinePlugin = require("../lib/DefinePlugin");
@ -252,20 +282,26 @@ module.exports = function(optimist, argv, convertOptions) {
ifBooleanArg("progress", function() {
var ProgressPlugin = require("../lib/ProgressPlugin");
ensureArray(options, "plugins");
var chars = 0, lastState;
var chars = 0, lastState, lastStateTime;
options.plugins.push(new ProgressPlugin(function(percentage, msg) {
var state = msg;
if(percentage < 1) {
percentage = Math.floor(percentage * 100);
msg = percentage + "% " + msg;
if(percentage < 100) msg = " " + msg;
if(percentage < 10) msg = " " + msg;
if(percentage < 100) {
msg = " " + msg;
}
if(percentage < 10) {
msg = " " + msg;
}
}
for(; chars > msg.length; chars--)
for(; chars > msg.length; chars--) {
process.stderr.write("\b \b");
}
chars = msg.length;
for(var i = 0; i < chars; i++)
for(var i = 0; i < chars; i++) {
process.stderr.write("\b");
}
if(options.profile) {
state = state.replace(/^\d+\/\d+\s+/, "");
if(percentage === 0) {
@ -289,14 +325,18 @@ module.exports = function(optimist, argv, convertOptions) {
});
ifArgPair("resolve-alias", function(name, value) {
if(!name) throw new Error("--resolve-alias <string>=<string>");
if(!name) {
throw new Error("--resolve-alias <string>=<string>");
}
ensureObject(options, "resolve");
ensureObject(options.resolve, "alias");
options.resolve.alias[name] = value;
});
ifArgPair("resolve-loader-alias", function(name, value) {
if(!name) throw new Error("--resolve-loader-alias <string>=<string>");
if(!name) {
throw new Error("--resolve-loader-alias <string>=<string>");
}
ensureObject(options, "resolveLoader");
ensureObject(options.resolveLoader, "alias");
options.resolveLoader.alias[name] = value;
@ -343,11 +383,12 @@ module.exports = function(optimist, argv, convertOptions) {
ifArg("provide", function(value) {
ensureArray(options, "plugins");
var idx = value.indexOf("=");
var name;
if(idx >= 0) {
var name = value.substr(0, idx);
name = value.substr(0, idx);
value = value.substr(idx + 1);
} else {
var name = value;
name = value;
}
var ProvidePlugin = require("../lib/ProvidePlugin");
options.plugins.push(new ProvidePlugin(name, value));
@ -387,8 +428,9 @@ module.exports = function(optimist, argv, convertOptions) {
ensureObject(options, "entry");
function addTo(name, entry) {
if(options.entry[name]) {
if(!Array.isArray(options.entry[name]))
if(!Array.isArray(options.entry[name])) {
options.entry[name] = [options.entry[name]];
}
options.entry[name].push(entry);
} else {
options.entry[name] = entry;
@ -399,9 +441,14 @@ module.exports = function(optimist, argv, convertOptions) {
var j = content.indexOf("?");
if(i < 0 || (j >= 0 && j < i)) {
var resolved = path.resolve(content);
if(fs.existsSync(resolved)) addTo("main", resolved);
else addTo("main", content);
} else addTo(content.substr(0, i), content.substr(i+1))
if(fs.existsSync(resolved)) {
addTo("main", resolved);
} else {
addTo("main", content);
}
} else {
addTo(content.substr(0, i), content.substr(i+1));
}
});
}

View File

@ -14,12 +14,20 @@ function Chunk(name, module, loc) {
this.rendered = false;
this.entry = false;
this.initial = false;
if(module) this.origins.push({module: module, loc: loc, name: name});
if(module) {
this.origins.push({
module: module,
loc: loc,
name: name
});
}
}
module.exports = Chunk;
Chunk.prototype.addModule = function(module) {
if(this.modules.indexOf(module) >= 0) return false;
if(this.modules.indexOf(module) >= 0) {
return false;
}
this.modules.push(module);
return true;
};
@ -33,8 +41,12 @@ Chunk.prototype.removeModule = function(module) {
};
Chunk.prototype.addChunk = function(chunk) {
if(chunk === this) return false;
if(this.chunks.indexOf(chunk) >= 0) return false;
if(chunk === this) {
return false;
}
if(this.chunks.indexOf(chunk) >= 0) {
return false;
}
this.chunks.push(chunk);
return true;
};
@ -48,8 +60,12 @@ Chunk.prototype.removeChunk = function(chunk) {
};
Chunk.prototype.addParent = function(chunk) {
if(chunk === this) return false;
if(this.parents.indexOf(chunk) >= 0) return false;
if(chunk === this) {
return false;
}
if(this.parents.indexOf(chunk) >= 0) {
return false;
}
this.parents.push(chunk);
return true;
};
@ -63,7 +79,9 @@ Chunk.prototype.removeParent = function(chunk) {
};
Chunk.prototype.addBlock = function(block) {
if(this.blocks.indexOf(block) >= 0) return false;
if(this.blocks.indexOf(block) >= 0) {
return false;
}
this.blocks.push(block);
return true;
};
@ -79,16 +97,18 @@ Chunk.prototype.remove = function(reason) {
}, this);
this.parents.forEach(function(c) {
var idx = c.chunks.indexOf(this);
if(idx >= 0)
if(idx >= 0) {
c.chunks.splice(idx, 1);
}
this.chunks.forEach(function(cc) {
cc.addParent(c);
});
}, this);
this.chunks.forEach(function(c) {
var idx = c.parents.indexOf(this);
if(idx >= 0)
if(idx >= 0) {
c.parents.splice(idx, 1);
}
this.parents.forEach(function(cc) {
cc.addChunk(c);
});
@ -100,7 +120,9 @@ Chunk.prototype.remove = function(reason) {
};
Chunk.prototype.integrate = function(other, reason) {
if(!this.canBeIntegrated(other)) return false;
if(!this.canBeIntegrated(other)) {
return false;
}
var otherModules = other.modules.slice();
otherModules.forEach(function(m) {
@ -111,18 +133,22 @@ Chunk.prototype.integrate = function(other, reason) {
other.modules.length = 0;
other.parents.forEach(function(c) {
var idx = c.chunks.indexOf(other);
if(idx >= 0)
if(idx >= 0) {
c.chunks.splice(idx, 1);
if(c !== this && this.addParent(c))
}
if(c !== this && this.addParent(c)) {
c.addChunk(this);
}
}, this);
other.parents.length = 0;
other.chunks.forEach(function(c) {
var idx = c.parents.indexOf(other);
if(idx >= 0)
if(idx >= 0) {
c.parents.splice(idx, 1);
if(c !== this && this.addChunk(c))
}
if(c !== this && this.addChunk(c)) {
c.addParent(this);
}
}, this);
other.chunks.length = 0;
other.blocks.forEach(function(b) {
@ -132,15 +158,18 @@ Chunk.prototype.integrate = function(other, reason) {
}, this);
other.blocks.length = 0;
other.origins.forEach(function(origin) {
if(!origin.reasons) origin.reasons = [reason];
else if(origin.reasons[0] !== reason) origin.reasons.unshift(reason);
if(!origin.reasons) {
origin.reasons = [reason];
} else if(origin.reasons[0] !== reason) {
origin.reasons.unshift(reason);
}
this.origins.push(origin);
}, this);
return true;
};
Chunk.prototype.isEmpty = function() {
return (this.modules.length == 0);
return (this.modules.length === 0);
};
Chunk.prototype.updateHash = function(hash) {
@ -165,24 +194,31 @@ Chunk.prototype.size = function(options) {
};
Chunk.prototype.canBeIntegrated = function(other) {
if(other.initial) return false;
if(other.initial) {
return false;
}
if(this.initial) {
if(other.parents.length !== 1 || other.parents[0] !== this) return false;
if(other.parents.length !== 1 || other.parents[0] !== this) {
return false;
}
}
return true;
};
Chunk.prototype.integratedSize = function(other, options) {
// Chunk if it's possible to integrate this chunks
if(!this.canBeIntegrated(other)) return false;
if(!this.canBeIntegrated(other)) {
return false;
}
var CHUNK_OVERHEAD = options.chunkOverhead || 10000;
var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
var mergedModules = this.modules.slice();
other.modules.forEach(function(m) {
if(this.modules.indexOf(m) < 0)
if(this.modules.indexOf(m) < 0) {
mergedModules.push(m);
}
}, this);
var modulesSize = mergedModules.map(function(m) {

View File

@ -60,7 +60,9 @@ Compilation.prototype = Object.create(Tapable.prototype);
Compilation.prototype.addModule = function(module, cacheGroup) {
cacheGroup = cacheGroup || "m";
var identifier = module.identifier();
if(this._modules[identifier]) return false;
if(this._modules[identifier]) {
return false;
}
if(this.cache && this.cache[cacheGroup + identifier]) {
var cacheModule = this.cache[cacheGroup + identifier];
@ -85,7 +87,9 @@ Compilation.prototype.addModule = function(module, cacheGroup) {
}
}
this._modules[identifier] = module;
if(this.cache) this.cache[cacheGroup + identifier] = module;
if(this.cache) {
this.cache[cacheGroup + identifier] = module;
}
this.modules.push(module);
return true;
};
@ -129,17 +133,24 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
var dependencies = [];
function addDependency(dep) {
for(var i = 0; i < dependencies.length; i++) {
if(dep.isEqualResource(dependencies[i][0]))
if(dep.isEqualResource(dependencies[i][0])) {
return dependencies[i].push(dep);
}
}
dependencies.push([dep]);
}
function addDependenciesBlock(block) {
if(block.dependencies) block.dependencies.forEach(addDependency);
if(block.blocks) block.blocks.forEach(addDependenciesBlock);
if(block.variables) block.variables.forEach(function(v) {
v.dependencies.forEach(addDependency);
});
if(block.dependencies) {
block.dependencies.forEach(addDependency);
}
if(block.blocks) {
block.blocks.forEach(addDependenciesBlock);
}
if(block.variables) {
block.variables.forEach(function(v) {
v.dependencies.forEach(addDependency);
});
}
}
addDependenciesBlock(module);
this.addModuleDependencies(module, dependencies, this.bail, null, true, callback);
@ -149,13 +160,16 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
var factories = [];
for(var i = 0; i < dependencies.length; i++) {
var factory = this.dependencyFactories.get(dependencies[i][0].Class);
if(!factory)
if(!factory) {
return callback(new Error("No module factory availible for dependency type: " + dependencies[i][0].Class.name));
}
factories[i] = [factory, dependencies[i]];
}
async.forEach(factories, function(item, callback) {
var dependencies = item[1];
var criticalDependencies = dependencies.filter(function(d) { return !!d.critical });
var criticalDependencies = dependencies.filter(function(d) {
return !!d.critical;
});
if(criticalDependencies.length > 0) {
this.warnings.push(new CriticalDependenciesWarning(module, criticalDependencies));
}
@ -165,10 +179,11 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
err.origin = module;
module.dependenciesErrors.push(err);
this.errors.push(err);
if(bail)
if(bail) {
callback(err);
else
} else {
callback();
}
}.bind(this);
var warningAndCallback = function warningAndCallback(err) {
err.dependencies = dependencies;
@ -179,21 +194,30 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
}.bind(this);
var factory = item[0];
if(this.profile) var start = +new Date();
var start = this.profile && +new Date();
factory.create(module.context, dependencies[0], function(err, dependantModule) {
function isOptional() {
return dependencies.filter(function(d) { return !d.optional }).length == 0;
return dependencies.filter(function(d) {
return !d.optional;
}).length == 0;
}
function errorOrWarningAndCallback(err) {
if(isOptional())
if(isOptional()) {
return warningAndCallback(err);
else
} else {
return errorAndCallback(err);
}
}
if(err) {
return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
}
if(!dependantModule) {
return callback();
}
if(err) return errorOrWarningAndCallback(new ModuleNotFoundError(module, err));
if(!dependantModule) return callback();
if(this.profile) {
if(!dependantModule.profile) dependantModule.profile = {};
if(!dependantModule.profile) {
dependantModule.profile = {};
}
var afterFactory = +new Date();
dependantModule.profile.factory = afterFactory - start;
}
@ -204,9 +228,11 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
if(!newModule) { // from cache
dependantModule = this.getModule(dependantModule);
if(dependantModule.id === 0) return errorOrWarningAndCallback(
new ModuleNotFoundError(module, new Error("a dependency to an entry point is not allowed"))
);
if(dependantModule.id === 0) {
return errorOrWarningAndCallback(
new ModuleNotFoundError(module, new Error("a dependency to an entry point is not allowed"))
);
}
dependencies.forEach(function(dep) {
dep.module = dependantModule;
@ -214,18 +240,22 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
});
if(this.profile) {
if(!module.profile) module.profile = {};
if(!module.profile) {
module.profile = {};
}
var time = +new Date() - start;
if(!module.profile.dependencies || time > module.profile.dependencies)
if(!module.profile.dependencies || time > module.profile.dependencies) {
module.profile.dependencies = time;
}
}
return callback();
}
if(newModule instanceof Module) {
if(this.profile)
if(this.profile) {
newModule.profile = dependantModule.profile;
}
newModule.issuer = dependantModule.issuer;
dependantModule = newModule;
@ -235,10 +265,11 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
dependantModule.addReason(module, dep);
});
if(recursive)
if(recursive) {
return this.processModuleDependencies(dependantModule, callback);
else
} else {
return callback();
}
}
dependencies.forEach(function(dep) {
@ -247,22 +278,27 @@ Compilation.prototype.addModuleDependencies = function(module, dependencies, bai
});
this.buildModule(dependantModule, function(err) {
if(err) return errorOrWarningAndCallback(err);
if(err) {
return errorOrWarningAndCallback(err);
}
if(this.profile) {
var afterBuilding = +new Date();
dependantModule.profile.building = afterBuilding - afterFactory;
}
if(recursive)
if(recursive) {
this.processModuleDependencies(dependantModule, callback);
else
} else {
return callback();
}
}.bind(this));
}.bind(this));
}.bind(this), function(err) {
if(err) callback(err);
if(err) {
callback(err);
}
return callback();
});
@ -277,19 +313,25 @@ Compilation.prototype._addModuleChain = function process(context, dependency, on
callback();
}.bind(this);
if(!(typeof dependency == "object" && dependency != null && dependency.Class))
if(typeof dependency !== "object" || dependency === null || !dependency.Class) {
throw new Error("Parameter 'dependency' must be a Dependency");
}
var moduleFactory = this.dependencyFactories.get(dependency.Class);
if(!moduleFactory)
if(!moduleFactory) {
throw new Error("No dependency factory availible for this dependency type: " + dependency.Class.name);
}
if(this.profile) var start = +new Date();
var start = this.profile && +new Date();
moduleFactory.create(context, dependency, function(err, module) {
if(err) return errorAndCallback(new EntryModuleNotFoundError(err));
if(err) {
return errorAndCallback(new EntryModuleNotFoundError(err));
}
if(this.profile) {
if(!module.profile) module.profile = {};
if(!module.profile) {
module.profile = {};
}
var afterFactory = +new Date();
module.profile.factory = afterFactory - start;
}
@ -317,7 +359,9 @@ Compilation.prototype._addModuleChain = function process(context, dependency, on
moduleReady.call(this);
} else {
this.buildModule(module, function(err) {
if(err) return errorAndCallback(err);
if(err) {
return errorAndCallback(err);
}
if(this.profile) {
var afterBuilding = +new Date();
@ -330,7 +374,9 @@ Compilation.prototype._addModuleChain = function process(context, dependency, on
function moduleReady() {
this.processModuleDependencies(module, function(err) {
if(err) return callback(err);
if(err) {
return callback(err);
}
return callback(null, module);
}.bind(this));
@ -346,10 +392,14 @@ Compilation.prototype.addEntry = function process(context, entry, name, callback
module.id = 0;
}.bind(this), function(err, module) {
if(err) return callback(err);
if(err) {
return callback(err);
}
if(module) {
if(module.reasons.length > 0) return callback(new Error("module cannot be added as entry point, because its already in the bundle"));
if(module.reasons.length > 0) {
return callback(new Error("module cannot be added as entry point, because its already in the bundle"));
}
this.preparedChunks.push({
name: name,
module: module
@ -386,7 +436,9 @@ Compilation.prototype.seal = function seal(callback) {
this.applyPlugins("after-optimize-chunks", this.chunks);
this.applyPluginsAsync("optimize-tree", this.chunks, this.modules, function(err) {
if(err) return callback(err);
if(err) {
return callback(err);
}
this.applyPlugins("revive-modules", this.modules, this.records);
this.applyPlugins("optimize-module-order", this.modules);
@ -413,12 +465,18 @@ Compilation.prototype.seal = function seal(callback) {
this.applyPlugins("record", this, this.records);
this.applyPluginsAsync("additional-assets", function(err) {
if(err) return callback(err);
if(err) {
return callback(err);
}
this.applyPluginsAsync("optimize-chunk-assets", this.chunks, function(err) {
if(err) return callback(err);
if(err) {
return callback(err);
}
this.applyPlugins("after-optimize-chunk-assets", this.chunks);
this.applyPluginsAsync("optimize-assets", this.assets, function(err) {
if(err) return callback(err);
if(err) {
return callback(err);
}
this.applyPlugins("after-optimize-assets", this.assets);
return callback();
}.bind(this));
@ -428,14 +486,17 @@ Compilation.prototype.seal = function seal(callback) {
};
Compilation.prototype.addChunk = function addChunk(name, module, loc) {
var chunk;
if(name) {
if(Object.prototype.hasOwnProperty.call(this.namedChunks, name)) {
var chunk = this.namedChunks[name];
if(module) chunk.addOrigin(module, loc);
chunk = this.namedChunks[name];
if(module) {
chunk.addOrigin(module, loc);
}
return chunk;
}
}
var chunk = new Chunk(name, module, loc);
chunk = new Chunk(name, module, loc);
this.chunks.push(chunk);
if(name) {
this.namedChunks[name] = chunk;
@ -458,7 +519,9 @@ Compilation.prototype.processDependenciesBlockForChunk = function processDepende
this.processDependenciesBlockForChunk(b, c);
}, this);
function iteratorDependency(d) {
if(!d.module) return;
if(!d.module) {
return;
}
if(d.module.error) {
d.module = null;
return;
@ -485,11 +548,11 @@ Compilation.prototype.applyModuleIds = function applyModuleIds() {
Compilation.prototype.applyChunkIds = function applyChunkIds() {
this.chunks.forEach(function(chunk) {
if(chunk.id === null) {
if(chunk.id === null)
chunk.id = this.nextFreeChunkId++;
chunk.id = this.nextFreeChunkId++;
}
if(!chunk.ids)
if(!chunk.ids) {
chunk.ids = [chunk.id];
}
}, this);
};