@@ -17,9 +17,8 @@ function getAssetPath(compilation, name) {
1717 return path . join ( compilation . getPath ( compilation . compiler . outputPath ) , name . split ( '?' ) [ 0 ] ) ;
1818}
1919
20- function getSource ( compilation , name ) {
21- const path = getAssetPath ( compilation , name ) ;
22- return fs . readFileSync ( path , { encoding : 'utf-8' } ) ;
20+ function getSource ( asset ) {
21+ return asset . source . source ( ) ;
2322}
2423
2524/**
@@ -56,6 +55,7 @@ class BundleTrackerPlugin {
5655 } ;
5756 this . name = 'BundleTrackerPlugin' ;
5857
58+ this . assets = { } ;
5959 this . outputChunkDir = '' ;
6060 this . outputTrackerFile = '' ;
6161 this . outputTrackerDir = '' ;
@@ -103,11 +103,8 @@ class BundleTrackerPlugin {
103103 }
104104 /**
105105 * Write bundle tracker stats file
106- *
107- * @param {Compiler } _compiler
108- * @param {Partial<Contents> } contents
109106 */
110- _writeOutput ( _compiler , contents ) {
107+ _writeOutput ( contents ) {
111108 Object . assign ( this . contents , contents , {
112109 assets : mergeObjectsAndSortKeys ( this . contents . assets , contents . assets ) ,
113110 chunks : mergeObjectsAndSortKeys ( this . contents . chunks , contents . chunks ) ,
@@ -140,44 +137,25 @@ class BundleTrackerPlugin {
140137 }
141138 /**
142139 * Handle compile hook
143- * @param {Compiler } compiler
144140 */
145- _handleCompile ( compiler ) {
146- this . _writeOutput ( compiler , { status : 'compile' } ) ;
141+ _handleCompile ( ) {
142+ this . _writeOutput ( { status : 'compile' } ) ;
147143 }
144+
148145 /**
149- * Handle compile hook
150- * @param {Compiler } compiler
151- * @param {Stats } stats
146+ * Hook to handle the assets when they are ready to be emitted
147+ * @param {Compilation } compilation
152148 */
153- _handleDone ( compiler , stats ) {
154- if ( stats . hasErrors ( ) ) {
155- const findError = compilation => {
156- if ( compilation . errors . length > 0 ) {
157- return compilation . errors [ 0 ] ;
158- }
159- return compilation . children . find ( child => findError ( child ) ) ;
160- } ;
161- const error = findError ( stats . compilation ) ;
162- this . _writeOutput ( compiler , {
163- status : 'error' ,
164- error : error ?. name ?? 'unknown-error' ,
165- message : stripAnsi ( error [ 'message' ] ) ,
166- } ) ;
167-
168- return ;
169- }
170-
171- /** @type {Contents } */
172- const output = { status : 'done' , assets : { } , chunks : { } } ;
173- Object . entries ( stats . compilation . assets ) . map ( ( [ assetName , _ ] ) => {
149+ _handleEmit ( compilation ) {
150+ Object . keys ( compilation . assets ) . forEach ( assetName => {
174151 const fileInfo = {
175152 name : assetName ,
176- path : getAssetPath ( stats . compilation , assetName ) ,
153+ path : getAssetPath ( compilation , assetName ) ,
177154 } ;
178155
179156 if ( this . options . integrity === true ) {
180- fileInfo . integrity = this . _computeIntegrity ( getSource ( stats . compilation , assetName ) ) ;
157+ const asset = compilation . getAsset ( assetName ) ;
158+ fileInfo . integrity = this . _computeIntegrity ( getSource ( asset ) ) ;
181159 }
182160
183161 if ( this . options . publicPath ) {
@@ -193,35 +171,59 @@ class BundleTrackerPlugin {
193171 }
194172
195173 // @ts -ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
196- if ( stats . compilation . assetsInfo ) {
174+ if ( compilation . assetsInfo ) {
197175 // @ts -ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
198- fileInfo . sourceFilename = stats . compilation . assetsInfo . get ( assetName ) . sourceFilename ;
176+ fileInfo . sourceFilename = compilation . assetsInfo . get ( assetName ) . sourceFilename ;
199177 }
200178
201- output . assets [ assetName ] = fileInfo ;
179+ this . assets [ assetName ] = fileInfo ;
202180 } ) ;
181+ }
182+
183+ /**
184+ * Handle done hook and write output file
185+ * @param {Stats } stats
186+ */
187+ _handleDone ( stats ) {
188+ if ( stats . hasErrors ( ) ) {
189+ const findError = compilation => {
190+ if ( compilation . errors . length > 0 ) {
191+ return compilation . errors [ 0 ] ;
192+ }
193+ return compilation . children . find ( child => findError ( child ) ) ;
194+ } ;
195+ const error = findError ( stats . compilation ) ;
196+ this . _writeOutput ( {
197+ status : 'error' ,
198+ error : error ?. name ?? 'unknown-error' ,
199+ message : stripAnsi ( error [ 'message' ] ) ,
200+ } ) ;
201+ return ;
202+ }
203+
204+ const chunks = { } ;
203205 stats . compilation . chunkGroups . forEach ( chunkGroup => {
204206 if ( ! chunkGroup . isInitial ( ) ) return ;
205-
206- output . chunks [ chunkGroup . name ] = chunkGroup . getFiles ( ) ;
207+ chunks [ chunkGroup . name ] = chunkGroup . getFiles ( ) ;
207208 } ) ;
208209
210+ const output = { status : 'done' , chunks, assets : this . assets } ;
209211 if ( this . options . logTime === true ) {
210212 output . startTime = stats . startTime ;
211213 output . endTime = stats . endTime ;
212214 }
213-
214- this . _writeOutput ( compiler , output ) ;
215+ this . _writeOutput ( output ) ;
215216 }
217+
216218 /**
217219 * Method called by webpack to apply plugin hook
218220 * @param {Compiler } compiler
219221 */
220222 apply ( compiler ) {
221223 this . _setParamsFromCompiler ( compiler ) ;
222-
223- compiler . hooks . compile . tap ( this . name , this . _handleCompile . bind ( this , compiler ) ) ;
224- compiler . hooks . done . tap ( this . name , this . _handleDone . bind ( this , compiler ) ) ;
224+ compiler . hooks . compile . tap ( this . name , this . _handleCompile . bind ( this ) ) ;
225+ compiler . hooks . emit . tap ( this . name , this . _handleEmit . bind ( this ) ) ;
226+ compiler . hooks . done . tap ( this . name , this . _handleDone . bind ( this ) ) ;
225227 }
226228}
227229
0 commit comments