@@ -3,11 +3,10 @@ const msgpack = require('msgpack-lite');
33const findCacheDir = require ( 'find-cache-dir' ) ;
44const { AsyncSeriesWaterfallHook } = require ( 'tapable' ) ;
55const SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
6- const { getAssetPath, trigger } = require ( './compat' ) ;
76
87const wwp = 'webapp-webpack-plugin' ;
98
10- module . exports . run = ( { prefix, favicons : options , logo, cache, publicPath : publicPathOption , outputPath } , context , compilation ) => {
9+ module . exports . run = async ( { prefix, favicons : options , logo, cache, publicPath : publicPathOption , outputPath } , context , compilation ) => {
1110 // The entry file is just an empty helper
1211 const filename = '[hash]' ;
1312 const publicPath = publicPathOption || compilation . outputOptions . publicPath || '/' ;
@@ -38,32 +37,30 @@ module.exports.run = ({ prefix, favicons: options, logo, cache, publicPath: publ
3837 }
3938
4039 // Compile and return a promise
41- return new Promise ( ( resolve , reject ) => {
42- compiler . runAsChild ( ( err , [ chunk ] = [ ] , { hash, errors = [ ] , assets = { } } = { } ) => {
40+ const { chunk , hash , assets } = await new Promise ( ( resolve , reject ) => {
41+ return compiler . runAsChild ( ( err , [ chunk ] = [ ] , { hash, errors = [ ] , assets = { } } = { } ) => {
4342 if ( err || errors . length ) {
4443 return reject ( err || errors [ 0 ] . error ) ;
4544 }
4645
47- // Replace [ hash] placeholders in filename
48- const output = getAssetPath ( compilation , filename , { hash , chunk } ) ;
49- const result = msgpack . decode ( Buffer . from ( eval ( assets [ output ] . source ( ) ) , 'base64' ) ) ;
46+ return resolve ( { chunk , hash, assets } ) ;
47+ } ) ;
48+ } ) ;
5049
51- delete compilation . assets [ output ] ;
50+ // Replace [hash] placeholders in filename
51+ const output = compilation . mainTemplate . getAssetPath ( filename , { hash, chunk } ) ;
52+ const result = msgpack . decode ( Buffer . from ( eval ( assets [ output ] . source ( ) ) , 'base64' ) ) ;
5253
53- trigger ( compilation , 'webapp-webpack-plugin-before-emit' , result , ( error , { tags = [ ] , assets = [ ] } = { } ) => {
54- if ( error ) {
55- return reject ( error ) ;
56- }
54+ delete compilation . assets [ output ] ;
5755
58- for ( const { name, contents } of assets ) {
59- compilation . assets [ name ] = {
60- source : ( ) => contents ,
61- size : ( ) => contents . length ,
62- } ;
63- }
56+ const { tags, assets : files } = await compilation . hooks . webappWebpackPluginBeforeEmit . promise ( result ) ;
6457
65- return resolve ( tags ) ;
66- } ) ;
67- } ) ;
68- } ) ;
58+ for ( const { name, contents } of files ) {
59+ compilation . assets [ name ] = {
60+ source : ( ) => contents ,
61+ size : ( ) => contents . length ,
62+ } ;
63+ }
64+
65+ return tags ;
6966} ;
0 commit comments