@@ -12,63 +12,68 @@ const DevMiddlewareError = require('./DevMiddlewareError');
1212module . exports = {
1313 toDisk ( context ) {
1414 const compilers = context . compiler . compilers || [ context . compiler ] ;
15+
1516 for ( const compiler of compilers ) {
16- compiler . hooks . afterEmit . tap ( 'WebpackDevMiddleware' , ( compilation ) => {
17- const { assets } = compilation ;
18- const { log } = context ;
19- const { writeToDisk : filter } = context . options ;
20- let { outputPath } = compiler ;
17+ compiler . hooks . emit . tap ( 'WebpackDevMiddleware' , ( compilation ) => {
18+ compiler . hooks . assetEmitted . tapAsync (
19+ 'WebpackDevMiddleware' ,
20+ ( file , content , callback ) => {
21+ let targetFile = file ;
2122
22- if ( outputPath === '/' ) {
23- outputPath = compiler . context ;
24- }
23+ const queryStringIdx = targetFile . indexOf ( '?' ) ;
2524
26- for ( const assetPath of Object . keys ( assets ) ) {
27- let targetFile = assetPath ;
25+ if ( queryStringIdx >= 0 ) {
26+ targetFile = targetFile . substr ( 0 , queryStringIdx ) ;
27+ }
2828
29- const queryStringIdx = targetFile . indexOf ( '?' ) ;
29+ let { outputPath } = compiler ;
3030
31- if ( queryStringIdx >= 0 ) {
32- targetFile = targetFile . substr ( 0 , queryStringIdx ) ;
33- }
31+ // TODO Why? Need remove in future major release
32+ if ( outputPath === '/' ) {
33+ outputPath = compiler . context ;
34+ }
3435
35- const targetPath = path . isAbsolute ( targetFile )
36- ? targetFile
37- : path . join ( outputPath , targetFile ) ;
38- const allowWrite =
39- filter && typeof filter === 'function' ? filter ( targetPath ) : true ;
36+ outputPath = compilation . getPath ( outputPath , { } ) ;
4037
41- if ( allowWrite ) {
42- const asset = assets [ assetPath ] ;
43- let content = asset . source ( ) ;
38+ const targetPath = path . join ( outputPath , targetFile ) ;
4439
45- if ( ! Buffer . isBuffer ( content ) ) {
46- // TODO need remove in next major release
47- if ( Array . isArray ( content ) ) {
48- content = content . join ( '\n' ) ;
49- }
40+ const { writeToDisk : filter } = context . options ;
41+ const allowWrite =
42+ filter && typeof filter === 'function'
43+ ? filter ( targetPath )
44+ : true ;
5045
51- content = Buffer . from ( content , 'utf8' ) ;
46+ if ( ! allowWrite ) {
47+ return callback ( ) ;
5248 }
5349
54- mkdirp . sync ( path . dirname ( targetPath ) ) ;
55-
56- try {
57- fs . writeFileSync ( targetPath , content , 'utf-8' ) ;
58-
59- log . debug (
60- colors . cyan (
61- `Asset written to disk: ${ path . relative (
62- process . cwd ( ) ,
63- targetPath
64- ) } `
65- )
66- ) ;
67- } catch ( e ) {
68- log . error ( `Unable to write asset to disk:\n${ e } ` ) ;
69- }
50+ const { log } = context ;
51+ const dir = path . dirname ( targetPath ) ;
52+
53+ return mkdirp ( dir , ( mkdirpError ) => {
54+ if ( mkdirpError ) {
55+ return callback ( mkdirpError ) ;
56+ }
57+
58+ return fs . writeFile ( targetPath , content , ( writeFileError ) => {
59+ if ( writeFileError ) {
60+ return callback ( writeFileError ) ;
61+ }
62+
63+ log . debug (
64+ colors . cyan (
65+ `Asset written to disk: ${ path . relative (
66+ process . cwd ( ) ,
67+ targetPath
68+ ) } `
69+ )
70+ ) ;
71+
72+ return callback ( ) ;
73+ } ) ;
74+ } ) ;
7075 }
71- }
76+ ) ;
7277 } ) ;
7378 }
7479 } ,
0 commit comments