| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require("./helpers/warmup-webpack"); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("graceful-fs"); | 
					
						
							|  |  |  | const rimraf = require("rimraf"); | 
					
						
							|  |  |  | const webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | const captureStdio = require("./helpers/captureStdio"); | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | const toMiB = (bytes) => `${Math.round(bytes / 1024 / 1024)}MiB`; | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | const base = path.join(__dirname, "memoryLimitCases"); | 
					
						
							|  |  |  | const outputBase = path.join(__dirname, "js", "memoryLimit"); | 
					
						
							|  |  |  | const tests = fs | 
					
						
							|  |  |  | 	.readdirSync(base) | 
					
						
							|  |  |  | 	.filter( | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		(testName) => | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			fs.existsSync(path.join(base, testName, "index.js")) || | 
					
						
							|  |  |  | 			fs.existsSync(path.join(base, testName, "webpack.config.js")) | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	.filter((testName) => { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 		const testDirectory = path.join(base, testName); | 
					
						
							|  |  |  | 		const filterPath = path.join(testDirectory, "test.filter.js"); | 
					
						
							|  |  |  | 		if (fs.existsSync(filterPath) && !require(filterPath)()) { | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 			// eslint-disable-next-line jest/no-disabled-tests, jest/valid-describe-callback
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			describe.skip(testName, () => it("filtered")); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("MemoryLimitTestCases", () => { | 
					
						
							|  |  |  | 	jest.setTimeout(40000); | 
					
						
							|  |  |  | 	let stderr; | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 	beforeEach(() => { | 
					
						
							|  |  |  | 		stderr = captureStdio(process.stderr, true); | 
					
						
							|  |  |  | 		if (global.gc) { | 
					
						
							|  |  |  | 			global.gc(); | 
					
						
							|  |  |  | 			global.gc(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 	afterEach(() => { | 
					
						
							|  |  |  | 		stderr.restore(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 	for (const testName of tests) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 		let testConfig = { | 
					
						
							|  |  |  | 			heapSizeLimitBytes: 250 * 1024 * 1024 | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			// try to load a test file
 | 
					
						
							|  |  |  | 			testConfig = Object.assign( | 
					
						
							|  |  |  | 				testConfig, | 
					
						
							|  |  |  | 				require(path.join(base, testName, "test.config.js")) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 		} catch (_err) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			// ignored
 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		const size = toMiB(testConfig.heapSizeLimitBytes); | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		// eslint-disable-next-line no-loop-func
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		it(`should build ${JSON.stringify(testName)} with heap limit of ${size}`, (done) => { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			const outputDirectory = path.join(outputBase, testName); | 
					
						
							|  |  |  | 			rimraf.sync(outputDirectory); | 
					
						
							|  |  |  | 			fs.mkdirSync(outputDirectory, { recursive: true }); | 
					
						
							|  |  |  | 			let options = { | 
					
						
							|  |  |  | 				mode: "development", | 
					
						
							|  |  |  | 				entry: "./index", | 
					
						
							|  |  |  | 				output: { | 
					
						
							|  |  |  | 					filename: "bundle.js" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			if (fs.existsSync(path.join(base, testName, "webpack.config.js"))) { | 
					
						
							|  |  |  | 				options = require(path.join(base, testName, "webpack.config.js")); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			const resolvedOptions = Array.isArray(options) ? options : [options]; | 
					
						
							|  |  |  | 			for (const options of resolvedOptions) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 				if (!options.context) options.context = path.join(base, testName); | 
					
						
							|  |  |  | 				if (!options.output) options.output = options.output || {}; | 
					
						
							|  |  |  | 				if (!options.output.path) options.output.path = outputDirectory; | 
					
						
							|  |  |  | 				if (!options.plugins) options.plugins = []; | 
					
						
							|  |  |  | 				if (!options.optimization) options.optimization = {}; | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 				if (options.optimization.minimize === undefined) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 					options.optimization.minimize = false; | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			const heapSizeStart = process.memoryUsage().heapUsed; | 
					
						
							|  |  |  | 			const c = webpack(options); | 
					
						
							|  |  |  | 			const compilers = c.compilers ? c.compilers : [c]; | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			for (const c of compilers) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 				const ifs = c.inputFileSystem; | 
					
						
							|  |  |  | 				c.inputFileSystem = Object.create(ifs); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | 				c.inputFileSystem.readFile = function readFile() { | 
					
						
							| 
									
										
										
										
											2024-07-31 09:37:24 +08:00
										 |  |  | 					// eslint-disable-next-line prefer-rest-params
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 					const args = Array.prototype.slice.call(arguments); | 
					
						
							|  |  |  | 					const callback = args.pop(); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | 					// eslint-disable-next-line no-useless-call
 | 
					
						
							|  |  |  | 					ifs.readFile.apply(ifs, [ | 
					
						
							|  |  |  | 						...args, | 
					
						
							|  |  |  | 						(err, result) => { | 
					
						
							|  |  |  | 							if (err) return callback(err); | 
					
						
							|  |  |  | 							if (!/\.(js|json|txt)$/.test(args[0])) { | 
					
						
							|  |  |  | 								return callback(null, result); | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | 							callback(null, result.toString("utf8").replace(/\r/g, "")); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					]); | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 			c.run((err, stats) => { | 
					
						
							|  |  |  | 				if (err) return done(err); | 
					
						
							| 
									
										
										
										
											2025-04-22 20:42:33 +08:00
										 |  |  | 				expect(stats.hasErrors()).toBe(testName.endsWith("error")); | 
					
						
							|  |  |  | 				if (!testName.endsWith("error") && stats.hasErrors()) { | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | 					return done( | 
					
						
							|  |  |  | 						new Error( | 
					
						
							|  |  |  | 							stats.toString({ | 
					
						
							|  |  |  | 								all: false, | 
					
						
							|  |  |  | 								errors: true, | 
					
						
							|  |  |  | 								errorStack: true, | 
					
						
							|  |  |  | 								errorDetails: true | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				const heapUsed = process.memoryUsage().heapUsed - heapSizeStart; | 
					
						
							|  |  |  | 				if (heapUsed > testConfig.heapSizeLimitBytes) { | 
					
						
							|  |  |  | 					return done( | 
					
						
							|  |  |  | 						new Error(`Out of memory limit with ${toMiB(heapUsed)} heap used`) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (testConfig.validate) testConfig.validate(stats, stderr.toString()); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-10 00:23:51 +08:00
										 |  |  | }); |