@@ -2,7 +2,7 @@ var path = require('path');
22var fse = require ( 'fs-extra' ) ;
33var _ = require ( 'lodash' ) ;
44
5- var manifestMap = { } ;
5+ const emitCountMap = new Map ( ) ;
66
77function ManifestPlugin ( opts ) {
88 this . opts = _ . assign ( {
@@ -35,6 +35,10 @@ ManifestPlugin.prototype.apply = function(compiler) {
3535 var seed = this . opts . seed || { } ;
3636 var moduleAssets = { } ;
3737
38+ var outputFolder = compiler . options . output . path ;
39+ var outputFile = path . resolve ( compiler . options . output . path , this . opts . fileName ) ;
40+ var outputName = path . relative ( outputFolder , outputFile ) ;
41+
3842 var moduleAsset = function ( module , file ) {
3943 moduleAssets [ file ] = path . join (
4044 path . dirname ( file ) ,
@@ -43,6 +47,9 @@ ManifestPlugin.prototype.apply = function(compiler) {
4347 } ;
4448
4549 var emit = function ( compilation , compileCallback ) {
50+ const emitCount = emitCountMap . get ( outputName ) - 1
51+ emitCountMap . set ( outputName , emitCount ) ;
52+
4653 var publicPath = compilation . options . output . publicPath ;
4754 var stats = compilation . getStats ( ) . toJson ( ) ;
4855
@@ -106,7 +113,7 @@ ManifestPlugin.prototype.apply = function(compiler) {
106113 // Don't add hot updates to manifest
107114 var isUpdateChunk = file . path . indexOf ( 'hot-update' ) >= 0 ;
108115 // Don't add manifest from another instance
109- var isManifest = manifestMap [ file . name ] ;
116+ var isManifest = emitCountMap . get ( file . name ) !== undefined ;
110117
111118 return ! isUpdateChunk && ! isManifest ;
112119 } ) ;
@@ -158,74 +165,59 @@ ManifestPlugin.prototype.apply = function(compiler) {
158165 } , seed ) ;
159166 }
160167
161- var output = this . opts . serialize ( manifest ) ;
162-
163- var outputFolder = compilation . options . output . path ;
164- var outputFile = path . resolve ( compilation . options . output . path , this . opts . fileName ) ;
165- var outputName = path . relative ( outputFolder , outputFile ) ;
168+ const isLastEmit = emitCount === 0
169+ if ( isLastEmit ) {
170+ var output = this . opts . serialize ( manifest ) ;
166171
167- compilation . assets [ outputName ] = {
168- source : function ( ) {
169- return output ;
170- } ,
171- size : function ( ) {
172- return output . length ;
173- }
174- } ;
175-
176- if ( this . opts . writeToFileEmit ) {
177- fse . outputFileSync ( outputFile , output ) ;
178- }
179-
180- if ( ! manifestMap [ outputName ] ) {
181- manifestMap [ outputName ] = {
182- running : false ,
183- queue : [ ]
172+ compilation . assets [ outputName ] = {
173+ source : function ( ) {
174+ return output ;
175+ } ,
176+ size : function ( ) {
177+ return output . length ;
178+ }
184179 } ;
185- }
186180
187- function unqueueNext ( ) {
188- if ( manifestMap [ outputName ] . queue . length > 0 ) {
189- manifestMap [ outputName ] . running = true ;
190- manifestMap [ outputName ] . queue . shift ( ) ( ) ;
191- } else {
192- manifestMap [ outputName ] . running = false ;
181+ if ( this . opts . writeToFileEmit ) {
182+ fse . outputFileSync ( outputFile , output ) ;
193183 }
194184 }
195185
186+ if ( compiler . hooks ) {
187+ compiler . hooks . webpackManifestPluginAfterEmit . call ( manifest ) ;
188+ } else {
189+ compilation . applyPluginsAsync ( 'webpack-manifest-plugin-after-emit' , manifest , compileCallback ) ;
190+ }
191+ } . bind ( this ) ;
196192
197- manifestMap [ outputName ] . queue . push ( function ( ) {
198- if ( compiler . hooks ) {
199- compiler . hooks . afterEmit . tap ( 'ManifestPlugin' , function ( compilation ) {
200- // TODO: when we deprecate webpack < 3, we can remove the queue logic
201- unqueueNext ( )
202- } ) ;
203- } else {
204- compiler . plugin ( 'after-emit' , function ( compilation , cb ) {
205- unqueueNext ( ) ;
206- cb ( ) ;
207- } ) ;
208-
209- compilation . applyPluginsAsync ( 'webpack-manifest-plugin-after-emit' , manifest , compileCallback ) ;
210- }
211- } )
193+ function beforeRun ( compiler , callback ) {
194+ let emitCount = emitCountMap . get ( outputName ) || 0 ;
195+ emitCountMap . set ( outputName , emitCount + 1 ) ;
212196
213- if ( ! manifestMap [ outputName ] . running ) {
214- manifestMap [ outputName ] . running = true ;
215- manifestMap [ outputName ] . queue . shift ( ) ( ) ;
197+ if ( callback ) {
198+ callback ( ) ;
216199 }
217- } . bind ( this ) ;
200+ }
218201
219202 if ( compiler . hooks ) {
203+ const SyncWaterfallHook = require ( 'tapable' ) . SyncWaterfallHook ;
204+ compiler . hooks . webpackManifestPluginAfterEmit = new SyncWaterfallHook ( [ 'manifest' ] ) ;
205+
220206 compiler . hooks . compilation . tap ( 'ManifestPlugin' , function ( compilation ) {
221207 compilation . hooks . moduleAsset . tap ( 'ManifestPlugin' , moduleAsset ) ;
222208 } ) ;
223209 compiler . hooks . emit . tap ( 'ManifestPlugin' , emit ) ;
210+
211+ compiler . hooks . run . tap ( 'ManifestPlugin' , beforeRun ) ;
212+ compiler . hooks . watchRun . tap ( 'ManifestPlugin' , beforeRun ) ;
224213 } else {
225214 compiler . plugin ( 'compilation' , function ( compilation ) {
226215 compilation . plugin ( 'module-asset' , moduleAsset ) ;
227216 } ) ;
228217 compiler . plugin ( 'emit' , emit ) ;
218+
219+ compiler . plugin ( 'before-run' , beforeRun ) ;
220+ compiler . plugin ( 'watch-run' , beforeRun ) ;
229221 }
230222} ;
231223
0 commit comments