@@ -12,63 +12,68 @@ const DevMiddlewareError = require('./DevMiddlewareError');
12
12
module . exports = {
13
13
toDisk ( context ) {
14
14
const compilers = context . compiler . compilers || [ context . compiler ] ;
15
+
15
16
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 ;
21
22
22
- if ( outputPath === '/' ) {
23
- outputPath = compiler . context ;
24
- }
23
+ const queryStringIdx = targetFile . indexOf ( '?' ) ;
25
24
26
- for ( const assetPath of Object . keys ( assets ) ) {
27
- let targetFile = assetPath ;
25
+ if ( queryStringIdx >= 0 ) {
26
+ targetFile = targetFile . substr ( 0 , queryStringIdx ) ;
27
+ }
28
28
29
- const queryStringIdx = targetFile . indexOf ( '?' ) ;
29
+ let { outputPath } = compiler ;
30
30
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
+ }
34
35
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 , { } ) ;
40
37
41
- if ( allowWrite ) {
42
- const asset = assets [ assetPath ] ;
43
- let content = asset . source ( ) ;
38
+ const targetPath = path . join ( outputPath , targetFile ) ;
44
39
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 ;
50
45
51
- content = Buffer . from ( content , 'utf8' ) ;
46
+ if ( ! allowWrite ) {
47
+ return callback ( ) ;
52
48
}
53
49
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
+ } ) ;
70
75
}
71
- }
76
+ ) ;
72
77
} ) ;
73
78
}
74
79
} ,
0 commit comments