| 
									
										
										
										
											2017-12-31 02:34:14 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-09 21:26:35 +08:00
										 |  |  | const webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2020-02-13 00:13:53 +08:00
										 |  |  | const { createFsFromVolume, Volume } = require("memfs"); | 
					
						
							| 
									
										
										
										
											2017-12-31 02:34:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-28 16:56:11 +08:00
										 |  |  | const compile = options => { | 
					
						
							|  |  |  | 	return new Promise((resolve, reject) => { | 
					
						
							|  |  |  | 		const compiler = webpack(options); | 
					
						
							| 
									
										
										
										
											2020-02-15 13:54:52 +08:00
										 |  |  | 		compiler.outputFileSystem = createFsFromVolume(new Volume()); | 
					
						
							| 
									
										
										
										
											2017-12-31 02:34:14 +08:00
										 |  |  | 		compiler.run((err, stats) => { | 
					
						
							| 
									
										
										
										
											2018-12-28 16:56:11 +08:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				reject(err); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				resolve(stats); | 
					
						
							| 
									
										
										
										
											2017-12-31 02:34:14 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-12-28 16:56:11 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("Stats", () => { | 
					
						
							|  |  |  | 	it("should print env string in stats", async () => { | 
					
						
							|  |  |  | 		const stats = await compile({ | 
					
						
							| 
									
										
										
										
											2018-12-26 03:58:22 +08:00
										 |  |  | 			context: __dirname, | 
					
						
							|  |  |  | 			entry: "./fixtures/a" | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-12-28 16:56:11 +08:00
										 |  |  | 		expect( | 
					
						
							|  |  |  | 			stats.toString({ | 
					
						
							|  |  |  | 				all: false, | 
					
						
							|  |  |  | 				env: true, | 
					
						
							|  |  |  | 				_env: "production" | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		).toBe('Environment (--env): "production"'); | 
					
						
							|  |  |  | 		expect( | 
					
						
							|  |  |  | 			stats.toString({ | 
					
						
							|  |  |  | 				all: false, | 
					
						
							|  |  |  | 				env: true, | 
					
						
							|  |  |  | 				_env: { | 
					
						
							|  |  |  | 					prod: ["foo", "bar"], | 
					
						
							|  |  |  | 					baz: true | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		).toBe( | 
					
						
							|  |  |  | 			"Environment (--env): {\n" + | 
					
						
							|  |  |  | 				'  "prod": [\n' + | 
					
						
							|  |  |  | 				'    "foo",\n' + | 
					
						
							|  |  |  | 				'    "bar"\n' + | 
					
						
							|  |  |  | 				"  ],\n" + | 
					
						
							|  |  |  | 				'  "baz": true\n' + | 
					
						
							|  |  |  | 				"}" | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	it("should omit all properties with all false", async () => { | 
					
						
							|  |  |  | 		const stats = await compile({ | 
					
						
							|  |  |  | 			context: __dirname, | 
					
						
							|  |  |  | 			entry: "./fixtures/a" | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		expect( | 
					
						
							|  |  |  | 			stats.toJson({ | 
					
						
							|  |  |  | 				all: false | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		).toEqual({}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	describe("chunkGroups", () => { | 
					
						
							|  |  |  | 		it("should be empty when there is no additional chunks", async () => { | 
					
						
							|  |  |  | 			const stats = await compile({ | 
					
						
							|  |  |  | 				context: __dirname, | 
					
						
							|  |  |  | 				entry: { | 
					
						
							|  |  |  | 					entryA: "./fixtures/a", | 
					
						
							|  |  |  | 					entryB: "./fixtures/b" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			expect( | 
					
						
							|  |  |  | 				stats.toJson({ | 
					
						
							|  |  |  | 					all: false, | 
					
						
							|  |  |  | 					chunkGroups: true | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			).toMatchInlineSnapshot(`
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			Object { | 
					
						
							|  |  |  | 			  "namedChunkGroups": Object { | 
					
						
							|  |  |  | 			    "entryA": Object { | 
					
						
							|  |  |  | 			      "assets": Array [ | 
					
						
							|  |  |  | 			        "entryA.js", | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "auxiliaryAssets": Array [], | 
					
						
							|  |  |  | 			      "childAssets": Object {}, | 
					
						
							|  |  |  | 			      "children": Object {}, | 
					
						
							|  |  |  | 			      "chunks": Array [ | 
					
						
							|  |  |  | 			        938, | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "name": "entryA", | 
					
						
							|  |  |  | 			    }, | 
					
						
							|  |  |  | 			    "entryB": Object { | 
					
						
							|  |  |  | 			      "assets": Array [ | 
					
						
							|  |  |  | 			        "entryB.js", | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "auxiliaryAssets": Array [], | 
					
						
							|  |  |  | 			      "childAssets": Object {}, | 
					
						
							|  |  |  | 			      "children": Object {}, | 
					
						
							|  |  |  | 			      "chunks": Array [ | 
					
						
							|  |  |  | 			        513, | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "name": "entryB", | 
					
						
							|  |  |  | 			    }, | 
					
						
							|  |  |  | 			  }, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		`);
 | 
					
						
							| 
									
										
										
										
											2018-12-28 16:56:11 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 		it("should contain additional chunks", async () => { | 
					
						
							|  |  |  | 			const stats = await compile({ | 
					
						
							|  |  |  | 				context: __dirname, | 
					
						
							|  |  |  | 				entry: { | 
					
						
							|  |  |  | 					entryA: "./fixtures/a", | 
					
						
							|  |  |  | 					entryB: "./fixtures/chunk-b" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			expect( | 
					
						
							|  |  |  | 				stats.toJson({ | 
					
						
							|  |  |  | 					all: false, | 
					
						
							|  |  |  | 					chunkGroups: true | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			).toMatchInlineSnapshot(`
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			Object { | 
					
						
							|  |  |  | 			  "namedChunkGroups": Object { | 
					
						
							|  |  |  | 			    "chunkB": Object { | 
					
						
							|  |  |  | 			      "assets": Array [ | 
					
						
							|  |  |  | 			        "chunkB.js", | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "auxiliaryAssets": Array [], | 
					
						
							|  |  |  | 			      "childAssets": Object {}, | 
					
						
							|  |  |  | 			      "children": Object {}, | 
					
						
							|  |  |  | 			      "chunks": Array [ | 
					
						
							|  |  |  | 			        336, | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "name": "chunkB", | 
					
						
							|  |  |  | 			    }, | 
					
						
							|  |  |  | 			    "entryA": Object { | 
					
						
							|  |  |  | 			      "assets": Array [ | 
					
						
							|  |  |  | 			        "entryA.js", | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "auxiliaryAssets": Array [], | 
					
						
							|  |  |  | 			      "childAssets": Object {}, | 
					
						
							|  |  |  | 			      "children": Object {}, | 
					
						
							|  |  |  | 			      "chunks": Array [ | 
					
						
							|  |  |  | 			        938, | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "name": "entryA", | 
					
						
							|  |  |  | 			    }, | 
					
						
							|  |  |  | 			    "entryB": Object { | 
					
						
							|  |  |  | 			      "assets": Array [ | 
					
						
							|  |  |  | 			        "entryB.js", | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "auxiliaryAssets": Array [], | 
					
						
							|  |  |  | 			      "childAssets": Object {}, | 
					
						
							|  |  |  | 			      "children": Object {}, | 
					
						
							|  |  |  | 			      "chunks": Array [ | 
					
						
							|  |  |  | 			        513, | 
					
						
							|  |  |  | 			      ], | 
					
						
							|  |  |  | 			      "name": "entryB", | 
					
						
							|  |  |  | 			    }, | 
					
						
							|  |  |  | 			  }, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		`);
 | 
					
						
							| 
									
										
										
										
											2018-12-31 16:15:01 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 		it("should contain assets", async () => { | 
					
						
							|  |  |  | 			const stats = await compile({ | 
					
						
							|  |  |  | 				context: __dirname, | 
					
						
							|  |  |  | 				entry: { | 
					
						
							|  |  |  | 					entryA: "./fixtures/a", | 
					
						
							|  |  |  | 					entryB: "./fixtures/chunk-b" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			expect( | 
					
						
							|  |  |  | 				stats.toJson({ | 
					
						
							|  |  |  | 					all: false, | 
					
						
							|  |  |  | 					assets: true | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			).toMatchInlineSnapshot(`
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			Object { | 
					
						
							|  |  |  | 			  "assets": Array [ | 
					
						
							|  |  |  | 			    Object { | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "auxiliaryChunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "auxiliaryChunkNames": Array [], | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "chunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "chunkNames": Array [ | 
					
						
							|  |  |  | 			        "chunkB", | 
					
						
							|  |  |  | 			      ], | 
					
						
							| 
									
										
										
										
											2019-11-04 23:48:47 +08:00
										 |  |  | 			      "comparedForEmit": false, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "emitted": true, | 
					
						
							| 
									
										
										
										
											2019-09-13 18:53:53 +08:00
										 |  |  | 			      "info": Object { | 
					
						
							|  |  |  | 			        "size": 111, | 
					
						
							|  |  |  | 			      }, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "name": "chunkB.js", | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 			      "size": 111, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			    }, | 
					
						
							|  |  |  | 			    Object { | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "auxiliaryChunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "auxiliaryChunkNames": Array [], | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "chunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "chunkNames": Array [ | 
					
						
							|  |  |  | 			        "entryA", | 
					
						
							|  |  |  | 			      ], | 
					
						
							| 
									
										
										
										
											2019-11-04 23:48:47 +08:00
										 |  |  | 			      "comparedForEmit": false, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "emitted": true, | 
					
						
							| 
									
										
										
										
											2019-09-13 18:53:53 +08:00
										 |  |  | 			      "info": Object { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			        "size": 182, | 
					
						
							| 
									
										
										
										
											2019-09-13 18:53:53 +08:00
										 |  |  | 			      }, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "name": "entryA.js", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			      "size": 182, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			    }, | 
					
						
							|  |  |  | 			    Object { | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "auxiliaryChunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "auxiliaryChunkNames": Array [], | 
					
						
							| 
									
										
										
										
											2019-10-10 20:32:07 +08:00
										 |  |  | 			      "chunkIdHints": Array [], | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "chunkNames": Array [ | 
					
						
							|  |  |  | 			        "entryB", | 
					
						
							|  |  |  | 			      ], | 
					
						
							| 
									
										
										
										
											2019-11-04 23:48:47 +08:00
										 |  |  | 			      "comparedForEmit": false, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "emitted": true, | 
					
						
							| 
									
										
										
										
											2019-09-13 18:53:53 +08:00
										 |  |  | 			      "info": Object { | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			        "size": 1865, | 
					
						
							| 
									
										
										
										
											2019-09-13 18:53:53 +08:00
										 |  |  | 			      }, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			      "name": "entryB.js", | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 			      "size": 1865, | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			    }, | 
					
						
							|  |  |  | 			  ], | 
					
						
							|  |  |  | 			  "assetsByChunkName": Object { | 
					
						
							|  |  |  | 			    "chunkB": Array [ | 
					
						
							|  |  |  | 			      "chunkB.js", | 
					
						
							|  |  |  | 			    ], | 
					
						
							|  |  |  | 			    "entryA": Array [ | 
					
						
							|  |  |  | 			      "entryA.js", | 
					
						
							|  |  |  | 			    ], | 
					
						
							|  |  |  | 			    "entryB": Array [ | 
					
						
							|  |  |  | 			      "entryB.js", | 
					
						
							|  |  |  | 			    ], | 
					
						
							|  |  |  | 			  }, | 
					
						
							|  |  |  | 			  "filteredAssets": 0, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		`);
 | 
					
						
							| 
									
										
										
										
											2018-12-26 03:58:22 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-12-31 02:34:14 +08:00
										 |  |  | }); |