| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const Tapable = require("tapable"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Compilation = require("./Compilation"); | 
					
						
							|  |  |  | const Stats = require("./Stats"); | 
					
						
							|  |  |  | const NormalModuleFactory = require("./NormalModuleFactory"); | 
					
						
							|  |  |  | const ContextModuleFactory = require("./ContextModuleFactory"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const makePathsRelative = require("./util/identifier").makePathsRelative; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Watching { | 
					
						
							|  |  |  | 	constructor(compiler, watchOptions, handler) { | 
					
						
							|  |  |  | 		this.startTime = null; | 
					
						
							|  |  |  | 		this.invalid = false; | 
					
						
							|  |  |  | 		this.handler = handler; | 
					
						
							| 
									
										
										
										
											2017-07-12 14:03:22 +08:00
										 |  |  | 		this.callbacks = []; | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.closed = false; | 
					
						
							|  |  |  | 		if(typeof watchOptions === "number") { | 
					
						
							|  |  |  | 			this.watchOptions = { | 
					
						
							|  |  |  | 				aggregateTimeout: watchOptions | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} else if(watchOptions && typeof watchOptions === "object") { | 
					
						
							|  |  |  | 			this.watchOptions = Object.assign({}, watchOptions); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.watchOptions = {}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200; | 
					
						
							|  |  |  | 		this.compiler = compiler; | 
					
						
							|  |  |  | 		this.running = true; | 
					
						
							|  |  |  | 		this.compiler.readRecords(err => { | 
					
						
							|  |  |  | 			if(err) return this._done(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			this._go(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-05-10 19:50:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	_go() { | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		this.startTime = Date.now(); | 
					
						
							|  |  |  | 		this.running = true; | 
					
						
							|  |  |  | 		this.invalid = false; | 
					
						
							|  |  |  | 		this.compiler.applyPluginsAsync("watch-run", this, err => { | 
					
						
							|  |  |  | 			if(err) return this._done(err); | 
					
						
							|  |  |  | 			const onCompiled = (err, compilation) => { | 
					
						
							|  |  |  | 				if(err) return this._done(err); | 
					
						
							|  |  |  | 				if(this.invalid) return this._done(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(this.compiler.applyPluginsBailResult("should-emit", compilation) === false) { | 
					
						
							|  |  |  | 					return this._done(null, compilation); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-09-03 20:16:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 				this.compiler.emitAssets(compilation, err => { | 
					
						
							|  |  |  | 					if(err) return this._done(err); | 
					
						
							|  |  |  | 					if(this.invalid) return this._done(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					this.compiler.emitRecords(err => { | 
					
						
							|  |  |  | 						if(err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 						if(compilation.applyPluginsBailResult("need-additional-pass")) { | 
					
						
							|  |  |  | 							compilation.needAdditionalPass = true; | 
					
						
							| 
									
										
										
										
											2015-01-30 07:46:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 							const stats = new Stats(compilation); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 							stats.startTime = this.startTime; | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 							stats.endTime = Date.now(); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 							this.compiler.applyPlugins("done", stats); | 
					
						
							| 
									
										
										
										
											2015-01-30 07:46:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 							this.compiler.applyPluginsAsync("additional-pass", err => { | 
					
						
							|  |  |  | 								if(err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2017-07-12 15:22:02 +08:00
										 |  |  | 								this.compiler.compile(onCompiled); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 							}); | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 						return this._done(null, compilation); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 			this.compiler.compile(onCompiled); | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 19:33:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	_getStats(compilation) { | 
					
						
							|  |  |  | 		const stats = new Stats(compilation); | 
					
						
							|  |  |  | 		stats.startTime = this.startTime; | 
					
						
							|  |  |  | 		stats.endTime = Date.now(); | 
					
						
							|  |  |  | 		return stats; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	_done(err, compilation) { | 
					
						
							|  |  |  | 		this.running = false; | 
					
						
							|  |  |  | 		if(this.invalid) return this._go(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const stats = compilation ? this._getStats(compilation) : null; | 
					
						
							|  |  |  | 		if(err) { | 
					
						
							|  |  |  | 			this.compiler.applyPlugins("failed", err); | 
					
						
							|  |  |  | 			this.handler(err, stats); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.compiler.applyPlugins("done", stats); | 
					
						
							|  |  |  | 		this.handler(null, stats); | 
					
						
							|  |  |  | 		if(!this.closed) { | 
					
						
							|  |  |  | 			this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-07-12 14:03:22 +08:00
										 |  |  | 		this.callbacks.forEach(cb => cb()); | 
					
						
							|  |  |  | 		this.callbacks.length = 0; | 
					
						
							| 
									
										
										
										
											2014-05-08 15:08:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	watch(files, dirs, missing) { | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 		this.pausedWatcher = null; | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, (err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) => { | 
					
						
							|  |  |  | 			this.pausedWatcher = this.watcher; | 
					
						
							|  |  |  | 			this.watcher = null; | 
					
						
							|  |  |  | 			if(err) return this.handler(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			this.compiler.fileTimestamps = fileTimestamps; | 
					
						
							|  |  |  | 			this.compiler.contextTimestamps = contextTimestamps; | 
					
						
							|  |  |  | 			this.invalidate(); | 
					
						
							|  |  |  | 		}, (fileName, changeTime) => { | 
					
						
							|  |  |  | 			this.compiler.applyPlugins("invalid", fileName, changeTime); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-12 13:57:53 +08:00
										 |  |  | 	invalidate(callback) { | 
					
						
							| 
									
										
										
										
											2017-07-12 14:03:22 +08:00
										 |  |  | 		if(callback) { | 
					
						
							|  |  |  | 			this.callbacks.push(callback); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		if(this.watcher) { | 
					
						
							|  |  |  | 			this.pausedWatcher = this.watcher; | 
					
						
							|  |  |  | 			this.watcher.pause(); | 
					
						
							|  |  |  | 			this.watcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(this.running) { | 
					
						
							|  |  |  | 			this.invalid = true; | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this._go(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	close(callback) { | 
					
						
							|  |  |  | 		if(callback === undefined) callback = function() {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.closed = true; | 
					
						
							|  |  |  | 		if(this.watcher) { | 
					
						
							|  |  |  | 			this.watcher.close(); | 
					
						
							|  |  |  | 			this.watcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(this.pausedWatcher) { | 
					
						
							|  |  |  | 			this.pausedWatcher.close(); | 
					
						
							|  |  |  | 			this.pausedWatcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(this.running) { | 
					
						
							|  |  |  | 			this.invalid = true; | 
					
						
							|  |  |  | 			this._done = () => { | 
					
						
							|  |  |  | 				this.compiler.applyPlugins("watch-close"); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-03-06 11:32:34 +08:00
										 |  |  | 			this.compiler.applyPlugins("watch-close"); | 
					
						
							| 
									
										
										
										
											2014-05-08 15:08:01 +08:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-08 15:08:01 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:59:43 +08:00
										 |  |  | class Compiler extends Tapable { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super(); | 
					
						
							|  |  |  | 		this.outputPath = ""; | 
					
						
							|  |  |  | 		this.outputFileSystem = null; | 
					
						
							|  |  |  | 		this.inputFileSystem = null; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.recordsInputPath = null; | 
					
						
							|  |  |  | 		this.recordsOutputPath = null; | 
					
						
							|  |  |  | 		this.records = {}; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.fileTimestamps = {}; | 
					
						
							|  |  |  | 		this.contextTimestamps = {}; | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.resolvers = { | 
					
						
							|  |  |  | 			normal: null, | 
					
						
							|  |  |  | 			loader: null, | 
					
						
							|  |  |  | 			context: null | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		let deprecationReported = false; | 
					
						
							|  |  |  | 		this.parser = { | 
					
						
							|  |  |  | 			plugin: (hook, fn) => { | 
					
						
							|  |  |  | 				if(!deprecationReported) { | 
					
						
							|  |  |  | 					console.warn("webpack: Using compiler.parser is deprecated.\n" + | 
					
						
							|  |  |  | 						"Use compiler.plugin(\"compilation\", function(compilation, data) {\n  data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. " + | 
					
						
							|  |  |  | 						"It was called " + new Error().stack.split("\n")[2].trim() + "."); | 
					
						
							|  |  |  | 					deprecationReported = true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				this.plugin("compilation", (compilation, data) => { | 
					
						
							|  |  |  | 					data.normalModuleFactory.plugin("parser", parser => { | 
					
						
							|  |  |  | 						parser.plugin(hook, fn); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 			apply: () => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				const args = arguments; | 
					
						
							|  |  |  | 				if(!deprecationReported) { | 
					
						
							|  |  |  | 					console.warn("webpack: Using compiler.parser is deprecated.\n" + | 
					
						
							|  |  |  | 						"Use compiler.plugin(\"compilation\", function(compilation, data) {\n  data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. " + | 
					
						
							|  |  |  | 						"It was called " + new Error().stack.split("\n")[2].trim() + "."); | 
					
						
							|  |  |  | 					deprecationReported = true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				this.plugin("compilation", (compilation, data) => { | 
					
						
							|  |  |  | 					data.normalModuleFactory.plugin("parser", parser => { | 
					
						
							|  |  |  | 						parser.apply.apply(parser, args); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		this.options = {}; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	watch(watchOptions, handler) { | 
					
						
							|  |  |  | 		this.fileTimestamps = {}; | 
					
						
							|  |  |  | 		this.contextTimestamps = {}; | 
					
						
							|  |  |  | 		const watching = new Watching(this, watchOptions, handler); | 
					
						
							|  |  |  | 		return watching; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	run(callback) { | 
					
						
							|  |  |  | 		const startTime = Date.now(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		const onCompiled = (err, compilation) => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			if(this.applyPluginsBailResult("should-emit", compilation) === false) { | 
					
						
							|  |  |  | 				const stats = new Stats(compilation); | 
					
						
							|  |  |  | 				stats.startTime = startTime; | 
					
						
							|  |  |  | 				stats.endTime = Date.now(); | 
					
						
							|  |  |  | 				this.applyPlugins("done", stats); | 
					
						
							|  |  |  | 				return callback(null, stats); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			this.emitAssets(compilation, err => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 				if(compilation.applyPluginsBailResult("need-additional-pass")) { | 
					
						
							|  |  |  | 					compilation.needAdditionalPass = true; | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					const stats = new Stats(compilation); | 
					
						
							|  |  |  | 					stats.startTime = startTime; | 
					
						
							|  |  |  | 					stats.endTime = Date.now(); | 
					
						
							|  |  |  | 					this.applyPlugins("done", stats); | 
					
						
							| 
									
										
										
										
											2015-01-30 07:46:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					this.applyPluginsAsync("additional-pass", err => { | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 						if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 						this.compile(onCompiled); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 				this.emitRecords(err => { | 
					
						
							|  |  |  | 					if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2016-05-07 04:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					const stats = new Stats(compilation); | 
					
						
							|  |  |  | 					stats.startTime = startTime; | 
					
						
							|  |  |  | 					stats.endTime = Date.now(); | 
					
						
							|  |  |  | 					this.applyPlugins("done", stats); | 
					
						
							|  |  |  | 					return callback(null, stats); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-05-07 04:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		this.applyPluginsAsync("before-run", this, err => { | 
					
						
							|  |  |  | 			if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2016-05-07 04:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			this.applyPluginsAsync("run", this, err => { | 
					
						
							|  |  |  | 				if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2016-05-07 04:45:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 				this.readRecords(err => { | 
					
						
							|  |  |  | 					if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					this.compile(onCompiled); | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	runAsChild(callback) { | 
					
						
							|  |  |  | 		this.compile((err, compilation) => { | 
					
						
							|  |  |  | 			if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			this.parentCompilation.children.push(compilation); | 
					
						
							|  |  |  | 			Object.keys(compilation.assets).forEach(name => { | 
					
						
							|  |  |  | 				this.parentCompilation.assets[name] = compilation.assets[name]; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-01-05 21:30:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			const entries = Object.keys(compilation.entrypoints).map(name => { | 
					
						
							|  |  |  | 				return compilation.entrypoints[name].chunks; | 
					
						
							|  |  |  | 			}).reduce((array, chunks) => { | 
					
						
							|  |  |  | 				return array.concat(chunks); | 
					
						
							|  |  |  | 			}, []); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			return callback(null, entries, compilation); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	purgeInputFileSystem() { | 
					
						
							|  |  |  | 		if(this.inputFileSystem && this.inputFileSystem.purge) | 
					
						
							|  |  |  | 			this.inputFileSystem.purge(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-12 04:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	emitAssets(compilation, callback) { | 
					
						
							|  |  |  | 		let outputPath; | 
					
						
							| 
									
										
										
										
											2014-03-12 01:42:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 		const emitFiles = (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			require("async").forEach(Object.keys(compilation.assets), (file, callback) => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				let targetFile = file; | 
					
						
							|  |  |  | 				const queryStringIdx = targetFile.indexOf("?"); | 
					
						
							|  |  |  | 				if(queryStringIdx >= 0) { | 
					
						
							|  |  |  | 					targetFile = targetFile.substr(0, queryStringIdx); | 
					
						
							| 
									
										
										
										
											2013-05-08 19:28:54 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-17 13:57:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 				const writeOut = (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 					if(err) return callback(err); | 
					
						
							|  |  |  | 					const targetPath = this.outputFileSystem.join(outputPath, targetFile); | 
					
						
							|  |  |  | 					const source = compilation.assets[file]; | 
					
						
							|  |  |  | 					if(source.existsAt === targetPath) { | 
					
						
							|  |  |  | 						source.emitted = false; | 
					
						
							|  |  |  | 						return callback(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					let content = source.source(); | 
					
						
							| 
									
										
										
										
											2017-01-17 13:57:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 					if(!Buffer.isBuffer(content)) { | 
					
						
							|  |  |  | 						content = new Buffer(content, "utf8"); // eslint-disable-line
 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					source.existsAt = targetPath; | 
					
						
							|  |  |  | 					source.emitted = true; | 
					
						
							|  |  |  | 					this.outputFileSystem.writeFile(targetPath, content, callback); | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(targetFile.match(/\/|\\/)) { | 
					
						
							|  |  |  | 					const dir = path.dirname(targetFile); | 
					
						
							|  |  |  | 					this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut); | 
					
						
							|  |  |  | 				} else writeOut(); | 
					
						
							| 
									
										
										
										
											2017-01-17 13:57:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			}, err => { | 
					
						
							|  |  |  | 				if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				afterEmit.call(this); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-08-11 13:52:25 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.applyPluginsAsync("emit", compilation, err => { | 
					
						
							|  |  |  | 			if(err) return callback(err); | 
					
						
							|  |  |  | 			outputPath = compilation.getPath(this.outputPath); | 
					
						
							|  |  |  | 			this.outputFileSystem.mkdirp(outputPath, emitFiles); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		function afterEmit() { | 
					
						
							|  |  |  | 			this.applyPluginsAsyncSeries1("after-emit", compilation, err => { | 
					
						
							|  |  |  | 				if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				return callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	emitRecords(callback) { | 
					
						
							|  |  |  | 		if(!this.recordsOutputPath) return callback(); | 
					
						
							|  |  |  | 		const idx1 = this.recordsOutputPath.lastIndexOf("/"); | 
					
						
							|  |  |  | 		const idx2 = this.recordsOutputPath.lastIndexOf("\\"); | 
					
						
							|  |  |  | 		let recordsOutputPathDirectory = null; | 
					
						
							|  |  |  | 		if(idx1 > idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1); | 
					
						
							|  |  |  | 		if(idx1 < idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2); | 
					
						
							|  |  |  | 		if(!recordsOutputPathDirectory) return writeFile.call(this); | 
					
						
							|  |  |  | 		this.outputFileSystem.mkdirp(recordsOutputPathDirectory, err => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			writeFile.call(this); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		function writeFile() { | 
					
						
							|  |  |  | 			this.outputFileSystem.writeFile(this.recordsOutputPath, JSON.stringify(this.records, undefined, 2), callback); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-06-10 20:25:54 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	readRecords(callback) { | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		if(!this.recordsInputPath) { | 
					
						
							|  |  |  | 			this.records = {}; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			return callback(); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		this.inputFileSystem.stat(this.recordsInputPath, err => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			// It doesn't exist
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			// We can ignore this.
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			if(err) return callback(); | 
					
						
							| 
									
										
										
										
											2013-06-10 20:25:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				try { | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					this.records = JSON.parse(content.toString("utf-8")); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				} catch(e) { | 
					
						
							|  |  |  | 					e.message = "Cannot parse records: " + e.message; | 
					
						
							|  |  |  | 					return callback(e); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				return callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) { | 
					
						
							|  |  |  | 		const childCompiler = new Compiler(); | 
					
						
							|  |  |  | 		if(Array.isArray(plugins)) { | 
					
						
							|  |  |  | 			plugins.forEach(plugin => childCompiler.apply(plugin)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for(const name in this._plugins) { | 
					
						
							|  |  |  | 			if(["make", "compile", "emit", "after-emit", "invalid", "done", "this-compilation"].indexOf(name) < 0) | 
					
						
							|  |  |  | 				childCompiler._plugins[name] = this._plugins[name].slice(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		childCompiler.name = compilerName; | 
					
						
							|  |  |  | 		childCompiler.outputPath = this.outputPath; | 
					
						
							|  |  |  | 		childCompiler.inputFileSystem = this.inputFileSystem; | 
					
						
							|  |  |  | 		childCompiler.outputFileSystem = null; | 
					
						
							|  |  |  | 		childCompiler.resolvers = this.resolvers; | 
					
						
							|  |  |  | 		childCompiler.fileTimestamps = this.fileTimestamps; | 
					
						
							|  |  |  | 		childCompiler.contextTimestamps = this.contextTimestamps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const relativeCompilerName = makePathsRelative(this.context, compilerName); | 
					
						
							|  |  |  | 		if(!this.records[relativeCompilerName]) this.records[relativeCompilerName] = []; | 
					
						
							|  |  |  | 		if(this.records[relativeCompilerName][compilerIndex]) | 
					
						
							|  |  |  | 			childCompiler.records = this.records[relativeCompilerName][compilerIndex]; | 
					
						
							| 
									
										
										
										
											2017-04-13 19:43:51 +08:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 			this.records[relativeCompilerName].push(childCompiler.records = {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		childCompiler.options = Object.create(this.options); | 
					
						
							|  |  |  | 		childCompiler.options.output = Object.create(childCompiler.options.output); | 
					
						
							|  |  |  | 		for(const name in outputOptions) { | 
					
						
							|  |  |  | 			childCompiler.options.output[name] = outputOptions[name]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		childCompiler.parentCompilation = compilation; | 
					
						
							| 
									
										
										
										
											2017-09-13 22:29:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		compilation.applyPlugins("child-compiler", childCompiler, compilerName, compilerIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 		return childCompiler; | 
					
						
							| 
									
										
										
										
											2013-06-10 20:25:54 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	isChild() { | 
					
						
							|  |  |  | 		return !!this.parentCompilation; | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	createCompilation() { | 
					
						
							|  |  |  | 		return new Compilation(this); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	newCompilation(params) { | 
					
						
							|  |  |  | 		const compilation = this.createCompilation(); | 
					
						
							|  |  |  | 		compilation.fileTimestamps = this.fileTimestamps; | 
					
						
							|  |  |  | 		compilation.contextTimestamps = this.contextTimestamps; | 
					
						
							|  |  |  | 		compilation.name = this.name; | 
					
						
							|  |  |  | 		compilation.records = this.records; | 
					
						
							|  |  |  | 		compilation.compilationDependencies = params.compilationDependencies; | 
					
						
							|  |  |  | 		this.applyPlugins("this-compilation", compilation, params); | 
					
						
							|  |  |  | 		this.applyPlugins("compilation", compilation, params); | 
					
						
							|  |  |  | 		return compilation; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-31 18:22:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	createNormalModuleFactory() { | 
					
						
							|  |  |  | 		const normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolvers, this.options.module || {}); | 
					
						
							|  |  |  | 		this.applyPlugins("normal-module-factory", normalModuleFactory); | 
					
						
							|  |  |  | 		return normalModuleFactory; | 
					
						
							| 
									
										
										
										
											2017-01-18 05:26:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-04 21:44:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	createContextModuleFactory() { | 
					
						
							|  |  |  | 		const contextModuleFactory = new ContextModuleFactory(this.resolvers, this.inputFileSystem); | 
					
						
							|  |  |  | 		this.applyPlugins("context-module-factory", contextModuleFactory); | 
					
						
							|  |  |  | 		return contextModuleFactory; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	newCompilationParams() { | 
					
						
							|  |  |  | 		const params = { | 
					
						
							|  |  |  | 			normalModuleFactory: this.createNormalModuleFactory(), | 
					
						
							|  |  |  | 			contextModuleFactory: this.createContextModuleFactory(), | 
					
						
							|  |  |  | 			compilationDependencies: [] | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		return params; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	compile(callback) { | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 		const params = this.newCompilationParams(); | 
					
						
							|  |  |  | 		this.applyPluginsAsync("before-compile", params, err => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			this.applyPlugins("compile", params); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			const compilation = this.newCompilation(params); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 			this.applyPluginsParallel("make", compilation, err => { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 				if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 				compilation.finish(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.seal(err => { | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 					if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:47:36 +08:00
										 |  |  | 					this.applyPluginsAsync("after-compile", compilation, err => { | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 						if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return callback(null, compilation); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-09-14 18:04:42 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-10-18 02:26:22 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-06-21 10:44:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-01 23:59:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Compiler.Watching = Watching; | 
					
						
							|  |  |  | module.exports = Compiler; |