@@ -5,63 +5,98 @@ require("../helpers/warmup-webpack");
55const path = require ( "path" ) ;
66const { Volume, createFsFromVolume } = require ( "memfs" ) ;
77
8- describe ( "CleanPlugin" , ( ) => {
9- it ( "stops tracking an asset once it has removed it" , async ( ) => {
10- const webpack = require ( "../.." ) ;
8+ /**
9+ * Creates a compiler emitting `extra.<build>.txt`, a name each build changes,
10+ * into an in-memory output whose `lstat` calls are recorded.
11+ * @param {import("../../").Configuration["output"] } output output options
12+ * @returns {{ fs: ReturnType<typeof createFsFromVolume>, checked: string[], build: (build: number) => Promise<void>, close: () => Promise<void> } } the harness
13+ */
14+ const setup = ( output ) => {
15+ const webpack = require ( "../.." ) ;
1116
12- let build = 0 ;
13- const compiler = webpack ( {
14- mode : "development" ,
15- context : path . join ( __dirname , ".." , "fixtures" ) ,
16- entry : "./a.js" ,
17- output : { path : "/dist" , clean : true } ,
18- plugins : [
19- {
20- apply ( compiler ) {
21- compiler . hooks . thisCompilation . tap ( "Test" , ( compilation ) => {
22- compilation . hooks . processAssets . tap ( "Test" , ( ) => {
23- compilation . emitAsset (
24- `extra.${ build } .txt` ,
25- new compiler . webpack . sources . RawSource ( `${ build } ` )
26- ) ;
27- compilation . emitAsset (
28- "nested/../kept.txt" ,
29- new compiler . webpack . sources . RawSource ( "kept" )
30- ) ;
31- } ) ;
17+ let current = 0 ;
18+ const compiler = webpack ( {
19+ mode : "development" ,
20+ context : path . join ( __dirname , ".." , "fixtures" ) ,
21+ entry : "./a.js" ,
22+ output : { path : "/dist" , ...output } ,
23+ plugins : [
24+ {
25+ apply ( compiler ) {
26+ compiler . hooks . thisCompilation . tap ( "Test" , ( compilation ) => {
27+ compilation . hooks . processAssets . tap ( "Test" , ( ) => {
28+ compilation . emitAsset (
29+ `extra.${ current } .txt` ,
30+ new compiler . webpack . sources . RawSource ( `${ current } ` )
31+ ) ;
32+ compilation . emitAsset (
33+ "nested/../kept.txt" ,
34+ new compiler . webpack . sources . RawSource ( "kept" )
35+ ) ;
3236 } ) ;
33- }
37+ } ) ;
3438 }
35- ]
36- } ) ;
37- const fs = createFsFromVolume ( new Volume ( ) ) ;
38- /** @type {string[] } */
39- const checked = [ ] ;
40- const lstat = fs . lstat . bind ( fs ) ;
41- fs . lstat = /** @type {typeof fs.lstat } */ (
42- ( /** @type {string } */ file , /** @type {EXPECTED_ANY } */ callback ) => {
43- checked . push ( path . basename ( file ) ) ;
44- lstat ( file , callback ) ;
4539 }
46- ) ;
47- compiler . outputFileSystem =
48- /** @type {import("../../").OutputFileSystem } */
49- ( /** @type {unknown } */ ( fs ) ) ;
40+ ]
41+ } ) ;
42+ const fs = createFsFromVolume ( new Volume ( ) ) ;
43+ /** @type {string[] } */
44+ const checked = [ ] ;
45+ const lstat = fs . lstat . bind ( fs ) ;
46+ fs . lstat = /** @type {typeof fs.lstat } */ (
47+ ( /** @type {string } */ file , /** @type {EXPECTED_ANY } */ callback ) => {
48+ checked . push ( path . basename ( file ) ) ;
49+ lstat ( file , callback ) ;
50+ }
51+ ) ;
52+ compiler . outputFileSystem =
53+ /** @type {import("../../").OutputFileSystem } */
54+ ( /** @type {unknown } */ ( fs ) ) ;
5055
51- const run = ( ) =>
56+ return {
57+ fs,
58+ checked,
59+ build : ( build ) =>
5260 new Promise ( ( resolve , reject ) => {
53- compiler . run ( ( err ) => ( err ? reject ( err ) : resolve ( undefined ) ) ) ;
54- } ) ;
61+ current = build ;
62+ checked . length = 0 ;
63+ compiler . run ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
64+ } ) ,
65+ close : ( ) =>
66+ new Promise ( ( resolve ) => {
67+ compiler . close ( ( ) => resolve ( ) ) ;
68+ } )
69+ } ;
70+ } ;
5571
56- for ( build = 0 ; build < 5 ; build ++ ) {
57- checked . length = 0 ;
58- await run ( ) ;
72+ describe ( "CleanPlugin" , ( ) => {
73+ it ( "stops tracking an asset once it has removed it" , async ( ) => {
74+ const { fs, checked, build, close } = setup ( { clean : true } ) ;
75+ for ( let i = 0 ; i < 5 ; i ++ ) {
76+ await build ( i ) ;
5977 // only the asset the previous build emitted is checked and removed
60- expect ( checked ) . toEqual ( build === 0 ? [ ] : [ `extra.${ build - 1 } .txt` ] ) ;
78+ expect ( checked ) . toEqual ( i === 0 ? [ ] : [ `extra.${ i - 1 } .txt` ] ) ;
6179 }
62- expect ( fs . readdirSync ( "/dist" ) . sort ( ) ) . toEqual ( [ "extra.4.txt" , "kept.txt" , "main.js" ] ) ;
63- await new Promise ( ( resolve ) => {
64- compiler . close ( resolve ) ;
65- } ) ;
80+ expect ( fs . readdirSync ( "/dist" ) . sort ( ) ) . toEqual ( [
81+ "extra.4.txt" ,
82+ "kept.txt" ,
83+ "main.js"
84+ ] ) ;
85+ await close ( ) ;
86+ } ) ;
87+
88+ it ( "keeps reporting what dry mode would remove" , async ( ) => {
89+ const { fs, checked, build, close } = setup ( { clean : { dry : true } } ) ;
90+ for ( let i = 0 ; i < 3 ; i ++ ) await build ( i ) ;
91+ // nothing was removed, so every earlier asset is still reported
92+ expect ( checked . sort ( ) ) . toEqual ( [ "extra.0.txt" , "extra.1.txt" ] ) ;
93+ expect ( fs . readdirSync ( "/dist" ) . sort ( ) ) . toEqual ( [
94+ "extra.0.txt" ,
95+ "extra.1.txt" ,
96+ "extra.2.txt" ,
97+ "kept.txt" ,
98+ "main.js"
99+ ] ) ;
100+ await close ( ) ;
66101 } ) ;
67102} ) ;
0 commit comments