@@ -21,96 +21,60 @@ function setupWriteToDisk(context) {
2121 ( context . compiler ) . compilers || [ context . compiler ] ;
2222
2323 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+ }
5229
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 ;
5437
55- if ( queryStringIdx >= 0 ) {
56- targetFile = targetFile . slice ( 0 , queryStringIdx ) ;
57- }
58-
59- let { outputPath } = compiler ;
38+ if ( ! allowWrite ) {
39+ return callback ( ) ;
40+ }
6041
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+ : "" ;
6646
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+ ) ;
7252
73- if ( ! allowWrite ) {
74- return callback ( ) ;
53+ return callback ( mkdirError ) ;
7554 }
7655
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 ) {
8458 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 } `
8660 ) ;
8761
88- return callback ( mkdirError ) ;
62+ return callback ( writeFileError ) ;
8963 }
9064
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+ ) ;
10368
104- return callback ( ) ;
105- } ) ;
69+ return callback ( ) ;
10670 } ) ;
107- }
108- ) ;
71+ } ) ;
72+ }
73+ ) ;
10974
110- // @ts -ignore
111- compiler . hasWebpackDevMiddlewareAssetEmittedCallback = true ;
112- }
113- ) ;
75+ // @ts -ignore
76+ compiler . hasWebpackDevMiddlewareAssetEmittedCallback = true ;
77+ } ) ;
11478 }
11579}
11680
0 commit comments