| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | const fs = require("fs"); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | const prettier = require("prettier"); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | const terser = require("terser"); | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // When --write is set, files will be written in place
 | 
					
						
							|  |  |  | // Otherwise it only prints outdated files
 | 
					
						
							|  |  |  | const doWrite = process.argv.includes("--write"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const files = ["lib/util/semver.js"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							|  |  |  | 	for (const file of files) { | 
					
						
							|  |  |  | 		const filePath = path.resolve(__dirname, "..", file); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 		const content = fs.readFileSync(filePath, "utf8"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 		const exports = require(`../${file}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 		const regexp = | 
					
						
							|  |  |  | 			/\n\/\/#region runtime code: (.+)\n[\s\S]+?\/\/#endregion\n/g; | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const replaces = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		let match = regexp.exec(content); | 
					
						
							|  |  |  | 		while (match) { | 
					
						
							|  |  |  | 			const [fullMatch, name] = match; | 
					
						
							|  |  |  | 			const originalCode = exports[name].toString(); | 
					
						
							| 
									
										
										
										
											2023-06-22 05:55:05 +08:00
										 |  |  | 			const header = | 
					
						
							|  |  |  | 				/** @type {RegExpExecArray} */ | 
					
						
							|  |  |  | 				(/^\(?([^=)]+)\)?\s=> \{/.exec(originalCode)); | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 			const body = originalCode.slice(header[0].length, -1); | 
					
						
							|  |  |  | 			const result = await terser.minify( | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 					"input.js": body | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					compress: true, | 
					
						
							|  |  |  | 					mangle: true, | 
					
						
							|  |  |  | 					ecma: 5, | 
					
						
							|  |  |  | 					toplevel: true, | 
					
						
							|  |  |  | 					parse: { | 
					
						
							| 
									
										
										
										
											2024-07-31 09:37:24 +08:00
										 |  |  | 						// eslint-disable-next-line camelcase
 | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 						bare_returns: true | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 05:55:05 +08:00
										 |  |  | 			if (!result.code) { | 
					
						
							|  |  |  | 				throw new Error(`No code generated for ${name} in ${file}`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 			const args = header[1]; | 
					
						
							|  |  |  | 			if (/`|const|let|=>|\.\.\./.test(result.code)) { | 
					
						
							|  |  |  | 				throw new Error(`Code Style of ${name} in ${file} is too high`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			let templateLiteral = false; | 
					
						
							|  |  |  | 			const code = result.code | 
					
						
							|  |  |  | 				.replace(/\\/g, "\\\\") | 
					
						
							|  |  |  | 				.replace(/'/g, "\\'") | 
					
						
							|  |  |  | 				.replace(/function\(([^)]+)\)/g, (m, args) => { | 
					
						
							|  |  |  | 					templateLiteral = true; | 
					
						
							|  |  |  | 					return `\${runtimeTemplate.supportsArrowFunction() ? '${ | 
					
						
							|  |  |  | 						args.includes(",") ? `(${args})` : args | 
					
						
							|  |  |  | 					}=>' : 'function(${args})'}`;
 | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			replaces.set( | 
					
						
							|  |  |  | 				fullMatch, | 
					
						
							|  |  |  | 				`
 | 
					
						
							|  |  |  | //#region runtime code: ${name}
 | 
					
						
							| 
									
										
										
										
											2024-10-24 11:02:20 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {RuntimeTemplate} runtimeTemplate | 
					
						
							|  |  |  |  * @returns {string} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | exports.${name}RuntimeCode = runtimeTemplate => \`var ${name} = \${runtimeTemplate.basicFunction("${args}", [
 | 
					
						
							|  |  |  | 	"// see webpack/${file} for original code", | 
					
						
							|  |  |  | 	${templateLiteral ? `\`${code}\`` : `'${code}'`} | 
					
						
							|  |  |  | ])}\`;
 | 
					
						
							|  |  |  | //#endregion
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			match = regexp.exec(content); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 		const prettierConfig = await prettier.resolveConfig(filePath); | 
					
						
							|  |  |  | 		const newContent = await prettier.format( | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 			content.replace(regexp, (match) => replaces.get(match)), | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 			{ filepath: filePath, ...prettierConfig } | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (newContent !== content) { | 
					
						
							|  |  |  | 			if (doWrite) { | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 				fs.writeFileSync(filePath, newContent, "utf8"); | 
					
						
							| 
									
										
										
										
											2020-08-05 14:41:50 +08:00
										 |  |  | 				console.error(`${file} updated`); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				console.error(`${file} need to be updated`); | 
					
						
							|  |  |  | 				process.exitCode = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | })(); |