webpack/lib/ContextModule.js

112 lines
3.4 KiB
JavaScript
Raw Normal View History

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");
2013-03-26 23:54:41 +08:00
var OriginalSource = require("webpack-core/lib/OriginalSource");
2013-10-14 19:59:44 +08:00
var RawSource = require("webpack-core/lib/RawSource");
2013-01-31 01:49:25 +08:00
function ContextModule(resolveDependencies, context, recursive, regExp, addon) {
Module.call(this);
this.resolveDependencies = resolveDependencies;
this.context = context;
this.recursive = recursive;
this.regExp = regExp;
this.addon = addon;
this.cacheable = true;
this.contextDependencies = [context];
this.built = false;
}
module.exports = ContextModule;
ContextModule.prototype = Object.create(Module.prototype);
ContextModule.prototype.identifier = function() {
var identifier = "";
identifier += this.context + " ";
if(!this.recursive)
identifier += "nonrecursive ";
if(this.addon)
identifier += this.addon;
if(this.regExp)
identifier += this.regExp;
return identifier.replace(/ $/, "");
};
function prettyRegExp(str) {
return str.substring(1, str.length - 1);
}
ContextModule.prototype.readableIdentifier = function(requestShortener) {
var identifier = "";
identifier += requestShortener.shorten(this.context) + " ";
if(!this.recursive)
identifier += "nonrecursive ";
if(this.addon)
2013-02-04 19:34:20 +08:00
identifier += requestShortener.shorten(this.addon);
2013-01-31 01:49:25 +08:00
if(this.regExp)
identifier += prettyRegExp(this.regExp + "");
return identifier.replace(/ $/, "");
};
ContextModule.prototype.needRebuild = function(fileTimestamps, contextTimestamps) {
var ts = contextTimestamps[this.context];
if(!ts) return true;
2013-02-01 15:03:38 +08:00
return ts >= this.builtTime;
2013-01-31 01:49:25 +08:00
};
2013-02-01 01:00:22 +08:00
ContextModule.prototype.disconnect = function disconnect() {
this.built = false;
Module.prototype.disconnect.call(this);
};
2013-01-31 01:49:25 +08:00
ContextModule.prototype.build = function(options, compilation, resolver, fs, callback) {
this.built = true;
this.builtTime = new Date().getTime();
var addon = this.addon;
this.resolveDependencies(fs, this.context, this.recursive, this.regExp, function(err, dependencies) {
if(err) return callback(err);
dependencies.forEach(function(dep) {
dep.userRequest = dep.request;
dep.request = addon + dep.userRequest;
});
this.dependencies = dependencies;
callback();
}.bind(this));
};
ContextModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
var map = {};
2013-02-13 18:51:08 +08:00
this.dependencies.slice().sort(function(a,b) {
2014-06-25 00:53:32 +08:00
if(a.userRequest === b.userRequest) return 0;
2013-02-13 18:51:08 +08:00
return a.userRequest < b.userRequest ? -1 : 1;
}).forEach(function(dep) {
2013-01-31 01:49:25 +08:00
if(dep.module)
map[dep.userRequest] = dep.module.id
});
var str = [
"var map = ", JSON.stringify(map, null, "\t"), ";\n",
2013-02-13 18:51:08 +08:00
"function webpackContext(req) {\n",
"\treturn __webpack_require__(webpackContextResolve(req));\n",
2013-02-21 03:40:40 +08:00
"};\n",
"function webpackContextResolve(req) {\n",
"\treturn map[req] || (function() { throw new Error(\"Cannot find module '\" + req + \"'.\") }());\n",
2013-01-31 01:49:25 +08:00
"};\n",
2013-02-13 18:51:08 +08:00
"webpackContext.keys = function webpackContextKeys() {\n",
2013-01-31 01:49:25 +08:00
"\treturn Object.keys(map);\n",
"};\n",
2013-02-21 03:40:40 +08:00
"webpackContext.resolve = webpackContextResolve;\n",
2013-02-13 18:51:08 +08:00
"module.exports = webpackContext;\n",
2013-01-31 01:49:25 +08:00
];
2013-10-14 19:59:44 +08:00
if(this.useSourceMap) {
return new OriginalSource(str.join(""), this.identifier());
} else {
return new RawSource(str.join(""));
}
2013-01-31 01:49:25 +08:00
};
ContextModule.prototype.size = function() {
return this.dependencies.map(function(dep) {
return dep.userRequest.length + 5;
}).reduce(function(a, b) { return a+b; }, 160);
};