@@ -3,15 +3,11 @@ const { fork } = require('child_process')
33const { join } = require ( 'path' )
44const fs = require ( 'fs-extra' )
55const hasha = require ( 'hasha' )
6- const { joinUrl, getRouteParams, sizeName } = require ( '../utils' )
6+ const { joinUrl, getRouteParams, sizeName, emitAsset } = require ( '../utils' )
77const { version } = require ( '../../package.json' )
88
9- module . exports = function ( pwa ) {
10- this . nuxt . hook ( 'build:before' , ( ) => run . call ( this , pwa ) )
11- }
12-
13- async function run ( pwa ) {
14- const { publicPath } = getRouteParams ( this . options )
9+ module . exports = async function ( nuxt , pwa , moduleContainer ) {
10+ const { publicPath } = getRouteParams ( nuxt . options )
1511
1612 // Defaults
1713 const defaults = {
@@ -33,7 +29,7 @@ async function run (pwa) {
3329 fileName : 'icon.png' ,
3430 source : null ,
3531 purpose : [ 'any' , 'maskable' ] ,
36- cacheDir : join ( this . nuxt . options . rootDir , 'node_modules/.cache/pwa/icon' ) ,
32+ cacheDir : join ( nuxt . options . rootDir , 'node_modules/.cache/pwa/icon' ) ,
3733
3834 targetDir : 'icons' ,
3935
@@ -55,12 +51,12 @@ async function run (pwa) {
5551 }
5652
5753 // Find source
58- options . source = await findIcon . call ( this , options )
54+ options . source = await findIcon ( nuxt , options )
5955
6056 // Disable module if no icon specified
6157 if ( ! options . source ) {
6258 // eslint-disable-next-line no-console
63- console . warn ( '[pwa] [icon] Icon not found in ' + path . resolve ( this . options . srcDir , this . options . dir . static , options . fileName ) )
59+ console . warn ( '[pwa] [icon] Icon not found in ' + path . resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) )
6460 return
6561 }
6662
@@ -77,42 +73,42 @@ async function run (pwa) {
7773 }
7874
7975 // Generate icons
80- await generateIcons . call ( this , options )
76+ await generateIcons ( nuxt , options )
8177
8278 // Add manifest
83- addManifest . call ( this , options , pwa )
79+ addManifest ( nuxt , options , pwa )
8480
8581 // Add plugin
8682 if ( options . plugin ) {
87- addPlugin . call ( this , options )
83+ addPlugin ( nuxt , options , moduleContainer )
8884 }
8985
9086 // Emit assets in background
91- emitAssets . call ( this , options )
87+ emitAssets ( nuxt , options )
9288}
9389
94- async function findIcon ( options ) {
90+ function findIcon ( nuxt , options ) {
9591 const iconSearchPath = [
9692 options . source ,
97- path . resolve ( this . options . srcDir , this . options . dir . static , options . fileName ) ,
98- path . resolve ( this . options . srcDir , this . options . dir . assets , options . fileName )
93+ path . resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) ,
94+ path . resolve ( nuxt . options . srcDir , nuxt . options . dir . assets , options . fileName )
9995 ] . filter ( p => p )
10096
10197 for ( const source of iconSearchPath ) {
102- if ( await fs . exists ( source ) ) {
98+ if ( fs . existsSync ( source ) ) {
10399 return source
104100 }
105101 }
106102}
107103
108- function addPlugin ( options ) {
104+ function addPlugin ( _nuxt , options , moduleContainer ) {
109105 const icons = { }
110106 for ( const asset of options . _assets ) {
111107 icons [ asset . name ] = joinUrl ( options . publicPath , asset . target )
112108 }
113109
114110 if ( options . plugin ) {
115- this . addPlugin ( {
111+ moduleContainer . addPlugin ( {
116112 src : path . resolve ( __dirname , './plugin.js' ) ,
117113 fileName : 'pwa/icons.js' ,
118114 options : {
@@ -123,7 +119,7 @@ function addPlugin (options) {
123119 }
124120}
125121
126- async function generateIcons ( options ) {
122+ async function generateIcons ( _nuxt , options ) {
127123 // Get hash of source image
128124 if ( ! options . iconHash ) {
129125 options . iconHash = await hasha . fromFile ( options . source ) . then ( h => h . substring ( 0 , 6 ) )
@@ -157,7 +153,7 @@ async function generateIcons (options) {
157153 }
158154}
159155
160- function addManifest ( options , pwa ) {
156+ function addManifest ( _nuxt , options , pwa ) {
161157 if ( ! pwa . manifest ) {
162158 pwa . manifest = { }
163159 }
@@ -172,30 +168,17 @@ function addManifest (options, pwa) {
172168 }
173169}
174170
175- function emitAssets ( options ) {
171+ function emitAssets ( nuxt , options ) {
176172 // Start resize task in background
177- const resizePromise = resizeIcons . call ( this , options )
178-
179- // Register webpack plugin to emit icons
180- this . extendBuild ( ( config , { isClient } ) => {
181- if ( isClient ) {
182- config . plugins . push ( {
183- apply ( compiler ) {
184- compiler . hooks . emit . tapPromise ( 'nuxt-pwa-icon' , async ( compilation ) => {
185- await resizePromise
186- await Promise . all ( options . _assets . map ( async ( { name, target } ) => {
187- const srcFileName = path . join ( options . cacheDir , `${ name } .png` )
188- const src = await fs . readFile ( srcFileName )
189- compilation . assets [ target ] = { source : ( ) => src , size : ( ) => src . length }
190- } ) )
191- } )
192- }
193- } )
194- }
195- } )
173+ const resizePromise = resizeIcons ( nuxt , options )
174+
175+ for ( const { name, target } of options . _assets ) {
176+ const srcFileName = path . join ( options . cacheDir , `${ name } .png` )
177+ emitAsset ( nuxt , target , resizePromise . then ( ( ) => fs . readFile ( srcFileName ) ) )
178+ }
196179}
197180
198- async function resizeIcons ( options ) {
181+ async function resizeIcons ( _nuxt , options ) {
199182 const resizeOpts = JSON . stringify ( {
200183 version,
201184 input : options . source ,
@@ -208,7 +191,7 @@ async function resizeIcons (options) {
208191
209192 const integrityFile = path . join ( options . cacheDir , '.' + hasha ( resizeOpts ) . substr ( 0 , 8 ) )
210193
211- if ( await fs . exists ( integrityFile ) ) {
194+ if ( fs . existsSync ( integrityFile ) ) {
212195 return
213196 }
214197 await fs . remove ( options . cacheDir )
0 commit comments