@@ -21,96 +21,60 @@ function setupWriteToDisk(context) {
21
21
( context . compiler ) . compilers || [ context . compiler ] ;
22
22
23
23
for ( const compiler of compilers ) {
24
- compiler . hooks . emit . tap (
25
- "DevMiddleware" ,
26
- /**
27
- * @param {Compilation } compilation
28
- */
29
- ( compilation ) => {
30
- // @ts -ignore
31
- if ( compiler . hasWebpackDevMiddlewareAssetEmittedCallback ) {
32
- return ;
33
- }
34
-
35
- compiler . hooks . assetEmitted . tapAsync (
36
- "DevMiddleware" ,
37
- ( file , info , callback ) => {
38
- /**
39
- * @type {string }
40
- */
41
- let targetPath ;
42
- /**
43
- * @type {Buffer }
44
- */
45
- let content ;
46
-
47
- // webpack@5
48
- if ( info . compilation ) {
49
- ( { targetPath, content } = info ) ;
50
- } else {
51
- let targetFile = file ;
24
+ compiler . hooks . emit . tap ( "DevMiddleware" , ( ) => {
25
+ // @ts -ignore
26
+ if ( compiler . hasWebpackDevMiddlewareAssetEmittedCallback ) {
27
+ return ;
28
+ }
52
29
53
- const queryStringIdx = targetFile . indexOf ( "?" ) ;
30
+ compiler . hooks . assetEmitted . tapAsync (
31
+ "DevMiddleware" ,
32
+ ( file , info , callback ) => {
33
+ const { targetPath, content } = info ;
34
+ const { writeToDisk : filter } = context . options ;
35
+ const allowWrite =
36
+ filter && typeof filter === "function" ? filter ( targetPath ) : true ;
54
37
55
- if ( queryStringIdx >= 0 ) {
56
- targetFile = targetFile . slice ( 0 , queryStringIdx ) ;
57
- }
58
-
59
- let { outputPath } = compiler ;
38
+ if ( ! allowWrite ) {
39
+ return callback ( ) ;
40
+ }
60
41
61
- outputPath = compilation . getPath ( outputPath , { } ) ;
62
- // @ts -ignore
63
- content = info ;
64
- targetPath = path . join ( outputPath , targetFile ) ;
65
- }
42
+ const dir = path . dirname ( targetPath ) ;
43
+ const name = compiler . options . name
44
+ ? `Child "${ compiler . options . name } ": `
45
+ : "" ;
66
46
67
- const { writeToDisk : filter } = context . options ;
68
- const allowWrite =
69
- filter && typeof filter === "function"
70
- ? filter ( targetPath )
71
- : true ;
47
+ return fs . mkdir ( dir , { recursive : true } , ( mkdirError ) => {
48
+ if ( mkdirError ) {
49
+ context . logger . error (
50
+ ` ${ name } Unable to write " ${ dir } " directory to disk:\n ${ mkdirError } `
51
+ ) ;
72
52
73
- if ( ! allowWrite ) {
74
- return callback ( ) ;
53
+ return callback ( mkdirError ) ;
75
54
}
76
55
77
- const dir = path . dirname ( targetPath ) ;
78
- const name = compiler . options . name
79
- ? `Child "${ compiler . options . name } ": `
80
- : "" ;
81
-
82
- return fs . mkdir ( dir , { recursive : true } , ( mkdirError ) => {
83
- if ( mkdirError ) {
56
+ return fs . writeFile ( targetPath , content , ( writeFileError ) => {
57
+ if ( writeFileError ) {
84
58
context . logger . error (
85
- `${ name } Unable to write "${ dir } " directory to disk:\n${ mkdirError } `
59
+ `${ name } Unable to write "${ targetPath } " asset to disk:\n${ writeFileError } `
86
60
) ;
87
61
88
- return callback ( mkdirError ) ;
62
+ return callback ( writeFileError ) ;
89
63
}
90
64
91
- return fs . writeFile ( targetPath , content , ( writeFileError ) => {
92
- if ( writeFileError ) {
93
- context . logger . error (
94
- `${ name } Unable to write "${ targetPath } " asset to disk:\n${ writeFileError } `
95
- ) ;
96
-
97
- return callback ( writeFileError ) ;
98
- }
99
-
100
- context . logger . log (
101
- `${ name } Asset written to disk: "${ targetPath } "`
102
- ) ;
65
+ context . logger . log (
66
+ `${ name } Asset written to disk: "${ targetPath } "`
67
+ ) ;
103
68
104
- return callback ( ) ;
105
- } ) ;
69
+ return callback ( ) ;
106
70
} ) ;
107
- }
108
- ) ;
71
+ } ) ;
72
+ }
73
+ ) ;
109
74
110
- // @ts -ignore
111
- compiler . hasWebpackDevMiddlewareAssetEmittedCallback = true ;
112
- }
113
- ) ;
75
+ // @ts -ignore
76
+ compiler . hasWebpackDevMiddlewareAssetEmittedCallback = true ;
77
+ } ) ;
114
78
}
115
79
}
116
80
0 commit comments