2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2015-05-17 00:27:59 +08:00
|
|
|
var path = require("path");
|
2013-01-31 01:49:25 +08:00
|
|
|
var Module = require("./Module");
|
2015-12-30 00:44:55 +08:00
|
|
|
var SourceMapSource = require("webpack-sources").SourceMapSource;
|
|
|
|
var OriginalSource = require("webpack-sources").OriginalSource;
|
|
|
|
var RawSource = require("webpack-sources").RawSource;
|
|
|
|
var ReplaceSource = require("webpack-sources").ReplaceSource;
|
|
|
|
var CachedSource = require("webpack-sources").CachedSource;
|
2016-01-04 04:42:56 +08:00
|
|
|
var LineToLineMappedSource = require("webpack-sources").LineToLineMappedSource;
|
2013-01-31 01:49:25 +08:00
|
|
|
var ModuleParseError = require("./ModuleParseError");
|
|
|
|
|
2016-01-04 04:42:56 +08:00
|
|
|
var ModuleBuildError = require("./ModuleBuildError");
|
|
|
|
var ModuleError = require("./ModuleError");
|
|
|
|
var ModuleWarning = require("./ModuleWarning");
|
|
|
|
|
|
|
|
var runLoaders = require("loader-runner").runLoaders;
|
|
|
|
var getContext = require("loader-runner").getContext;
|
|
|
|
|
|
|
|
function asString(buf) {
|
|
|
|
if(Buffer.isBuffer(buf)) {
|
|
|
|
return buf.toString("utf-8");
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function NormalModule(request, userRequest, rawRequest, loaders, resource, parser) {
|
|
|
|
Module.call(this);
|
|
|
|
this.request = request;
|
|
|
|
this.userRequest = userRequest;
|
|
|
|
this.rawRequest = rawRequest;
|
|
|
|
this.parser = parser;
|
2016-01-04 04:42:56 +08:00
|
|
|
this.resource = resource;
|
|
|
|
this.context = getContext(resource);
|
|
|
|
this.loaders = loaders;
|
|
|
|
this.fileDependencies = [];
|
|
|
|
this.contextDependencies = [];
|
|
|
|
this.warnings = [];
|
|
|
|
this.errors = [];
|
|
|
|
this.error = null;
|
|
|
|
this._source = null;
|
2013-01-31 01:49:25 +08:00
|
|
|
this.assets = {};
|
|
|
|
this.built = false;
|
2014-05-17 06:31:52 +08:00
|
|
|
this._cachedSource = null;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
module.exports = NormalModule;
|
|
|
|
|
|
|
|
NormalModule.prototype = Object.create(Module.prototype);
|
2016-05-20 13:39:36 +08:00
|
|
|
NormalModule.prototype.constructor = NormalModule;
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
NormalModule.prototype.identifier = function() {
|
|
|
|
return this.request;
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.readableIdentifier = function(requestShortener) {
|
|
|
|
return requestShortener.shorten(this.userRequest);
|
|
|
|
};
|
|
|
|
|
2015-05-17 00:27:59 +08:00
|
|
|
function contextify(options, request) {
|
|
|
|
return request.split("!").map(function(r) {
|
|
|
|
var rp = path.relative(options.context, r);
|
|
|
|
if(path.sep === "\\")
|
|
|
|
rp = rp.replace(/\\/g, "/");
|
|
|
|
if(rp.indexOf("../") !== 0)
|
|
|
|
rp = "./" + rp;
|
|
|
|
return rp;
|
|
|
|
}).join("!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NormalModule.prototype.libIdent = function(options) {
|
|
|
|
return contextify(options, this.userRequest);
|
|
|
|
};
|
|
|
|
|
2016-09-14 18:04:42 +08:00
|
|
|
NormalModule.prototype.nameForCondition = function() {
|
|
|
|
var idx = this.resource.indexOf("?");
|
|
|
|
if(idx >= 0) return this.resource.substr(0, idx);
|
|
|
|
return this.resource;
|
|
|
|
};
|
|
|
|
|
2016-01-04 04:42:56 +08:00
|
|
|
NormalModule.prototype.doBuild = function doBuild(options, compilation, resolver, fs, callback) {
|
|
|
|
this.cacheable = false;
|
|
|
|
var module = this;
|
|
|
|
var loaderContext = {
|
|
|
|
version: 2,
|
|
|
|
emitWarning: function(warning) {
|
|
|
|
module.warnings.push(new ModuleWarning(module, warning));
|
|
|
|
},
|
|
|
|
emitError: function(error) {
|
|
|
|
module.errors.push(new ModuleError(module, error));
|
|
|
|
},
|
|
|
|
exec: function(code, filename) {
|
|
|
|
var Module = require("module");
|
|
|
|
var m = new Module(filename, module);
|
|
|
|
m.paths = Module._nodeModulePaths(module.context);
|
|
|
|
m.filename = filename;
|
|
|
|
m._compile(code, filename);
|
|
|
|
return m.exports;
|
|
|
|
},
|
|
|
|
resolve: function(context, request, callback) {
|
|
|
|
resolver.resolve({}, context, request, callback);
|
|
|
|
},
|
|
|
|
resolveSync: function(context, request) {
|
|
|
|
return resolver.resolveSync({}, context, request);
|
|
|
|
},
|
|
|
|
options: options
|
|
|
|
};
|
2013-01-31 01:49:25 +08:00
|
|
|
loaderContext.webpack = true;
|
2014-08-27 23:00:26 +08:00
|
|
|
loaderContext.sourceMap = !!this.useSourceMap;
|
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;
|
2016-01-04 04:42:56 +08:00
|
|
|
loaderContext.fs = fs;
|
2013-12-20 05:31:12 +08:00
|
|
|
compilation.applyPlugins("normal-module-loader", loaderContext, this);
|
2016-01-04 04:42:56 +08:00
|
|
|
if(options.loader)
|
|
|
|
for(var key in options.loader)
|
|
|
|
loaderContext[key] = options.loader[key];
|
|
|
|
|
|
|
|
runLoaders({
|
|
|
|
resource: this.resource,
|
|
|
|
loaders: this.loaders,
|
|
|
|
context: loaderContext,
|
|
|
|
readResource: fs.readFile.bind(fs)
|
|
|
|
}, function(err, result) {
|
2016-02-04 06:46:51 +08:00
|
|
|
if(result) {
|
|
|
|
module.cacheable = result.cacheable;
|
|
|
|
module.fileDependencies = result.fileDependencies;
|
|
|
|
module.contextDependencies = result.contextDependencies;
|
|
|
|
}
|
2016-01-04 04:42:56 +08:00
|
|
|
if(err) {
|
2016-07-03 19:13:01 +08:00
|
|
|
return callback(module.error = new ModuleBuildError(module, err));
|
2016-01-04 04:42:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var resourceBuffer = result.resourceBuffer;
|
|
|
|
var source = result.result[0];
|
|
|
|
var sourceMap = result.result[1];
|
|
|
|
|
|
|
|
if(!Buffer.isBuffer(source) && typeof source !== "string") {
|
2016-07-03 19:13:01 +08:00
|
|
|
return callback(module.error = new ModuleBuildError(module, new Error("Final loader didn't return a Buffer or String")));
|
2016-01-04 04:42:56 +08:00
|
|
|
}
|
|
|
|
source = asString(source);
|
|
|
|
if(module.identifier && module.lineToLine && resourceBuffer) {
|
|
|
|
module._source = new LineToLineMappedSource(source, module.identifier(),
|
|
|
|
asString(resourceBuffer));
|
|
|
|
} else if(module.identifier && module.useSourceMap && sourceMap) {
|
|
|
|
module._source = new SourceMapSource(source, module.identifier(), sourceMap);
|
|
|
|
} else if(module.identifier) {
|
|
|
|
module._source = new OriginalSource(source, module.identifier());
|
|
|
|
} else {
|
|
|
|
module._source = new RawSource(source);
|
|
|
|
}
|
|
|
|
return callback();
|
2016-02-04 06:46:51 +08:00
|
|
|
});
|
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) {
|
2016-07-03 19:13:01 +08:00
|
|
|
var _this = this;
|
|
|
|
_this.buildTimestamp = new Date().getTime();
|
|
|
|
_this.built = true;
|
|
|
|
_this._source = null;
|
|
|
|
_this.error = null;
|
2016-12-14 19:03:24 +08:00
|
|
|
_this.errors.length = 0;
|
|
|
|
_this.warnings.length = 0;
|
2016-12-14 19:03:24 +08:00
|
|
|
_this.meta = {};
|
2016-07-03 19:13:01 +08:00
|
|
|
return _this.doBuild(options, compilation, resolver, fs, function(err) {
|
|
|
|
_this.dependencies.length = 0;
|
|
|
|
_this.variables.length = 0;
|
|
|
|
_this.blocks.length = 0;
|
|
|
|
_this._cachedSource = null;
|
|
|
|
if(err) return setError(err);
|
2013-10-14 19:59:44 +08:00
|
|
|
if(options.module && options.module.noParse) {
|
2016-10-29 21:57:58 +08:00
|
|
|
var testRegExp = function testRegExp(regExp) {
|
2016-06-21 03:46:27 +08:00
|
|
|
return typeof regExp === "string" ?
|
2016-07-03 19:13:01 +08:00
|
|
|
_this.request.indexOf(regExp) === 0 :
|
|
|
|
regExp.test(_this.request);
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2013-10-14 19:59:44 +08:00
|
|
|
if(Array.isArray(options.module.noParse)) {
|
2016-07-03 19:13:01 +08:00
|
|
|
if(options.module.noParse.some(testRegExp, _this))
|
2016-06-21 03:46:27 +08:00
|
|
|
return callback();
|
2016-07-03 19:13:01 +08:00
|
|
|
} else if(testRegExp.call(_this, options.module.noParse)) {
|
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 {
|
2016-07-03 19:13:01 +08:00
|
|
|
_this.parser.parse(_this._source.source(), {
|
|
|
|
current: _this,
|
|
|
|
module: _this,
|
2013-06-20 18:04:31 +08:00
|
|
|
compilation: compilation,
|
|
|
|
options: options
|
2013-02-05 15:31:46 +08:00
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
} catch(e) {
|
2016-07-03 19:13:01 +08:00
|
|
|
var source = _this._source.source();
|
|
|
|
return setError(_this.error = new ModuleParseError(_this, source, e));
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
return callback();
|
2016-07-03 19:13:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
function setError(err) {
|
|
|
|
_this.meta = null;
|
|
|
|
if(_this.error) {
|
|
|
|
_this.errors.push(_this.error);
|
|
|
|
_this._source = new RawSource("throw new Error(" + JSON.stringify(_this.error.message) + ");");
|
|
|
|
} else {
|
|
|
|
_this._source = new RawSource("throw new Error('Module build failed');");
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NormalModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
|
2014-05-17 06:31:52 +08:00
|
|
|
var hash = require("crypto").createHash("md5");
|
|
|
|
this.updateHash(hash);
|
|
|
|
hash = hash.digest("hex");
|
|
|
|
if(this._cachedSource && this._cachedSource.hash === hash) {
|
|
|
|
return this._cachedSource.source;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
var _source = this._source;
|
2015-05-27 10:59:02 +08:00
|
|
|
if(!_source) return new RawSource("throw new Error('No source available');");
|
2014-03-08 00:07:58 +08:00
|
|
|
var source = new ReplaceSource(_source);
|
2015-07-13 06:20:09 +08:00
|
|
|
this._cachedSource = {
|
|
|
|
source: source,
|
|
|
|
hash: hash
|
|
|
|
};
|
2013-10-01 16:11:25 +08:00
|
|
|
var topLevelBlock = this;
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
function doDep(dep) {
|
2015-10-18 16:53:38 +08:00
|
|
|
var template = dependencyTemplates.get(dep.constructor);
|
|
|
|
if(!template) throw new Error("No template for dependency: " + dep.constructor.name);
|
2013-06-12 22:16:06 +08:00
|
|
|
template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2015-07-07 06:11:13 +08:00
|
|
|
function doVariable(availableVars, vars, variable) {
|
2013-01-31 01:49:25 +08:00
|
|
|
var name = variable.name;
|
|
|
|
var expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
|
2016-06-21 03:46:27 +08:00
|
|
|
|
|
|
|
function isEqual(v) {
|
2016-06-20 14:21:00 +08:00
|
|
|
return v.name === name && v.expression.source() === expr.source();
|
2016-06-21 03:46:27 +08:00
|
|
|
}
|
|
|
|
if(availableVars.some(isEqual)) return;
|
2015-07-13 06:20:09 +08:00
|
|
|
vars.push({
|
|
|
|
name: name,
|
|
|
|
expression: expr
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2015-07-07 06:11:13 +08:00
|
|
|
function doBlock(availableVars, block) {
|
2013-01-31 01:49:25 +08:00
|
|
|
block.dependencies.forEach(doDep);
|
2015-07-07 06:11:13 +08:00
|
|
|
var vars = [];
|
2013-01-31 01:49:25 +08:00
|
|
|
if(block.variables.length > 0) {
|
2015-07-07 06:11:13 +08:00
|
|
|
block.variables.forEach(doVariable.bind(null, availableVars, vars));
|
2013-01-31 01:49:25 +08:00
|
|
|
var varNames = [];
|
|
|
|
var varExpressions = [];
|
|
|
|
var varStartCode = "";
|
|
|
|
var varEndCode = "";
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2016-10-29 21:57:58 +08:00
|
|
|
var emitFunction = function emitFunction() {
|
2014-06-25 00:53:32 +08:00
|
|
|
if(varNames.length === 0) return;
|
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...
|
2017-01-16 10:21:13 +08:00
|
|
|
varEndCode = (topLevelBlock === block ? "}.call(" + (topLevelBlock.exportsArgument || "exports") + ", " : "}.call(this, ") +
|
2015-04-24 05:55:50 +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;
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2013-01-31 01:49:25 +08:00
|
|
|
vars.forEach(function(v) {
|
|
|
|
if(varNames.indexOf(v.name) >= 0) emitFunction();
|
|
|
|
varNames.push(v.name);
|
|
|
|
varExpressions.push(v.expression);
|
|
|
|
});
|
|
|
|
emitFunction();
|
2016-02-23 00:13:28 +08:00
|
|
|
var start = block.range ? block.range[0] : -10;
|
2016-03-10 17:50:29 +08:00
|
|
|
var end = block.range ? block.range[1] : (_source.size() + 1);
|
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
|
|
|
}
|
2015-07-07 06:11:13 +08:00
|
|
|
block.blocks.forEach(doBlock.bind(null, availableVars.concat(vars)));
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2015-07-07 06:11:13 +08:00
|
|
|
doBlock([], this);
|
2015-04-04 01:47:53 +08:00
|
|
|
return new CachedSource(source);
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
2014-11-06 07:21:32 +08:00
|
|
|
hash.update("meta");
|
|
|
|
hash.update(JSON.stringify(this.meta));
|
2013-01-31 01:49:25 +08:00
|
|
|
Module.prototype.updateHash.call(this, hash);
|
|
|
|
};
|