PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Compiler.js

https://gitlab.com/0072016/webpack
JavaScript | 441 lines | 384 code | 53 blank | 4 comment | 52 complexity | 740dc535c2f9c1f02b87bcd47dc2f81e 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 assign = require("object-assign");
  7. var Tapable = require("tapable");
  8. var Compilation = require("./Compilation");
  9. var Parser = require("./Parser");
  10. var Resolver = require("enhanced-resolve/lib/Resolver");
  11. var NormalModuleFactory = require("./NormalModuleFactory");
  12. var ContextModuleFactory = require("./ContextModuleFactory");
  13. function Watching(compiler, watchOptions, handler) {
  14. this.startTime = null;
  15. this.invalid = false;
  16. this.error = null;
  17. this.stats = null;
  18. this.handler = handler;
  19. if(typeof watchOptions === "number") {
  20. this.watchOptions = {
  21. aggregateTimeout: watchOptions
  22. };
  23. } else if(watchOptions && typeof watchOptions === "object") {
  24. this.watchOptions = assign({}, watchOptions);
  25. } else {
  26. this.watchOptions = {};
  27. }
  28. this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
  29. this.compiler = compiler;
  30. this.running = true;
  31. this.compiler.readRecords(function(err) {
  32. if(err) return this._done(err);
  33. this._go();
  34. }.bind(this));
  35. }
  36. Watching.prototype._go = function() {
  37. this.startTime = new Date().getTime();
  38. this.running = true;
  39. this.invalid = false;
  40. this.compiler.applyPluginsAsync("watch-run", this, function(err) {
  41. if(err) return this._done(err);
  42. this.compiler.compile(function onCompiled(err, compilation) {
  43. if(err) return this._done(err);
  44. if(this.invalid) return this._done();
  45. if(this.compiler.applyPluginsBailResult("should-emit", compilation) === false) {
  46. return this._done(null, compilation);
  47. }
  48. this.compiler.emitAssets(compilation, function(err) {
  49. if(err) return this._done(err);
  50. if(this.invalid) return this._done();
  51. this.compiler.emitRecords(function(err) {
  52. if(err) return this._done(err);
  53. if(compilation.applyPluginsBailResult("need-additional-pass")) {
  54. compilation.needAdditionalPass = true;
  55. var stats = compilation.getStats();
  56. stats.startTime = this.startTime;
  57. stats.endTime = new Date().getTime();
  58. this.compiler.applyPlugins("done", stats);
  59. this.compiler.applyPluginsAsync("additional-pass", function(err) {
  60. if(err) return this._done(err);
  61. this.compiler.compile(onCompiled.bind(this));
  62. }.bind(this));
  63. return;
  64. }
  65. return this._done(null, compilation);
  66. }.bind(this));
  67. }.bind(this));
  68. }.bind(this));
  69. }.bind(this));
  70. };
  71. Watching.prototype._done = function(err, compilation) {
  72. this.running = false;
  73. if(this.invalid) return this._go();
  74. this.error = err || null;
  75. this.stats = compilation ? compilation.getStats() : null;
  76. if(this.stats) {
  77. this.stats.startTime = this.startTime;
  78. this.stats.endTime = new Date().getTime();
  79. }
  80. if(this.stats)
  81. this.compiler.applyPlugins("done", this.stats);
  82. else
  83. this.compiler.applyPlugins("failed", this.error);
  84. this.handler(this.error, this.stats);
  85. if(!this.error)
  86. this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
  87. };
  88. Watching.prototype.watch = function(files, dirs, missing) {
  89. this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) {
  90. this.watcher = null;
  91. if(err) return this.handler(err);
  92. this.compiler.fileTimestamps = fileTimestamps;
  93. this.compiler.contextTimestamps = contextTimestamps;
  94. this.invalidate();
  95. }.bind(this), function(fileName, changeTime) {
  96. this.compiler.applyPlugins("invalid", fileName, changeTime);
  97. }.bind(this));
  98. };
  99. Watching.prototype.invalidate = function() {
  100. if(this.watcher) {
  101. this.watcher.pause();
  102. this.watcher = null;
  103. }
  104. if(this.running) {
  105. this.invalid = true;
  106. return false;
  107. } else {
  108. this._go();
  109. }
  110. };
  111. Watching.prototype.close = function(callback) {
  112. if(callback === undefined) callback = function() {};
  113. if(this.watcher) {
  114. this.watcher.close();
  115. this.watcher = null;
  116. }
  117. if(this.running) {
  118. this.invalid = true;
  119. this._done = function() {
  120. callback();
  121. };
  122. } else {
  123. callback();
  124. }
  125. };
  126. function Compiler() {
  127. Tapable.call(this);
  128. this.outputPath = "";
  129. this.outputFileSystem = null;
  130. this.inputFileSystem = null;
  131. this.recordsInputPath = null;
  132. this.recordsOutputPath = null;
  133. this.records = {};
  134. this.fileTimestamps = {};
  135. this.contextTimestamps = {};
  136. this.resolvers = {
  137. normal: new Resolver(null),
  138. loader: new Resolver(null),
  139. context: new Resolver(null)
  140. };
  141. this.parser = new Parser();
  142. this.options = {};
  143. }
  144. module.exports = Compiler;
  145. Compiler.prototype = Object.create(Tapable.prototype);
  146. Compiler.prototype.constructor = Compiler;
  147. Compiler.Watching = Watching;
  148. Compiler.prototype.watch = function(watchOptions, handler) {
  149. this.fileTimestamps = {};
  150. this.contextTimestamps = {};
  151. var watching = new Watching(this, watchOptions, handler);
  152. return watching;
  153. };
  154. Compiler.prototype.run = function(callback) {
  155. var startTime = new Date().getTime();
  156. this.applyPluginsAsync("before-run", this, function(err) {
  157. if(err) return callback(err);
  158. this.applyPluginsAsync("run", this, function(err) {
  159. if(err) return callback(err);
  160. this.readRecords(function(err) {
  161. if(err) return callback(err);
  162. this.compile(function onCompiled(err, compilation) {
  163. if(err) return callback(err);
  164. if(this.applyPluginsBailResult("should-emit", compilation) === false) {
  165. var stats = compilation.getStats();
  166. stats.startTime = startTime;
  167. stats.endTime = new Date().getTime();
  168. this.applyPlugins("done", stats);
  169. return callback(null, stats);
  170. }
  171. this.emitAssets(compilation, function(err) {
  172. if(err) return callback(err);
  173. if(compilation.applyPluginsBailResult("need-additional-pass")) {
  174. compilation.needAdditionalPass = true;
  175. var stats = compilation.getStats();
  176. stats.startTime = startTime;
  177. stats.endTime = new Date().getTime();
  178. this.applyPlugins("done", stats);
  179. this.applyPluginsAsync("additional-pass", function(err) {
  180. if(err) return callback(err);
  181. this.compile(onCompiled.bind(this));
  182. }.bind(this));
  183. return;
  184. }
  185. this.emitRecords(function(err) {
  186. if(err) return callback(err);
  187. var stats = compilation.getStats();
  188. stats.startTime = startTime;
  189. stats.endTime = new Date().getTime();
  190. this.applyPlugins("done", stats);
  191. return callback(null, stats);
  192. }.bind(this));
  193. }.bind(this));
  194. }.bind(this));
  195. }.bind(this));
  196. }.bind(this));
  197. }.bind(this));
  198. };
  199. Compiler.prototype.runAsChild = function(callback) {
  200. this.compile(function(err, compilation) {
  201. if(err) return callback(err);
  202. this.parentCompilation.children.push(compilation);
  203. Object.keys(compilation.assets).forEach(function(name) {
  204. this.parentCompilation.assets[name] = compilation.assets[name];
  205. }.bind(this));
  206. var entries = compilation.chunks.filter(function(chunk) {
  207. return chunk.entry;
  208. });
  209. return callback(null, entries, compilation);
  210. }.bind(this));
  211. };
  212. Compiler.prototype.purgeInputFileSystem = function() {
  213. if(this.inputFileSystem && this.inputFileSystem.purge)
  214. this.inputFileSystem.purge();
  215. };
  216. Compiler.prototype.emitAssets = function(compilation, callback) {
  217. var outputPath;
  218. this.applyPluginsAsync("emit", compilation, function(err) {
  219. if(err) return callback(err);
  220. outputPath = compilation.getPath(this.outputPath);
  221. this.outputFileSystem.mkdirp(outputPath, emitFiles.bind(this));
  222. }.bind(this));
  223. function emitFiles(err) {
  224. if(err) return callback(err);
  225. require("async").forEach(Object.keys(compilation.assets), function(file, callback) {
  226. var targetFile = file;
  227. var queryStringIdx = targetFile.indexOf("?");
  228. if(queryStringIdx >= 0) {
  229. targetFile = targetFile.substr(0, queryStringIdx);
  230. }
  231. if(targetFile.match(/\/|\\/)) {
  232. var dir = path.dirname(targetFile);
  233. this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut.bind(this));
  234. } else writeOut.call(this);
  235. function writeOut(err) {
  236. if(err) return callback(err);
  237. var targetPath = this.outputFileSystem.join(outputPath, targetFile);
  238. var source = compilation.assets[file];
  239. if(source.existsAt === targetPath) {
  240. source.emitted = false;
  241. return callback();
  242. }
  243. var content = source.source();
  244. if(!Buffer.isBuffer(content))
  245. content = new Buffer(content, "utf-8");
  246. source.existsAt = targetPath;
  247. source.emitted = true;
  248. this.outputFileSystem.writeFile(targetPath, content, callback);
  249. }
  250. }.bind(this), function(err) {
  251. if(err) return callback(err);
  252. afterEmit.call(this);
  253. }.bind(this));
  254. }
  255. function afterEmit() {
  256. this.applyPluginsAsync("after-emit", compilation, function(err) {
  257. if(err) return callback(err);
  258. return callback();
  259. });
  260. }
  261. };
  262. Compiler.prototype.emitRecords = function emitRecords(callback) {
  263. if(!this.recordsOutputPath) return callback();
  264. var idx1 = this.recordsOutputPath.lastIndexOf("/");
  265. var idx2 = this.recordsOutputPath.lastIndexOf("\\");
  266. var recordsOutputPathDirectory = null;
  267. if(idx1 > idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1);
  268. if(idx1 < idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2);
  269. if(!recordsOutputPathDirectory) return writeFile.call(this);
  270. this.outputFileSystem.mkdirp(recordsOutputPathDirectory, function(err) {
  271. if(err) return callback(err);
  272. writeFile.call(this);
  273. }.bind(this));
  274. function writeFile() {
  275. this.outputFileSystem.writeFile(this.recordsOutputPath, JSON.stringify(this.records, undefined, 2), callback);
  276. }
  277. };
  278. Compiler.prototype.readRecords = function readRecords(callback) {
  279. if(!this.recordsInputPath) {
  280. this.records = {};
  281. return callback();
  282. }
  283. this.inputFileSystem.stat(this.recordsInputPath, function(err) {
  284. // It doesn't exist
  285. // We can ignore this.
  286. if(err) return callback();
  287. this.inputFileSystem.readFile(this.recordsInputPath, function(err, content) {
  288. if(err) return callback(err);
  289. try {
  290. this.records = JSON.parse(content);
  291. } catch(e) {
  292. e.message = "Cannot parse records: " + e.message;
  293. return callback(e);
  294. }
  295. return callback();
  296. }.bind(this));
  297. }.bind(this));
  298. };
  299. Compiler.prototype.createChildCompiler = function(compilation, compilerName, outputOptions) {
  300. var childCompiler = new Compiler();
  301. for(var name in this._plugins) {
  302. if(["make", "compile", "emit", "after-emit", "invalid", "done", "this-compilation"].indexOf(name) < 0)
  303. childCompiler._plugins[name] = this._plugins[name].slice();
  304. }
  305. childCompiler.name = compilerName;
  306. childCompiler.outputPath = this.outputPath;
  307. childCompiler.inputFileSystem = this.inputFileSystem;
  308. childCompiler.outputFileSystem = null;
  309. childCompiler.resolvers = this.resolvers;
  310. childCompiler.parser = this.parser;
  311. childCompiler.fileTimestamps = this.fileTimestamps;
  312. childCompiler.contextTimestamps = this.contextTimestamps;
  313. if(!this.records[compilerName]) this.records[compilerName] = [];
  314. this.records[compilerName].push(childCompiler.records = {});
  315. childCompiler.options = Object.create(this.options);
  316. childCompiler.options.output = Object.create(childCompiler.options.output);
  317. for(name in outputOptions) {
  318. childCompiler.options.output[name] = outputOptions[name];
  319. }
  320. childCompiler.parentCompilation = compilation;
  321. return childCompiler;
  322. };
  323. Compiler.prototype.isChild = function() {
  324. return !!this.parentCompilation;
  325. };
  326. Compiler.prototype.createCompilation = function() {
  327. return new Compilation(this);
  328. };
  329. Compiler.prototype.newCompilation = function(params) {
  330. var compilation = this.createCompilation();
  331. compilation.fileTimestamps = this.fileTimestamps;
  332. compilation.contextTimestamps = this.contextTimestamps;
  333. compilation.name = this.name;
  334. compilation.records = this.records;
  335. this.applyPlugins("this-compilation", compilation, params);
  336. this.applyPlugins("compilation", compilation, params);
  337. return compilation;
  338. };
  339. Compiler.prototype.createNormalModuleFactory = function() {
  340. var normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolvers, this.parser, this.options.module || {});
  341. this.applyPlugins("normal-module-factory", normalModuleFactory);
  342. return normalModuleFactory;
  343. };
  344. Compiler.prototype.createContextModuleFactory = function() {
  345. var contextModuleFactory = new ContextModuleFactory(this.resolvers, this.inputFileSystem);
  346. this.applyPlugins("context-module-factory", contextModuleFactory);
  347. return contextModuleFactory;
  348. };
  349. Compiler.prototype.newCompilationParams = function() {
  350. var params = {
  351. normalModuleFactory: this.createNormalModuleFactory(),
  352. contextModuleFactory: this.createContextModuleFactory()
  353. };
  354. return params;
  355. };
  356. Compiler.prototype.compile = function(callback) {
  357. var params = this.newCompilationParams();
  358. this.applyPlugins("compile", params);
  359. var compilation = this.newCompilation(params);
  360. this.applyPluginsParallel("make", compilation, function(err) {
  361. if(err) return callback(err);
  362. compilation.seal(function(err) {
  363. if(err) return callback(err);
  364. this.applyPluginsAsync("after-compile", compilation, function(err) {
  365. if(err) return callback(err);
  366. return callback(null, compilation);
  367. });
  368. }.bind(this));
  369. }.bind(this));
  370. };