| 
									
										
										
										
											2017-01-11 17:51:58 +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-11 17:51:58 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:43:42 +08:00
										 |  |  | const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency"); | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = class HarmonyDetectionParserPlugin { | 
					
						
							|  |  |  | 	apply(parser) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		parser.hooks.program.tap("HarmonyDetectionParserPlugin", ast => { | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 			const isStrictHarmony = parser.state.module.type === "javascript/esm"; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			const isHarmony = | 
					
						
							|  |  |  | 				isStrictHarmony || | 
					
						
							| 
									
										
										
										
											2019-01-04 21:53:08 +08:00
										 |  |  | 				ast.body.some( | 
					
						
							|  |  |  | 					statement => | 
					
						
							|  |  |  | 						statement.type === "ImportDeclaration" || | 
					
						
							|  |  |  | 						statement.type === "ExportDefaultDeclaration" || | 
					
						
							|  |  |  | 						statement.type === "ExportNamedDeclaration" || | 
					
						
							|  |  |  | 						statement.type === "ExportAllDeclaration" | 
					
						
							| 
									
										
										
										
											2019-01-04 03:39:52 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (isHarmony) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:15:47 +08:00
										 |  |  | 				const module = parser.state.module; | 
					
						
							| 
									
										
										
										
											2018-08-07 20:20:53 +08:00
										 |  |  | 				const compatDep = new HarmonyCompatibilityDependency(); | 
					
						
							| 
									
										
										
										
											2017-08-08 15:32:43 +08:00
										 |  |  | 				compatDep.loc = { | 
					
						
							|  |  |  | 					start: { | 
					
						
							|  |  |  | 						line: -1, | 
					
						
							|  |  |  | 						column: 0 | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					end: { | 
					
						
							|  |  |  | 						line: -1, | 
					
						
							|  |  |  | 						column: 0 | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					index: -3 | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				module.addDependency(compatDep); | 
					
						
							| 
									
										
										
										
											2018-09-27 19:40:58 +08:00
										 |  |  | 				parser.state.harmonyModule = true; | 
					
						
							| 
									
										
										
										
											2018-01-09 17:35:08 +08:00
										 |  |  | 				parser.scope.isStrict = true; | 
					
						
							| 
									
										
										
										
											2017-12-23 01:23:20 +08:00
										 |  |  | 				module.buildMeta.exportsType = "namespace"; | 
					
						
							| 
									
										
										
										
											2017-12-06 19:09:17 +08:00
										 |  |  | 				module.buildInfo.strict = true; | 
					
						
							|  |  |  | 				module.buildInfo.exportsArgument = "__webpack_exports__"; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (isStrictHarmony) { | 
					
						
							| 
									
										
										
										
											2017-12-06 19:09:17 +08:00
										 |  |  | 					module.buildMeta.strictHarmonyModule = true; | 
					
						
							|  |  |  | 					module.buildInfo.moduleArgument = "__webpack_module__"; | 
					
						
							| 
									
										
										
										
											2017-11-23 21:10:52 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const skipInHarmony = () => { | 
					
						
							|  |  |  | 			const module = parser.state.module; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			if (module && module.buildMeta && module.buildMeta.exportsType) { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				return true; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const nullInHarmony = () => { | 
					
						
							|  |  |  | 			const module = parser.state.module; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			if (module && module.buildMeta && module.buildMeta.exportsType) { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				return null; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 16:28:45 +08:00
										 |  |  | 		const nonHarmonyIdentifiers = ["define", "exports"]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const identifer of nonHarmonyIdentifiers) { | 
					
						
							|  |  |  | 			parser.hooks.evaluateTypeof | 
					
						
							|  |  |  | 				.for(identifer) | 
					
						
							|  |  |  | 				.tap("HarmonyDetectionParserPlugin", nullInHarmony); | 
					
						
							|  |  |  | 			parser.hooks.typeof | 
					
						
							|  |  |  | 				.for(identifer) | 
					
						
							|  |  |  | 				.tap("HarmonyDetectionParserPlugin", skipInHarmony); | 
					
						
							|  |  |  | 			parser.hooks.evaluate | 
					
						
							|  |  |  | 				.for(identifer) | 
					
						
							|  |  |  | 				.tap("HarmonyDetectionParserPlugin", nullInHarmony); | 
					
						
							|  |  |  | 			parser.hooks.expression | 
					
						
							|  |  |  | 				.for(identifer) | 
					
						
							|  |  |  | 				.tap("HarmonyDetectionParserPlugin", skipInHarmony); | 
					
						
							|  |  |  | 			parser.hooks.call | 
					
						
							|  |  |  | 				.for(identifer) | 
					
						
							|  |  |  | 				.tap("HarmonyDetectionParserPlugin", skipInHarmony); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; |