@@ -14,7 +14,7 @@ import { stat, readFile } from './utils/promisify';
14
14
15
15
/* eslint-disable no-param-reassign */
16
16
17
- export default function postProcessPattern ( globalRef , pattern , file ) {
17
+ export default async function postProcessPattern ( globalRef , pattern , file ) {
18
18
const { logger, compilation, inputFileSystem } = globalRef ;
19
19
20
20
logger . debug ( `getting stats for '${ file . absoluteFrom } ' to write to assets` ) ;
@@ -23,145 +23,136 @@ export default function postProcessPattern(globalRef, pattern, file) {
23
23
? Promise . resolve ( ) . then ( ( ) => pattern . stats )
24
24
: stat ( inputFileSystem , file . absoluteFrom ) ;
25
25
26
- return getStats . then ( ( stats ) => {
27
- // We don't write empty directories
28
- if ( stats . isDirectory ( ) ) {
29
- logger . debug (
30
- `skipping '${ file . absoluteFrom } ' because it is empty directory`
31
- ) ;
26
+ let stats ;
27
+
28
+ try {
29
+ stats = await getStats ;
30
+ } catch ( error ) {
31
+ compilation . errors . push ( error ) ;
32
+ return ;
33
+ }
34
+
35
+ if ( stats . isDirectory ( ) ) {
36
+ logger . debug (
37
+ `skipping '${ file . absoluteFrom } ' because it is empty directory`
38
+ ) ;
39
+ }
40
+
41
+ // If this came from a glob, add it to the file watchlist
42
+ if ( pattern . fromType === 'glob' ) {
43
+ logger . debug ( `add ${ file . absoluteFrom } as fileDependencies` ) ;
44
+ compilation . fileDependencies . add ( file . absoluteFrom ) ;
45
+ }
46
+
47
+ logger . debug ( `reading '${ file . absoluteFrom } ' to write to assets` ) ;
48
+
49
+ let content ;
50
+
51
+ try {
52
+ content = await readFile ( inputFileSystem , file . absoluteFrom ) ;
53
+ } catch ( error ) {
54
+ compilation . errors . push ( error ) ;
55
+ return ;
56
+ }
57
+
58
+ if ( pattern . transform ) {
59
+ logger . log ( `transforming content for '${ file . absoluteFrom } '` ) ;
60
+
61
+ // eslint-disable-next-line no-shadow
62
+ const transform = ( content , absoluteFrom ) =>
63
+ pattern . transform ( content , absoluteFrom ) ;
64
+
65
+ if ( pattern . cacheTransform ) {
66
+ if ( ! globalRef . cacheDir ) {
67
+ globalRef . cacheDir =
68
+ findCacheDir ( { name : 'copy-webpack-plugin' } ) || os . tmpdir ( ) ;
69
+ }
70
+
71
+ const cacheKey = pattern . cacheTransform . key
72
+ ? pattern . cacheTransform . key
73
+ : serialize ( {
74
+ name,
75
+ version,
76
+ pattern,
77
+ hash : crypto . createHash ( 'md4' ) . update ( content ) . digest ( 'hex' ) ,
78
+ } ) ;
79
+
80
+ try {
81
+ const result = await cacache . get ( globalRef . cacheDir , cacheKey ) ;
82
+
83
+ logger . debug (
84
+ `getting cached transformation for '${ file . absoluteFrom } '`
85
+ ) ;
32
86
33
- return Promise . resolve ( ) ;
34
- }
87
+ content = result . data ;
88
+ } catch ( e ) {
89
+ content = await transform ( content , file . absoluteFrom ) ;
90
+
91
+ logger . debug ( `caching transformation for '${ file . absoluteFrom } '` ) ;
35
92
36
- // If this came from a glob, add it to the file watchlist
37
- if ( pattern . fromType === 'glob' ) {
38
- logger . debug ( `add ${ file . absoluteFrom } as fileDependencies` ) ;
39
- compilation . fileDependencies . add ( file . absoluteFrom ) ;
93
+ content = await cacache
94
+ . put ( globalRef . cacheDir , cacheKey , content )
95
+ . then ( ( ) => content ) ;
96
+ }
97
+ } else {
98
+ content = await transform ( content , file . absoluteFrom ) ;
40
99
}
100
+ }
41
101
42
- logger . debug ( `reading '${ file . absoluteFrom } ' to write to assets` ) ;
43
-
44
- return readFile ( inputFileSystem , file . absoluteFrom )
45
- . then ( ( content ) => {
46
- if ( pattern . transform ) {
47
- logger . log ( `transforming content for '${ file . absoluteFrom } '` ) ;
48
-
49
- // eslint-disable-next-line no-shadow
50
- const transform = ( content , absoluteFrom ) =>
51
- pattern . transform ( content , absoluteFrom ) ;
52
-
53
- if ( pattern . cacheTransform ) {
54
- if ( ! globalRef . cacheDir ) {
55
- globalRef . cacheDir =
56
- findCacheDir ( { name : 'copy-webpack-plugin' } ) || os . tmpdir ( ) ;
57
- }
58
-
59
- const cacheKey = pattern . cacheTransform . key
60
- ? pattern . cacheTransform . key
61
- : serialize ( {
62
- name,
63
- version,
64
- pattern,
65
- hash : crypto . createHash ( 'md4' ) . update ( content ) . digest ( 'hex' ) ,
66
- } ) ;
67
-
68
- return cacache . get ( globalRef . cacheDir , cacheKey ) . then (
69
- ( result ) => {
70
- logger . debug (
71
- `getting cached transformation for '${ file . absoluteFrom } '`
72
- ) ;
73
-
74
- return result . data ;
75
- } ,
76
- ( ) =>
77
- Promise . resolve ( )
78
- . then ( ( ) => transform ( content , file . absoluteFrom ) )
79
- // eslint-disable-next-line no-shadow
80
- . then ( ( content ) => {
81
- logger . debug (
82
- `caching transformation for '${ file . absoluteFrom } '`
83
- ) ;
84
-
85
- return cacache
86
- . put ( globalRef . cacheDir , cacheKey , content )
87
- . then ( ( ) => content ) ;
88
- } )
89
- ) ;
90
- }
91
-
92
- content = transform ( content , file . absoluteFrom ) ;
93
- }
94
-
95
- return content ;
96
- } )
97
- . then ( ( content ) => {
98
- if ( pattern . toType === 'template' ) {
99
- logger . log (
100
- `interpolating template '${ file . webpackTo } ' for '${ file . relativeFrom } '`
101
- ) ;
102
-
103
- // If it doesn't have an extension, remove it from the pattern
104
- // ie. [name].[ext] or [name][ext] both become [name]
105
- if ( ! path . extname ( file . relativeFrom ) ) {
106
- file . webpackTo = file . webpackTo . replace ( / \. ? \[ e x t \] / g, '' ) ;
107
- }
108
-
109
- file . webpackTo = loaderUtils . interpolateName (
110
- { resourcePath : file . absoluteFrom } ,
111
- file . webpackTo ,
112
- {
113
- content,
114
- regExp : file . webpackToRegExp ,
115
- context : pattern . context ,
116
- }
117
- ) ;
118
-
119
- // Bug in `loader-utils`, package convert `\\` to `/`, need fix in loader-utils
120
- file . webpackTo = path . normalize ( file . webpackTo ) ;
121
- }
122
-
123
- return content ;
124
- } )
125
- . then ( ( content ) => {
126
- if ( pattern . transformPath ) {
127
- logger . log (
128
- `transforming path '${ file . webpackTo } ' for '${ file . absoluteFrom } '`
129
- ) ;
130
-
131
- return Promise . resolve ( )
132
- . then ( ( ) =>
133
- pattern . transformPath ( file . webpackTo , file . absoluteFrom )
134
- )
135
- . then ( ( newPath ) => {
136
- file . webpackTo = newPath ;
137
-
138
- return content ;
139
- } ) ;
140
- }
141
-
142
- return content ;
143
- } )
144
- . then ( ( content ) => {
145
- const targetPath = normalizePath ( file . webpackTo ) ;
146
-
147
- if ( compilation . assets [ targetPath ] && ! file . force ) {
148
- logger . log ( `skipping '${ file . webpackTo } ', because it already exists` ) ;
149
-
150
- return ;
151
- }
152
-
153
- logger . log (
154
- `writing '${ file . webpackTo } ' to compilation assets from '${ file . absoluteFrom } '`
155
- ) ;
102
+ if ( pattern . toType === 'template' ) {
103
+ logger . log (
104
+ `interpolating template '${ file . webpackTo } ' for '${ file . relativeFrom } '`
105
+ ) ;
106
+
107
+ // If it doesn't have an extension, remove it from the pattern
108
+ // ie. [name].[ext] or [name][ext] both become [name]
109
+ if ( ! path . extname ( file . relativeFrom ) ) {
110
+ file . webpackTo = file . webpackTo . replace ( / \. ? \[ e x t \] / g, '' ) ;
111
+ }
156
112
157
- compilation . assets [ targetPath ] = {
158
- size ( ) {
159
- return stats . size ;
160
- } ,
161
- source ( ) {
162
- return content ;
163
- } ,
164
- } ;
165
- } ) ;
166
- } ) ;
113
+ file . webpackTo = loaderUtils . interpolateName (
114
+ { resourcePath : file . absoluteFrom } ,
115
+ file . webpackTo ,
116
+ {
117
+ content,
118
+ regExp : file . webpackToRegExp ,
119
+ context : pattern . context ,
120
+ }
121
+ ) ;
122
+
123
+ // Bug in `loader-utils`, package convert `\\` to `/`, need fix in loader-utils
124
+ file . webpackTo = path . normalize ( file . webpackTo ) ;
125
+ }
126
+
127
+ if ( pattern . transformPath ) {
128
+ logger . log (
129
+ `transforming path '${ file . webpackTo } ' for '${ file . absoluteFrom } '`
130
+ ) ;
131
+
132
+ file . webpackTo = await pattern . transformPath (
133
+ file . webpackTo ,
134
+ file . absoluteFrom
135
+ ) ;
136
+ }
137
+
138
+ const targetPath = normalizePath ( file . webpackTo ) ;
139
+
140
+ if ( compilation . assets [ targetPath ] && ! file . force ) {
141
+ logger . log ( `skipping '${ file . webpackTo } ', because it already exists` ) ;
142
+
143
+ return ;
144
+ }
145
+
146
+ logger . log (
147
+ `writing '${ file . webpackTo } ' to compilation assets from '${ file . absoluteFrom } '`
148
+ ) ;
149
+
150
+ compilation . assets [ targetPath ] = {
151
+ size ( ) {
152
+ return stats . size ;
153
+ } ,
154
+ source ( ) {
155
+ return content ;
156
+ } ,
157
+ } ;
167
158
}
0 commit comments