| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const Module = require("./Module"); | 
					
						
							|  |  |  | const OriginalSource = require("webpack-sources").OriginalSource; | 
					
						
							|  |  |  | const RawSource = require("webpack-sources").RawSource; | 
					
						
							|  |  |  | const AsyncDependenciesBlock = require("./AsyncDependenciesBlock"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | class ContextModule extends Module { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 	constructor(resolveDependencies, context, recursive, regExp, addon, isAsync) { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		super(); | 
					
						
							|  |  |  | 		this.resolveDependencies = resolveDependencies; | 
					
						
							|  |  |  | 		this.context = context; | 
					
						
							|  |  |  | 		this.recursive = recursive; | 
					
						
							|  |  |  | 		this.regExp = regExp; | 
					
						
							|  |  |  | 		this.addon = addon; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 		this.async = !!isAsync; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		this.cacheable = true; | 
					
						
							|  |  |  | 		this.contextDependencies = [context]; | 
					
						
							|  |  |  | 		this.built = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:26:47 +08:00
										 |  |  | 	prettyRegExp(regexString) { | 
					
						
							|  |  |  | 		// remove the "/" at the front and the beginning
 | 
					
						
							|  |  |  | 		// "/foo/" -> "foo"
 | 
					
						
							|  |  |  | 		return regexString.substring(1, regexString.length - 1); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:15:29 +08:00
										 |  |  | 	contextify(context, request) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 		return request.split("!").map(subrequest => { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:15:29 +08:00
										 |  |  | 			let rp = path.relative(context, subrequest); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 			if(path.sep === "\\") | 
					
						
							|  |  |  | 				rp = rp.replace(/\\/g, "/"); | 
					
						
							|  |  |  | 			if(rp.indexOf("../") !== 0) | 
					
						
							|  |  |  | 				rp = "./" + rp; | 
					
						
							|  |  |  | 			return rp; | 
					
						
							|  |  |  | 		}).join("!"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 	identifier() { | 
					
						
							|  |  |  | 		let identifier = this.context; | 
					
						
							|  |  |  | 		if(this.async) | 
					
						
							|  |  |  | 			identifier += " async"; | 
					
						
							|  |  |  | 		if(!this.recursive) | 
					
						
							|  |  |  | 			identifier += " nonrecursive"; | 
					
						
							|  |  |  | 		if(this.addon) | 
					
						
							|  |  |  | 			identifier += ` ${this.addon}`; | 
					
						
							|  |  |  | 		if(this.regExp) | 
					
						
							|  |  |  | 			identifier += ` ${this.regExp}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return identifier; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	readableIdentifier(requestShortener) { | 
					
						
							|  |  |  | 		let identifier = requestShortener.shorten(this.context); | 
					
						
							|  |  |  | 		if(this.async) | 
					
						
							|  |  |  | 			identifier += " async"; | 
					
						
							|  |  |  | 		if(!this.recursive) | 
					
						
							|  |  |  | 			identifier += " nonrecursive"; | 
					
						
							|  |  |  | 		if(this.addon) | 
					
						
							|  |  |  | 			identifier += ` ${requestShortener.shorten(this.addon)}`; | 
					
						
							|  |  |  | 		if(this.regExp) | 
					
						
							|  |  |  | 			identifier += ` ${this.prettyRegExp(this.regExp + "")}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return identifier; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	libIdent(options) { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 		let identifier = this.contextify(options.context, this.context); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		if(this.async) | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 			identifier += " async"; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		if(this.recursive) | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 			identifier += " recursive"; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		if(this.addon) | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 			identifier += ` ${this.contextify(options.context, this.addon)}`; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		if(this.regExp) | 
					
						
							| 
									
										
										
										
											2017-02-18 17:29:21 +08:00
										 |  |  | 			identifier += ` ${this.prettyRegExp(this.regExp + "")}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return identifier; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	needRebuild(fileTimestamps, contextTimestamps) { | 
					
						
							|  |  |  | 		const ts = contextTimestamps[this.context]; | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 		if(!ts) { | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		return ts >= this.builtTime; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	unbuild() { | 
					
						
							|  |  |  | 		this.built = false; | 
					
						
							|  |  |  | 		super.unbuild(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	build(options, compilation, resolver, fs, callback) { | 
					
						
							|  |  |  | 		this.built = true; | 
					
						
							|  |  |  | 		this.builtTime = new Date().getTime(); | 
					
						
							|  |  |  | 		this.resolveDependencies(fs, this.context, this.recursive, this.regExp, (err, dependencies) => { | 
					
						
							|  |  |  | 			if(err) return callback(err); | 
					
						
							| 
									
										
										
										
											2013-02-01 01:00:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 			if(!dependencies) { | 
					
						
							| 
									
										
										
										
											2017-02-19 08:00:06 +08:00
										 |  |  | 				this.dependencies = []; | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2016-06-23 00:24:10 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// enhance dependencies
 | 
					
						
							|  |  |  | 			dependencies.forEach(dep => { | 
					
						
							|  |  |  | 				dep.loc = dep.userRequest; | 
					
						
							|  |  |  | 				dep.request = this.addon + dep.request; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// if these we are not a async context
 | 
					
						
							|  |  |  | 			// add dependencies and continue
 | 
					
						
							|  |  |  | 			if(!this.async) { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 				this.dependencies = dependencies; | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 				return; | 
					
						
							| 
									
										
										
										
											2015-07-01 06:19:52 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-18 18:09:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// if we are async however create a new async dependency block
 | 
					
						
							|  |  |  | 			// and add that block to this context
 | 
					
						
							|  |  |  | 			dependencies.forEach(dep => { | 
					
						
							|  |  |  | 				const block = new AsyncDependenciesBlock(null, dep.module, dep.loc); | 
					
						
							|  |  |  | 				block.addDependency(dep); | 
					
						
							|  |  |  | 				this.addBlock(block); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2015-07-01 06:19:52 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-26 20:48:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 	getSourceWithDependencies(dependencies, id) { | 
					
						
							|  |  |  | 		// if we filter first we get a new array
 | 
					
						
							|  |  |  | 		// therefor we dont need to create a clone of dependencies explicitly
 | 
					
						
							|  |  |  | 		// therefore the order of this is !important!
 | 
					
						
							|  |  |  | 		const map = dependencies | 
					
						
							|  |  |  | 			.filter(dependency => dependency.module) | 
					
						
							|  |  |  | 			.sort((a, b) => { | 
					
						
							|  |  |  | 				if(a.userRequest === b.userRequest) { | 
					
						
							|  |  |  | 					return 0; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 				return a.userRequest < b.userRequest ? -1 : 1; | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 			}).reduce(function(map, dep) { | 
					
						
							|  |  |  | 				map[dep.userRequest] = dep.module.id; | 
					
						
							|  |  |  | 				return map; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 			}, Object.create(null)); | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 		return `var map = ${JSON.stringify(map, null, "\t")};
 | 
					
						
							|  |  |  | function webpackContext(req) { | 
					
						
							|  |  |  | 	return __webpack_require__(webpackContextResolve(req)); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | function webpackContextResolve(req) { | 
					
						
							|  |  |  | 	var id = map[req]; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 	if(!(id + 1)) // check for number or string
 | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 		throw new Error("Cannot find module '" + req + "'."); | 
					
						
							|  |  |  | 	return id; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | webpackContext.keys = function webpackContextKeys() { | 
					
						
							|  |  |  | 	return Object.keys(map); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | webpackContext.resolve = webpackContextResolve; | 
					
						
							|  |  |  | module.exports = webpackContext; | 
					
						
							|  |  |  | webpackContext.id = ${JSON.stringify(id)};`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSourceWithBlocks(blocks, id) { | 
					
						
							| 
									
										
										
										
											2017-02-18 20:01:29 +08:00
										 |  |  | 		let hasMultipleOrNoChunks = false; | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 		const map = blocks | 
					
						
							|  |  |  | 			.filter(block => block.dependencies[0].module) | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 			.map((block) => ({ | 
					
						
							|  |  |  | 				dependency: block.dependencies[0], | 
					
						
							|  |  |  | 				block: block, | 
					
						
							|  |  |  | 				userRequest: block.dependencies[0].userRequest | 
					
						
							|  |  |  | 			})).sort((a, b) => { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 				if(a.userRequest === b.userRequest) return 0; | 
					
						
							|  |  |  | 				return a.userRequest < b.userRequest ? -1 : 1; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 			}).reduce((map, item) => { | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 				const chunks = item.block.chunks || []; | 
					
						
							|  |  |  | 				if(chunks.length !== 1) { | 
					
						
							| 
									
										
										
										
											2017-02-18 20:01:29 +08:00
										 |  |  | 					hasMultipleOrNoChunks = true; | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 				map[item.userRequest] = [item.dependency.module.id] | 
					
						
							|  |  |  | 					.concat(chunks.map(chunk => chunk.id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return map; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:52:11 +08:00
										 |  |  | 			}, Object.create(null)); | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:01:29 +08:00
										 |  |  | 		const requestPrefix = hasMultipleOrNoChunks ? | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 			"Promise.all(ids.slice(1).map(__webpack_require__.e))" : | 
					
						
							|  |  |  | 			"__webpack_require__.e(ids[1])"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return `var map = ${JSON.stringify(map, null, "\t")};
 | 
					
						
							|  |  |  | function webpackAsyncContext(req) { | 
					
						
							|  |  |  | 	var ids = map[req]; | 
					
						
							|  |  |  | 	if(!ids) | 
					
						
							|  |  |  | 		return Promise.reject(new Error("Cannot find module '" + req + "'.")); | 
					
						
							|  |  |  | 	return ${requestPrefix}.then(function() { | 
					
						
							|  |  |  | 		return __webpack_require__(ids[0]); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | webpackAsyncContext.keys = function webpackAsyncContextKeys() { | 
					
						
							|  |  |  | 	return Object.keys(map); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | module.exports = webpackAsyncContext; | 
					
						
							|  |  |  | webpackAsyncContext.id = ${JSON.stringify(id)};`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSourceForEmptyContext(id) { | 
					
						
							|  |  |  | 		return `function webpackEmptyContext(req) {
 | 
					
						
							|  |  |  | 	throw new Error("Cannot find module '" + req + "'."); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | webpackEmptyContext.keys = function() { return []; }; | 
					
						
							|  |  |  | webpackEmptyContext.resolve = webpackEmptyContext; | 
					
						
							|  |  |  | module.exports = webpackEmptyContext; | 
					
						
							|  |  |  | webpackEmptyContext.id = ${JSON.stringify(id)};`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSourceString() { | 
					
						
							|  |  |  | 		if(this.dependencies && this.dependencies.length > 0) { | 
					
						
							|  |  |  | 			return this.getSourceWithDependencies(this.dependencies, this.id); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(this.blocks && this.blocks.length > 0) { | 
					
						
							|  |  |  | 			return this.getSourceWithBlocks(this.blocks, this.id); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return this.getSourceForEmptyContext(this.id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getSource(sourceString) { | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		if(this.useSourceMap) { | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 			return new OriginalSource(sourceString, this.identifier()); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-18 19:41:39 +08:00
										 |  |  | 		return new RawSource(sourceString); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	source() { | 
					
						
							|  |  |  | 		return this.getSource( | 
					
						
							|  |  |  | 			this.getSourceString() | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2013-10-14 19:59:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	size() { | 
					
						
							| 
									
										
										
										
											2017-02-18 18:13:39 +08:00
										 |  |  | 		// base penalty
 | 
					
						
							| 
									
										
										
										
											2017-02-28 19:07:27 +08:00
										 |  |  | 		const initialSize = 160; | 
					
						
							| 
									
										
										
										
											2017-02-18 18:13:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// if we dont have dependencies we stop here.
 | 
					
						
							| 
									
										
										
										
											2017-02-19 08:00:06 +08:00
										 |  |  | 		return this.dependencies | 
					
						
							| 
									
										
										
										
											2017-02-28 19:07:27 +08:00
										 |  |  | 			.reduce((size, dependency) => size + 5 + dependency.userRequest.length, initialSize); | 
					
						
							| 
									
										
										
										
											2017-02-18 17:12:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ContextModule; |