Compiler.js 15 KB

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