PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/webpack/lib/NormalModule.js

https://gitlab.com/limorelv/trelloApp
JavaScript | 322 lines | 313 code | 5 blank | 4 comment | 2 complexity | b3d544844bc61c172e095f225a02394f MD5 | raw file
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var path = require("path");
  6. var Module = require("./Module");
  7. var NormalModuleMixin = require("webpack-core/lib/NormalModuleMixin");
  8. var SourceMapSource = require("webpack-core/lib/SourceMapSource");
  9. var OriginalSource = require("webpack-core/lib/OriginalSource");
  10. var RawSource = require("webpack-core/lib/RawSource");
  11. var ReplaceSource = require("webpack-core/lib/ReplaceSource");
  12. var CachedSource = require("webpack-core/lib/CachedSource");
  13. var ModuleParseError = require("./ModuleParseError");
  14. var TemplateArgumentDependency = require("./dependencies/TemplateArgumentDependency");
  15. var AsyncDependenciesBlock = require("./AsyncDependenciesBlock");
  16. function NormalModule(request, userRequest, rawRequest, loaders, resource, parser) {
  17. Module.call(this);
  18. this.request = request;
  19. this.userRequest = userRequest;
  20. this.rawRequest = rawRequest;
  21. this.parser = parser;
  22. NormalModuleMixin.call(this, loaders, resource);
  23. this.meta = {};
  24. this.assets = {};
  25. this.built = false;
  26. this._cachedSource = null;
  27. }
  28. module.exports = NormalModule;
  29. NormalModule.prototype = Object.create(Module.prototype);
  30. NormalModuleMixin.mixin(NormalModule.prototype);
  31. NormalModule.prototype.identifier = function() {
  32. return this.request;
  33. };
  34. NormalModule.prototype.readableIdentifier = function(requestShortener) {
  35. return requestShortener.shorten(this.userRequest);
  36. };
  37. function contextify(options, request) {
  38. return request.split("!").map(function(r) {
  39. var rp = path.relative(options.context, r);
  40. if(path.sep === "\\")
  41. rp = rp.replace(/\\/g, "/");
  42. if(rp.indexOf("../") !== 0)
  43. rp = "./" + rp;
  44. return rp;
  45. }).join("!");
  46. }
  47. NormalModule.prototype.libIdent = function(options) {
  48. return contextify(options, this.userRequest);
  49. };
  50. NormalModule.prototype.fillLoaderContext = function fillLoaderContext(loaderContext, options, compilation) {
  51. loaderContext.webpack = true;
  52. loaderContext.sourceMap = !!this.useSourceMap;
  53. loaderContext.emitFile = function(name, content, sourceMap) {
  54. if(typeof sourceMap === "string") {
  55. this.assets[name] = new OriginalSource(content, sourceMap);
  56. } else if(sourceMap) {
  57. this.assets[name] = new SourceMapSource(content, name, sourceMap);
  58. } else {
  59. this.assets[name] = new RawSource(content);
  60. }
  61. }.bind(this);
  62. loaderContext._module = this;
  63. loaderContext._compilation = compilation;
  64. loaderContext._compiler = compilation.compiler;
  65. loaderContext.fs = compilation.compiler.inputFileSystem;
  66. compilation.applyPlugins("normal-module-loader", loaderContext, this);
  67. };
  68. NormalModule.prototype.disconnect = function disconnect() {
  69. this.built = false;
  70. Module.prototype.disconnect.call(this);
  71. };
  72. NormalModule.prototype.build = function build(options, compilation, resolver, fs, callback) {
  73. this.buildTimestamp = new Date().getTime();
  74. this.built = true;
  75. return this.doBuild(options, compilation, resolver, fs, function(err) {
  76. if(err) return callback(err);
  77. this.dependencies.length = 0;
  78. this.variables.length = 0;
  79. this.blocks.length = 0;
  80. this._cachedSource = null;
  81. if(options.module && options.module.noParse) {
  82. if(Array.isArray(options.module.noParse)) {
  83. if(options.module.noParse.some(function(regExp) {
  84. return typeof regExp === "string" ?
  85. this.request.indexOf(regExp) === 0 :
  86. regExp.test(this.request);
  87. }, this)) return callback();
  88. } else if(typeof options.module.noParse === "string" ?
  89. this.request.indexOf(options.module.noParse) === 0 :
  90. options.module.noParse.test(this.request)) {
  91. return callback();
  92. }
  93. }
  94. try {
  95. this.parser.parse(this._source.source(), {
  96. current: this,
  97. module: this,
  98. compilation: compilation,
  99. options: options
  100. });
  101. } catch(e) {
  102. var source = this._source.source();
  103. this._source = null;
  104. return callback(new ModuleParseError(this, source, e));
  105. }
  106. return callback();
  107. }.bind(this));
  108. };
  109. NormalModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
  110. var hash = require("crypto").createHash("md5");
  111. this.updateHash(hash);
  112. hash = hash.digest("hex");
  113. if(this._cachedSource && this._cachedSource.hash === hash) {
  114. return this._cachedSource.source;
  115. }
  116. var _source = this._source;
  117. if(!_source) return new RawSource("throw new Error('No source available');");
  118. var source = new ReplaceSource(_source);
  119. this._cachedSource = {
  120. source: source,
  121. hash: hash
  122. };
  123. var topLevelBlock = this;
  124. function doDep(dep) {
  125. var template = dependencyTemplates.get(dep.constructor);
  126. if(!template) throw new Error("No template for dependency: " + dep.constructor.name);
  127. template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
  128. }
  129. function doVariable(vars, variable) {
  130. var name = variable.name;
  131. var expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
  132. vars.push({
  133. name: name,
  134. expression: expr
  135. });
  136. }
  137. function doBlock(block) {
  138. block.dependencies.forEach(doDep);
  139. block.blocks.forEach(doBlock);
  140. if(block.variables.length > 0) {
  141. var vars = [];
  142. block.variables.forEach(doVariable.bind(null, vars));
  143. var varNames = [];
  144. var varExpressions = [];
  145. var varStartCode = "";
  146. var varEndCode = "";
  147. function emitFunction() {
  148. if(varNames.length === 0) return;
  149. varStartCode += "/* WEBPACK VAR INJECTION */(function(" + varNames.join(", ") + ") {";
  150. // exports === this in the topLevelBlock, but exports do compress better...
  151. varEndCode = (topLevelBlock === block ? "}.call(exports, " : "}.call(this, ") +
  152. varExpressions.map(function(e) {
  153. return e.source();
  154. }).join(", ") + "))" + varEndCode;
  155. varNames.length = 0;
  156. varExpressions.length = 0;
  157. }
  158. vars.forEach(function(v) {
  159. if(varNames.indexOf(v.name) >= 0) emitFunction();
  160. varNames.push(v.name);
  161. varExpressions.push(v.expression);
  162. });
  163. emitFunction();
  164. var start = block.range ? block.range[0] : 0;
  165. var end = block.range ? block.range[1] : _source.size();
  166. if(varStartCode) source.insert(start + 0.5, varStartCode);
  167. if(varEndCode) source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
  168. }
  169. }
  170. doBlock(this);
  171. return new CachedSource(source);
  172. };
  173. NormalModule.prototype.needRebuild = function needRebuild(fileTimestamps, contextTimestamps) {
  174. var timestamp = 0;
  175. this.fileDependencies.forEach(function(file) {
  176. var ts = fileTimestamps[file];
  177. if(!ts) timestamp = Infinity;
  178. if(ts > timestamp) timestamp = ts;
  179. });
  180. this.contextDependencies.forEach(function(context) {
  181. var ts = contextTimestamps[context];
  182. if(!ts) timestamp = Infinity;
  183. if(ts > timestamp) timestamp = ts;
  184. });
  185. return timestamp >= this.buildTimestamp;
  186. };
  187. NormalModule.prototype.size = function() {
  188. return this._source ? this._source.size() : -1;
  189. };
  190. NormalModule.prototype.updateHash = function(hash) {
  191. if(this._source) {
  192. hash.update("source");
  193. this._source.updateHash(hash);
  194. } else
  195. hash.update("null");
  196. hash.update("meta");
  197. hash.update(JSON.stringify(this.meta));
  198. Module.prototype.updateHash.call(this, hash);
  199. };
  200. NormalModule.prototype.getSourceHash = function() {
  201. if(!this._source) return "";
  202. var hash = require("crypto").createHash("md5");
  203. hash.update(this._source.source());
  204. return hash.digest("hex");
  205. };
  206. NormalModule.prototype.getAllModuleDependencies = function() {
  207. var list = [];
  208. function doDep(dep) {
  209. if(dep.module && list.indexOf(dep.module) < 0)
  210. list.push(dep.module);
  211. }
  212. function doVariable(variable) {
  213. variable.dependencies.forEach(doDep);
  214. }
  215. function doBlock(block) {
  216. block.variables.forEach(doVariable);
  217. block.dependencies.forEach(doDep);
  218. block.blocks.forEach(doBlock);
  219. }
  220. doBlock(this);
  221. return list;
  222. };
  223. NormalModule.prototype.createTemplate = function(keepModules, roots) {
  224. roots.sort(function(a, b) {
  225. var ia = a.identifier();
  226. var ib = b.identifier();
  227. if(ia < ib) return -1;
  228. if(ib < ia) return 1;
  229. return 0;
  230. });
  231. var template = new NormalModule("", "", "", [], "", null);
  232. template._source = this._source;
  233. template.built = this.built;
  234. template.templateModules = keepModules;
  235. template._templateOrigin = this;
  236. template.readableIdentifier = function() {
  237. return "template of " + this._templateOrigin.id + " referencing " + keepModules.map(function(m) {
  238. return m.id;
  239. }).join(", ");
  240. };
  241. template.identifier = function() {
  242. var array = roots.map(function(m) {
  243. return m.identifier();
  244. });
  245. array.sort();
  246. return array.join("|");
  247. };
  248. var args = template.arguments = [];
  249. function doDeps(deps) {
  250. return deps.map(function(dep) {
  251. if(dep.module && keepModules.indexOf(dep.module) < 0) {
  252. var argName = "__webpack_module_template_argument_" + args.length + "__";
  253. args.push(argName);
  254. return new TemplateArgumentDependency(argName, dep);
  255. } else {
  256. return dep;
  257. }
  258. });
  259. }
  260. function doBlock(block, newBlock) {
  261. block.variables.forEach(function(variable) {
  262. var newDependencies = doDeps(variable.dependencies);
  263. newBlock.addVariable(variable.name, variable.expression, newDependencies);
  264. });
  265. newBlock.dependencies = doDeps(block.dependencies);
  266. block.blocks.forEach(function(childBlock) {
  267. var newChildBlock = new AsyncDependenciesBlock(childBlock.name, childBlock.module, childBlock.loc);
  268. newBlock.addBlock(newChildBlock);
  269. doBlock(childBlock, newChildBlock);
  270. });
  271. }
  272. doBlock(this, template);
  273. return template;
  274. };
  275. NormalModule.prototype.getTemplateArguments = function(keepModules) {
  276. var list = [];
  277. function doDep(dep) {
  278. if(dep.module && keepModules.indexOf(dep.module) < 0)
  279. list.push(dep.module);
  280. }
  281. function doVariable(variable) {
  282. variable.dependencies.forEach(doDep);
  283. }
  284. function doBlock(block) {
  285. block.variables.forEach(doVariable);
  286. block.dependencies.forEach(doDep);
  287. block.blocks.forEach(doBlock);
  288. }
  289. doBlock(this);
  290. return list;
  291. };