| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Yuta Hiroto @hiroppy | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | const Parser = require("../Parser"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 18:11:02 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").AssetParserOptions} AssetParserOptions */ | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | /** @typedef {import("../Parser").ParserState} ParserState */ | 
					
						
							|  |  |  | /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */ | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | class AssetParser extends Parser { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2021-01-05 18:11:02 +08:00
										 |  |  | 	 * @param {AssetParserOptions["dataUrlCondition"] | boolean} dataUrlCondition condition for inlining as DataUrl | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 	constructor(dataUrlCondition) { | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		this.dataUrlCondition = dataUrlCondition; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string | Buffer | PreparsedAst} source the source to parse | 
					
						
							|  |  |  | 	 * @param {ParserState} state the parser state | 
					
						
							|  |  |  | 	 * @returns {ParserState} the parser state | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	parse(source, state) { | 
					
						
							| 
									
										
										
										
											2019-11-30 03:24:13 +08:00
										 |  |  | 		if (typeof source === "object" && !Buffer.isBuffer(source)) { | 
					
						
							|  |  |  | 			throw new Error("AssetParser doesn't accept preparsed AST"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 		state.module.buildInfo.strict = true; | 
					
						
							|  |  |  | 		state.module.buildMeta.exportsType = "default"; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		if (typeof this.dataUrlCondition === "function") { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 			state.module.buildInfo.dataUrl = this.dataUrlCondition(source, { | 
					
						
							|  |  |  | 				filename: state.module.matchResource || state.module.resource, | 
					
						
							|  |  |  | 				module: state.module | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} else if (typeof this.dataUrlCondition === "boolean") { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 			state.module.buildInfo.dataUrl = this.dataUrlCondition; | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		} else if ( | 
					
						
							|  |  |  | 			this.dataUrlCondition && | 
					
						
							|  |  |  | 			typeof this.dataUrlCondition === "object" | 
					
						
							|  |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 			state.module.buildInfo.dataUrl = | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 				Buffer.byteLength(source) <= this.dataUrlCondition.maxSize; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			throw new Error("Unexpected dataUrlCondition type"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 		return state; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | module.exports = AssetParser; |