| 
									
										
										
										
											2016-09-06 05:41:03 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2016-09-06 05:41:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2017-12-01 17:42:43 +08:00
										 |  |  | const Queue = require("./util/Queue"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | /** @typedef {import("./DependenciesBlock")} DependenciesBlock */ | 
					
						
							|  |  |  | /** @typedef {import("./Dependency")} Dependency */ | 
					
						
							|  |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const addToSet = (a, b) => { | 
					
						
							|  |  |  | 	let changed = false; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	for (const item of b) { | 
					
						
							|  |  |  | 		if (!a.has(item)) { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 			a.add(item); | 
					
						
							|  |  |  | 			changed = true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-01 17:42:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 	return changed; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | const getCacheIdentifier = (compilation, module) => { | 
					
						
							|  |  |  | 	return `${ | 
					
						
							|  |  |  | 		compilation.compilerPath | 
					
						
							|  |  |  | 	}/FlagDependencyExportsPlugin/${module.identifier()}`;
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | class FlagDependencyExportsPlugin { | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"FlagDependencyExportsPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 				const moduleGraph = compilation.moduleGraph; | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 				compilation.hooks.finishModules.tapAsync( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					"FlagDependencyExportsPlugin", | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 					(modules, callback) => { | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 						/** @type {Queue<Module>} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const queue = new Queue(); | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						// Step 1: Try to restore cached provided export info from cache
 | 
					
						
							|  |  |  | 						asyncLib.each( | 
					
						
							|  |  |  | 							modules, | 
					
						
							|  |  |  | 							(module, callback) => { | 
					
						
							|  |  |  | 								if ( | 
					
						
							|  |  |  | 									module.buildInfo.cacheable !== true || | 
					
						
							|  |  |  | 									typeof module.buildInfo.hash !== "string" | 
					
						
							|  |  |  | 								) { | 
					
						
							|  |  |  | 									// Enqueue uncacheable module for determining the exports
 | 
					
						
							|  |  |  | 									queue.enqueue(module); | 
					
						
							|  |  |  | 									return callback(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 								compilation.cache.get( | 
					
						
							|  |  |  | 									getCacheIdentifier(compilation, module), | 
					
						
							|  |  |  | 									module.buildInfo.hash, | 
					
						
							|  |  |  | 									(err, result) => { | 
					
						
							|  |  |  | 										if (err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 										if (result !== undefined) { | 
					
						
							|  |  |  | 											moduleGraph.setProvidedExports(module, result); | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											// Without cached info enqueue module for determining the exports
 | 
					
						
							|  |  |  | 											queue.enqueue(module); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										callback(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 								); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							err => { | 
					
						
							|  |  |  | 								if (err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								/** @type {Set<Module>} */ | 
					
						
							|  |  |  | 								const modulesToStore = new Set(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								/** @type {Map<Module, Set<Module>>} */ | 
					
						
							|  |  |  | 								const dependencies = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								/** @type {Module} */ | 
					
						
							|  |  |  | 								let module; | 
					
						
							|  |  |  | 								/** @type {Set<string> | true} */ | 
					
						
							|  |  |  | 								let moduleProvidedExports; | 
					
						
							|  |  |  | 								let providedExportsAreCacheable = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								/** | 
					
						
							|  |  |  | 								 * @param {DependenciesBlock} depBlock the dependencies block | 
					
						
							|  |  |  | 								 * @returns {boolean} true if the traversal must be continued | 
					
						
							|  |  |  | 								 */ | 
					
						
							|  |  |  | 								const processDependenciesBlock = depBlock => { | 
					
						
							|  |  |  | 									for (const dep of depBlock.dependencies) { | 
					
						
							|  |  |  | 										if (processDependency(dep)) { | 
					
						
							|  |  |  | 											return true; | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									for (const block of depBlock.blocks) { | 
					
						
							|  |  |  | 										if (processDependenciesBlock(block)) { | 
					
						
							|  |  |  | 											return true; | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									return false; | 
					
						
							|  |  |  | 								}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								/** | 
					
						
							|  |  |  | 								 * @param {Dependency} dep the dependency | 
					
						
							|  |  |  | 								 * @returns {boolean} true if the traversal must be continued | 
					
						
							|  |  |  | 								 */ | 
					
						
							|  |  |  | 								const processDependency = dep => { | 
					
						
							|  |  |  | 									const exportDesc = dep.getExports(moduleGraph); | 
					
						
							|  |  |  | 									if (!exportDesc) return; | 
					
						
							|  |  |  | 									const exports = exportDesc.exports; | 
					
						
							|  |  |  | 									// break early if it's only in the worst state
 | 
					
						
							|  |  |  | 									if (moduleProvidedExports === true) { | 
					
						
							|  |  |  | 										return true; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									// break if it should move to the worst state
 | 
					
						
							|  |  |  | 									if (exports === true) { | 
					
						
							|  |  |  | 										moduleProvidedExports = true; | 
					
						
							|  |  |  | 										notifyDependencies(); | 
					
						
							|  |  |  | 										return true; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									// merge in new exports
 | 
					
						
							|  |  |  | 									if (Array.isArray(exports)) { | 
					
						
							|  |  |  | 										if (addToSet(moduleProvidedExports, exports)) { | 
					
						
							|  |  |  | 											notifyDependencies(); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									// store dependencies
 | 
					
						
							|  |  |  | 									const exportDeps = exportDesc.dependencies; | 
					
						
							|  |  |  | 									if (exportDeps) { | 
					
						
							|  |  |  | 										providedExportsAreCacheable = false; | 
					
						
							|  |  |  | 										for (const exportDependency of exportDeps) { | 
					
						
							|  |  |  | 											// add dependency for this module
 | 
					
						
							|  |  |  | 											const set = dependencies.get(exportDependency); | 
					
						
							|  |  |  | 											if (set === undefined) { | 
					
						
							|  |  |  | 												dependencies.set(exportDependency, new Set([module])); | 
					
						
							|  |  |  | 											} else { | 
					
						
							|  |  |  | 												set.add(module); | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									return false; | 
					
						
							|  |  |  | 								}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								const notifyDependencies = () => { | 
					
						
							|  |  |  | 									const deps = dependencies.get(module); | 
					
						
							|  |  |  | 									if (deps !== undefined) { | 
					
						
							|  |  |  | 										for (const dep of deps) { | 
					
						
							|  |  |  | 											queue.enqueue(dep); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								while (queue.length > 0) { | 
					
						
							|  |  |  | 									module = queue.dequeue(); | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 									const providedExports = moduleGraph.getProvidedExports( | 
					
						
							|  |  |  | 										module | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									); | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 									if (providedExports !== true) { | 
					
						
							|  |  |  | 										if (!module.buildMeta || !module.buildMeta.exportsType) { | 
					
						
							|  |  |  | 											// It's a module without declared exports
 | 
					
						
							|  |  |  | 											moduleGraph.setProvidedExports(module, true); | 
					
						
							|  |  |  | 											modulesToStore.add(module); | 
					
						
							|  |  |  | 											notifyDependencies(); | 
					
						
							|  |  |  | 										} else { | 
					
						
							|  |  |  | 											// It's a module with declared exports
 | 
					
						
							|  |  |  | 											moduleProvidedExports = /** @type {Set<string> | true} */ (Array.isArray( | 
					
						
							|  |  |  | 												providedExports | 
					
						
							|  |  |  | 											) | 
					
						
							|  |  |  | 												? new Set(providedExports) | 
					
						
							|  |  |  | 												: new Set()); | 
					
						
							|  |  |  | 											providedExportsAreCacheable = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 											processDependenciesBlock(module); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 											if (providedExportsAreCacheable) { | 
					
						
							|  |  |  | 												modulesToStore.add(module); | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 											moduleGraph.setProvidedExports( | 
					
						
							|  |  |  | 												module, | 
					
						
							|  |  |  | 												moduleProvidedExports === true | 
					
						
							|  |  |  | 													? true | 
					
						
							|  |  |  | 													: Array.from(moduleProvidedExports) | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								asyncLib.each( | 
					
						
							|  |  |  | 									modulesToStore, | 
					
						
							|  |  |  | 									(module, callback) => { | 
					
						
							|  |  |  | 										if ( | 
					
						
							|  |  |  | 											module.buildInfo.cacheable !== true || | 
					
						
							|  |  |  | 											typeof module.buildInfo.hash !== "string" | 
					
						
							|  |  |  | 										) { | 
					
						
							|  |  |  | 											// not cacheable
 | 
					
						
							|  |  |  | 											return callback(); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										compilation.cache.store( | 
					
						
							|  |  |  | 											getCacheIdentifier(compilation, module), | 
					
						
							|  |  |  | 											module.buildInfo.hash, | 
					
						
							|  |  |  | 											moduleGraph.getProvidedExports(module), | 
					
						
							|  |  |  | 											callback | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 									}, | 
					
						
							|  |  |  | 									callback | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-12-30 21:54:47 +08:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 19:42:39 +08:00
										 |  |  | 				/** @type {WeakMap<Module, true | string[] | null>} */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const providedExportsCache = new WeakMap(); | 
					
						
							|  |  |  | 				compilation.hooks.rebuildModule.tap( | 
					
						
							|  |  |  | 					"FlagDependencyExportsPlugin", | 
					
						
							|  |  |  | 					module => { | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 						providedExportsCache.set( | 
					
						
							|  |  |  | 							module, | 
					
						
							|  |  |  | 							moduleGraph.getProvidedExports(module) | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.hooks.finishRebuildingModule.tap( | 
					
						
							|  |  |  | 					"FlagDependencyExportsPlugin", | 
					
						
							|  |  |  | 					module => { | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 						moduleGraph.setProvidedExports( | 
					
						
							|  |  |  | 							module, | 
					
						
							|  |  |  | 							providedExportsCache.get(module) | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-06 05:41:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 18:59:56 +08:00
										 |  |  | module.exports = FlagDependencyExportsPlugin; |