@@ -11,47 +11,21 @@ const globAsync = Promise.promisify(require('glob'));
1111const fs = Promise . promisifyAll ( require ( 'fs-extra' ) ) ;
1212/* eslint-enable */
1313
14- const union = ( set1 , set2 ) => {
15- return new Set ( [ ...set1 , ...set2 ] ) ;
16- } ;
17-
18- const isDevServer = ( compiler ) => {
19- return compiler . outputFileSystem . constructor . name === 'MemoryFileSystem' ;
20- } ;
21-
22- const getOutputDir = ( compiler ) => {
23- if ( compiler . options . output . path && compiler . options . output . path !== '/' ) {
24- return compiler . options . output . path ;
25- }
26-
27- const devServer = compiler . options . devServer ;
28-
29- if ( ! devServer || ! devServer . outputPath || devServer . outputPath === '/' ) {
30- throw new Error ( 'CopyWebpackPlugin: to use webpack-dev-server, devServer.outputPath must be defined in the webpack config' ) ;
31- }
32-
33- return devServer . outputPath ;
34- } ;
35-
3614function CopyWebpackPlugin ( patterns = [ ] , options = { } ) {
3715 if ( ! _ . isArray ( patterns ) ) {
3816 throw new Error ( 'CopyWebpackPlugin: patterns must be an array' ) ;
3917 }
4018
4119 const apply = ( compiler ) => {
4220 const webpackContext = compiler . options . context ;
43- const outputPath = getOutputDir ( compiler ) ;
21+ const outputPath = compiler . options . output . path ;
4422 const fileDependencies = [ ] ;
4523 const contextDependencies = [ ] ;
4624 const webpackIgnore = options . ignore || [ ] ;
4725 const copyUnmodified = options . copyUnmodified ;
48- let writtenAssets ;
49- let lastGlobalUpdate ;
50-
51- lastGlobalUpdate = 0 ;
26+ const writtenAssetHashes = { } ;
5227
5328 compiler . plugin ( 'emit' , ( compilation , cb ) => {
54- writtenAssets = new Set ( ) ;
5529
5630 Promise . each ( patterns , ( pattern ) => {
5731 let relDest ;
@@ -102,11 +76,8 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
10276 flatten : pattern . flatten ,
10377 forceWrite,
10478 ignoreList,
105- lastGlobalUpdate,
106- relDirDest : relDest
107- } )
108- . then ( ( assets ) => {
109- writtenAssets = union ( writtenAssets , assets ) ;
79+ relDirDest : relDest ,
80+ writtenAssetHashes
11081 } ) ;
11182 }
11283
@@ -166,18 +137,12 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
166137 compilation,
167138 copyUnmodified,
168139 forceWrite,
169- lastGlobalUpdate,
170- relFileDest
171- } )
172- . then ( ( asset ) => {
173- writtenAssets . add ( asset ) ;
140+ relFileDest,
141+ writtenAssetHashes
174142 } ) ;
175143 } ) ;
176144 } ) ;
177145 } )
178- . then ( ( ) => {
179- lastGlobalUpdate = _ . now ( ) ;
180- } )
181146 . catch ( ( err ) => {
182147 compilation . errors . push ( err ) ;
183148 } )
@@ -201,35 +166,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
201166 }
202167 } ) ;
203168
204- // Write files to file system if webpack-dev-server
205-
206- if ( ! isDevServer ( compiler ) ) {
207- callback ( ) ;
208-
209- return ;
210- }
211-
212- const writeFilePromises = [ ] ;
213-
214- _ . forEach ( compilation . assets , ( asset , assetPath ) => {
215- // If this is not our asset, ignore it
216- if ( ! writtenAssets . has ( assetPath ) ) {
217- return ;
218- }
219-
220- const outputFilePath = path . join ( outputPath , assetPath ) ;
221- const absOutputPath = path . resolve ( process . cwd ( ) , outputFilePath ) ;
222-
223- writeFilePromises . push ( fs . mkdirsAsync ( path . dirname ( absOutputPath ) )
224- . then ( ( ) => {
225- return fs . writeFileAsync ( absOutputPath , asset . source ( ) ) ;
226- } ) ) ;
227- } ) ;
228-
229- Promise . all ( writeFilePromises )
230- . then ( ( ) => {
231- callback ( ) ;
232- } ) ;
169+ callback ( ) ;
233170 } ) ;
234171 } ;
235172
0 commit comments