2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
var Module = require("./Module");
|
|
|
|
var NormalModuleMixin = require("webpack-core/lib/NormalModuleMixin");
|
2013-03-26 23:54:41 +08:00
|
|
|
var SourceMapSource = require("webpack-core/lib/SourceMapSource");
|
|
|
|
var OriginalSource = require("webpack-core/lib/OriginalSource");
|
2013-01-31 01:49:25 +08:00
|
|
|
var RawSource = require("webpack-core/lib/RawSource");
|
2013-03-26 23:54:41 +08:00
|
|
|
var ReplaceSource = require("webpack-core/lib/ReplaceSource");
|
2013-01-31 01:49:25 +08:00
|
|
|
var ModuleParseError = require("./ModuleParseError");
|
2013-06-12 22:16:06 +08:00
|
|
|
var TemplateArgumentDependency = require("./dependencies/TemplateArgumentDependency");
|
2013-01-31 01:49:25 +08:00
|
|
|
var path = require("path");
|
|
|
|
|
|
|
|
function NormalModule(request, userRequest, rawRequest, loaders, resource, parser) {
|
|
|
|
Module.call(this);
|
|
|
|
this.request = request;
|
|
|
|
this.userRequest = userRequest;
|
|
|
|
this.rawRequest = rawRequest;
|
|
|
|
this.parser = parser;
|
|
|
|
NormalModuleMixin.call(this, loaders, resource);
|
2013-02-26 19:36:34 +08:00
|
|
|
this.meta = {};
|
2013-01-31 01:49:25 +08:00
|
|
|
this.assets = {};
|
|
|
|
this.built = false;
|
|
|
|
}
|
|
|
|
module.exports = NormalModule;
|
|
|
|
|
|
|
|
NormalModule.prototype = Object.create(Module.prototype);
|
|
|
|
NormalModuleMixin.mixin(NormalModule.prototype);
|
|
|
|
|
|
|
|
NormalModule.prototype.identifier = function() {
|
|
|
|
return this.request;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.readableIdentifier = function(requestShortener) {
|
|
|
|
return requestShortener.shorten(this.userRequest);
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.fillLoaderContext = function fillLoaderContext(loaderContext, options, compilation) {
|
|
|
|
loaderContext.webpack = true;
|
2013-03-26 23:54:41 +08:00
|
|
|
loaderContext.emitFile = function(name, content, sourceMap) {
|
|
|
|
if(typeof sourceMap === "string") {
|
|
|
|
this.assets[name] = new OriginalSource(content, sourceMap);
|
|
|
|
} else if(sourceMap) {
|
|
|
|
this.assets[name] = new SourceMapSource(content, name, sourceMap);
|
|
|
|
} else {
|
|
|
|
this.assets[name] = new RawSource(content);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}.bind(this);
|
2013-12-20 05:31:12 +08:00
|
|
|
loaderContext._module = this;
|
2013-01-31 01:49:25 +08:00
|
|
|
loaderContext._compilation = compilation;
|
|
|
|
loaderContext._compiler = compilation.compiler;
|
2013-12-20 05:31:12 +08:00
|
|
|
compilation.applyPlugins("normal-module-loader", loaderContext, this);
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.disconnect = function disconnect() {
|
|
|
|
this.built = false;
|
|
|
|
Module.prototype.disconnect.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.build = function build(options, compilation, resolver, fs, callback) {
|
|
|
|
this.buildTimestamp = new Date().getTime();
|
|
|
|
this.built = true;
|
|
|
|
return this.doBuild(options, compilation, resolver, fs, function(err) {
|
|
|
|
if(err) return callback(err);
|
2014-01-29 17:13:28 +08:00
|
|
|
this.dependencies.length = 0;
|
|
|
|
this.variables.length = 0;
|
|
|
|
this.blocks.length = 0;
|
2014-02-13 18:41:10 +08:00
|
|
|
this._cachedSource = null;
|
2013-10-14 19:59:44 +08:00
|
|
|
if(options.module && options.module.noParse) {
|
|
|
|
if(Array.isArray(options.module.noParse)) {
|
|
|
|
if(options.module.noParse.some(function(regExp) {
|
2014-02-04 19:21:01 +08:00
|
|
|
return typeof regExp === "string" ?
|
|
|
|
this.request.indexOf(regExp) === 0 :
|
|
|
|
regExp.test(this.request);
|
2013-10-14 19:59:44 +08:00
|
|
|
}, this)) return callback();
|
2014-02-04 19:21:01 +08:00
|
|
|
} else if(typeof regExp === "string" ?
|
|
|
|
this.request.indexOf(options.module.noParse) === 0 :
|
|
|
|
options.module.noParse.test(this.request)) {
|
2013-10-14 19:59:44 +08:00
|
|
|
return callback();
|
2014-02-04 19:21:01 +08:00
|
|
|
}
|
2013-10-14 19:59:44 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
try {
|
2013-02-05 15:31:46 +08:00
|
|
|
this.parser.parse(this._source.source(), {
|
|
|
|
current: this,
|
2013-06-20 18:04:31 +08:00
|
|
|
module: this,
|
|
|
|
compilation: compilation,
|
|
|
|
options: options
|
2013-02-05 15:31:46 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
} catch(e) {
|
|
|
|
var source = this._source.source();
|
|
|
|
this._source = null;
|
|
|
|
return callback(new ModuleParseError(this, source, e));
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
|
2014-02-13 18:41:10 +08:00
|
|
|
if(this._cachedSource) return this._cachedSource;
|
2013-01-31 01:49:25 +08:00
|
|
|
var _source = this._source;
|
|
|
|
if(!_source) return new RawSource("throw new Error('No source availible');");
|
2014-02-13 18:41:10 +08:00
|
|
|
var source = this._cachedSource = new ReplaceSource(_source);
|
2013-10-01 16:11:25 +08:00
|
|
|
var topLevelBlock = this;
|
2013-01-31 01:49:25 +08:00
|
|
|
function doDep(dep) {
|
|
|
|
var template = dependencyTemplates.get(dep.Class);
|
|
|
|
if(!template) throw new Error("No template for dependency: " + dep.Class.name);
|
2013-06-12 22:16:06 +08:00
|
|
|
template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
function doVariable(vars, variable) {
|
|
|
|
var name = variable.name;
|
|
|
|
var expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
|
|
|
|
vars.push({name: name, expression: expr});
|
|
|
|
}
|
|
|
|
function doBlock(block) {
|
|
|
|
block.dependencies.forEach(doDep);
|
|
|
|
block.blocks.forEach(doBlock);
|
|
|
|
if(block.variables.length > 0) {
|
|
|
|
var vars = [];
|
|
|
|
block.variables.forEach(doVariable.bind(null, vars));
|
|
|
|
var varNames = [];
|
|
|
|
var varExpressions = [];
|
|
|
|
var varStartCode = "";
|
|
|
|
var varEndCode = "";
|
|
|
|
function emitFunction() {
|
|
|
|
if(varNames.length == 0) return;
|
2013-02-26 19:36:34 +08:00
|
|
|
|
2014-03-03 21:56:17 +08:00
|
|
|
varStartCode += "/* WEBPACK VAR INJECTION */(function(" + varNames.join(", ") + ") {";
|
2013-10-01 16:11:25 +08:00
|
|
|
// exports === this in the topLevelBlock, but exports do compress better...
|
2014-03-03 21:56:17 +08:00
|
|
|
varEndCode = (topLevelBlock === block ? "}.call(exports, " : "}.call(this, ") +
|
2013-10-01 16:11:25 +08:00
|
|
|
varExpressions.map(function(e) {return e.source()}).join(", ") + "))" + varEndCode;
|
2013-02-26 19:36:34 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
varNames.length = 0;
|
|
|
|
varExpressions.length = 0;
|
|
|
|
}
|
|
|
|
vars.forEach(function(v) {
|
|
|
|
if(varNames.indexOf(v.name) >= 0) emitFunction();
|
|
|
|
varNames.push(v.name);
|
|
|
|
varExpressions.push(v.expression);
|
|
|
|
});
|
|
|
|
emitFunction();
|
|
|
|
var start = block.range ? block.range[0] : 0;
|
|
|
|
var end = block.range ? block.range[1] : _source.size();
|
2014-02-12 21:53:33 +08:00
|
|
|
if(varStartCode) source.insert(start + 0.5, varStartCode);
|
|
|
|
if(varEndCode) source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
doBlock(this);
|
|
|
|
return source;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.needRebuild = function needRebuild(fileTimestamps, contextTimestamps) {
|
|
|
|
var timestamp = 0;
|
|
|
|
this.fileDependencies.forEach(function(file) {
|
|
|
|
var ts = fileTimestamps[file];
|
|
|
|
if(!ts) timestamp = Infinity;
|
|
|
|
if(ts > timestamp) timestamp = ts;
|
|
|
|
});
|
|
|
|
this.contextDependencies.forEach(function(context) {
|
|
|
|
var ts = contextTimestamps[context];
|
|
|
|
if(!ts) timestamp = Infinity;
|
|
|
|
if(ts > timestamp) timestamp = ts;
|
|
|
|
});
|
2013-02-01 15:03:38 +08:00
|
|
|
return timestamp >= this.buildTimestamp;
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.size = function() {
|
|
|
|
return this._source ? this._source.size() : -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.updateHash = function(hash) {
|
|
|
|
if(this._source) {
|
|
|
|
hash.update("source");
|
|
|
|
this._source.updateHash(hash);
|
|
|
|
} else
|
|
|
|
hash.update("null");
|
|
|
|
Module.prototype.updateHash.call(this, hash);
|
|
|
|
};
|
2013-06-12 22:16:06 +08:00
|
|
|
|
|
|
|
NormalModule.prototype.getSourceHash = function() {
|
|
|
|
if(!this._source) return "";
|
2013-07-11 05:20:07 +08:00
|
|
|
var hash = require("crypto").createHash("md5");
|
2013-06-12 22:16:06 +08:00
|
|
|
hash.update(this._source.source());
|
|
|
|
return hash.digest("hex");
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.getAllModuleDependencies = function() {
|
|
|
|
var list = [];
|
|
|
|
function doDep(dep) {
|
|
|
|
if(dep.module && list.indexOf(dep.module) < 0)
|
|
|
|
list.push(dep.module);
|
|
|
|
}
|
|
|
|
function doVariable(variable) {
|
|
|
|
variable.dependencies.forEach(doDep);
|
|
|
|
}
|
|
|
|
function doBlock(block) {
|
|
|
|
block.variables.forEach(doVariable);
|
|
|
|
block.dependencies.forEach(doDep);
|
|
|
|
block.blocks.forEach(doBlock);
|
|
|
|
}
|
|
|
|
doBlock(this);
|
|
|
|
return list;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.createTemplate = function(keepModules) {
|
|
|
|
var template = new NormalModule("", "", "", [], "", null);
|
|
|
|
template._source = this._source;
|
|
|
|
template.built = this.built;
|
|
|
|
template.templateModules = keepModules;
|
2013-10-28 23:21:29 +08:00
|
|
|
template._templateOrigin = this;
|
|
|
|
template.readableIdentifier = function() {
|
|
|
|
return "template of " + this._templateOrigin.id + " referencing " + keepModules.map(function(m) {
|
|
|
|
return m.id;
|
|
|
|
}).join(", ");
|
|
|
|
};
|
2013-06-12 22:16:06 +08:00
|
|
|
var args = template.arguments = [];
|
|
|
|
function doDeps(deps) {
|
|
|
|
return deps.map(function(dep) {
|
2014-01-24 01:35:56 +08:00
|
|
|
if(dep.module && keepModules.indexOf(dep.module) < 0) {
|
2013-06-12 22:16:06 +08:00
|
|
|
var argName = "__webpack_module_template_argument_" + args.length + "__";
|
|
|
|
args.push(argName);
|
|
|
|
return new TemplateArgumentDependency(argName, dep);
|
|
|
|
} else {
|
|
|
|
return dep;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function doVariable(variable, newVariable) {
|
|
|
|
variable.dependencies.forEach(doDep);
|
|
|
|
}
|
|
|
|
function doBlock(block, newBlock) {
|
|
|
|
block.variables.forEach(function(variable) {
|
|
|
|
var newDependencies = doDeps(variable.dependencies);
|
|
|
|
newBlock.addVariable(variable.name, variable.expression, newDependencies);
|
|
|
|
});
|
|
|
|
newBlock.dependencies = doDeps(block.dependencies);
|
|
|
|
block.blocks.forEach(function(childBlock) {
|
2014-01-23 22:31:40 +08:00
|
|
|
var newChildBlock = new AsyncDependenciesBlock(childBlock.name, childBlock.module, childBlock.loc);
|
2013-06-12 22:16:06 +08:00
|
|
|
newBlock.addBlock(newChildBlock);
|
|
|
|
doBlock(childBlock, newChildBlock);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
doBlock(this, template);
|
|
|
|
return template;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.getTemplateArguments = function(keepModules) {
|
|
|
|
var list = [];
|
|
|
|
function doDep(dep) {
|
|
|
|
if(dep.module && keepModules.indexOf(dep.module) < 0)
|
|
|
|
list.push(dep.module);
|
|
|
|
}
|
|
|
|
function doVariable(variable) {
|
|
|
|
variable.dependencies.forEach(doDep);
|
|
|
|
}
|
|
|
|
function doBlock(block) {
|
|
|
|
block.variables.forEach(doVariable);
|
|
|
|
block.dependencies.forEach(doDep);
|
|
|
|
block.blocks.forEach(doBlock);
|
|
|
|
}
|
|
|
|
doBlock(this);
|
|
|
|
return list;
|
|
|
|
};
|