| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-07-27 19:24:56 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 19:24:56 +08:00
										 |  |  | const cp = require("child_process"); | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							|  |  |  | const tc = require("./template-common"); | 
					
						
							|  |  |  | const fs = require("fs"); | 
					
						
							| 
									
										
										
										
											2018-02-11 12:27:09 +08:00
										 |  |  | const async = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 19:24:56 +08:00
										 |  |  | const extraArgs = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 17:29:32 +08:00
										 |  |  | const targetArgs = global.NO_TARGET_ARGS ? "" : "--entry ./example.js --output-filename output.js"; | 
					
						
							|  |  |  | const displayReasons = global.NO_REASONS ? "" : "--stats-reasons --stats-used-exports --stats-provided-exports"; | 
					
						
							|  |  |  | const statsArgs = global.NO_STATS_OPTIONS ? "" : "--stats-chunks  --stats-modules-space 99999 --stats-chunk-origins"; | 
					
						
							| 
									
										
										
										
											2020-06-04 08:48:12 +08:00
										 |  |  | const publicPathArgs = global.NO_PUBLIC_PATH ? "" : '--output-public-path "dist/"'; | 
					
						
							| 
									
										
										
										
											2025-04-29 02:11:48 +08:00
										 |  |  | const statsColorsArg = global.STATS_COLORS ? "" : "--no-color"; | 
					
						
							| 
									
										
										
										
											2023-06-20 09:10:49 +08:00
										 |  |  | const commonArgs = `${statsColorsArg} ${statsArgs} ${publicPathArgs} ${extraArgs} ${targetArgs}`; | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | let readme = fs.readFileSync(require("path").join(process.cwd(), "template.md"), "utf-8"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const doCompileAndReplace = (args, prefix, callback) => { | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 	if (!tc.needResults(readme, prefix)) { | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 		callback(); | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-06-05 22:12:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const deleteFiles = (dir) => { | 
					
						
							|  |  |  | 		const targetDir = path.resolve("dist", dir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (path.extname(targetDir) === "") { | 
					
						
							|  |  |  | 			fs.readdirSync(targetDir).forEach((file) => { | 
					
						
							|  |  |  | 				deleteFiles(path.join(targetDir, file)); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			fs.unlinkSync(targetDir); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fs.existsSync("dist")) { | 
					
						
							|  |  |  | 		for (const dir of fs.readdirSync("dist")) { | 
					
						
							|  |  |  | 			deleteFiles(dir); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-04 04:26:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		require.resolve("webpack-cli"); | 
					
						
							|  |  |  | 	} catch (e) { | 
					
						
							|  |  |  | 		throw new Error("Please install webpack-cli at root."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 00:40:27 +08:00
										 |  |  | 	const connectIO = (subprocess) => { | 
					
						
							|  |  |  | 		const { stdin, stdout, stderr } = process; | 
					
						
							|  |  |  | 		const { stdin: _stdin, stdout: _stdout, stderr: _stderr } = subprocess; | 
					
						
							|  |  |  | 		const inputPair = [[stdin, _stdin]]; | 
					
						
							|  |  |  | 		const outputPair = [[stdout, _stdout], [stderr, _stderr]]; | 
					
						
							|  |  |  | 		inputPair.forEach(pair => { | 
					
						
							|  |  |  | 			pair[0].pipe(pair[1]) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		outputPair.forEach(pair => { | 
					
						
							|  |  |  | 			pair[1].pipe(pair[0]) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		disconnectIO = () => { | 
					
						
							|  |  |  | 			inputPair.forEach(pair => { | 
					
						
							|  |  |  | 				pair[0].unpipe(pair[1]) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			outputPair.forEach(pair => { | 
					
						
							|  |  |  | 				pair[1].unpipe(pair[0]) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	let disconnectIO = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const subprocess = cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${args} ${displayReasons} ${commonArgs}`, (error, stdout, stderr) => { | 
					
						
							|  |  |  | 		disconnectIO && disconnectIO(); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 		if (stderr) | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 			console.log(stderr); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 		if (error !== null) | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 			console.log(error); | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2025-04-29 02:11:48 +08:00
										 |  |  | 			readme = tc.replaceResults( | 
					
						
							|  |  |  | 				readme, | 
					
						
							|  |  |  | 				process.cwd(), | 
					
						
							|  |  |  | 				stdout | 
					
						
							|  |  |  | 					.replace(/[\r?\n]*$/, "") | 
					
						
							|  |  |  | 					.replace(/\d\d\d\d-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])/g, "XXXX-XX-XX") | 
					
						
							|  |  |  | 					.replace(/([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/g, "XXXX:XX:XX") | 
					
						
							|  |  |  | 					.replace(/webpack [0-9.]+/g, "webpack X.X.X"), | 
					
						
							|  |  |  | 				prefix | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 		} catch (e) { | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | 			console.log(stderr); | 
					
						
							|  |  |  | 			throw e; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2012-04-05 20:59:01 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2023-09-27 00:40:27 +08:00
										 |  |  | 	connectIO(subprocess); | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async.series([ | 
					
						
							| 
									
										
										
										
											2018-10-10 15:54:13 +08:00
										 |  |  | 	callback => doCompileAndReplace("--mode production --env production", "production", callback), | 
					
						
							|  |  |  | 	callback => doCompileAndReplace("--mode development --env development --devtool none", "development", callback), | 
					
						
							| 
									
										
										
										
											2020-12-11 17:29:32 +08:00
										 |  |  | 	callback => doCompileAndReplace("--mode none --env none --output-pathinfo verbose", "", callback) | 
					
						
							| 
									
										
										
										
											2017-12-14 17:09:09 +08:00
										 |  |  | ], () => { | 
					
						
							|  |  |  | 	readme = tc.replaceBase(readme); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:17:22 +08:00
										 |  |  | 	fs.writeFile("README.md", readme, "utf-8", function () { }); | 
					
						
							| 
									
										
										
										
											2017-06-05 22:12:12 +08:00
										 |  |  | }); |