1
- const path = require ( 'path' )
2
- const { fork } = require ( 'child_process' )
3
- const { join } = require ( 'path' )
4
- const fs = require ( 'fs-extra' )
5
- const hasha = require ( 'hasha' )
6
- const { joinUrl, getRouteParams, sizeName, emitAsset } = require ( '../utils' )
7
- const { version } = require ( '../../package.json' )
8
-
9
- module . exports = async function ( nuxt , pwa , moduleContainer ) {
1
+ import { join , resolve } from 'path'
2
+ import { fork } from 'child_process'
3
+ import fs from 'fs-extra'
4
+ import hasha from 'hasha'
5
+ import type { IconOptions } from '../types/icon'
6
+ import { joinUrl , getRouteParams , sizeName , emitAsset , PKG , PKG_DIR } from './utils'
7
+
8
+ export async function icon ( nuxt , pwa , moduleContainer ) {
10
9
const { publicPath } = getRouteParams ( nuxt . options )
11
10
12
11
// Defaults
13
- const defaults = {
12
+ const defaults : IconOptions = {
14
13
sizes : [ 64 , 120 , 144 , 152 , 192 , 384 , 512 ] ,
15
14
16
15
iosSizes : [
@@ -38,14 +37,15 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
38
37
39
38
publicPath,
40
39
40
+ // @ts -ignore
41
41
_iconHash : null ,
42
42
_assets : null ,
43
43
_manifestIcons : null ,
44
44
_iosSplash : null
45
45
}
46
46
47
47
// Merge options
48
- const options = {
48
+ const options : IconOptions = {
49
49
...defaults ,
50
50
...pwa . icon
51
51
}
@@ -56,7 +56,7 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
56
56
// Disable module if no icon specified
57
57
if ( ! options . source ) {
58
58
// eslint-disable-next-line no-console
59
- console . warn ( '[pwa] [icon] Icon not found in ' + path . resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) )
59
+ console . warn ( '[pwa] [icon] Icon not found in ' + resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) )
60
60
return
61
61
}
62
62
@@ -90,8 +90,8 @@ module.exports = async function (nuxt, pwa, moduleContainer) {
90
90
function findIcon ( nuxt , options ) {
91
91
const iconSearchPath = [
92
92
options . source ,
93
- path . resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) ,
94
- path . resolve ( nuxt . options . srcDir , nuxt . options . dir . assets , options . fileName )
93
+ resolve ( nuxt . options . srcDir , nuxt . options . dir . static , options . fileName ) ,
94
+ resolve ( nuxt . options . srcDir , nuxt . options . dir . assets , options . fileName )
95
95
] . filter ( p => p )
96
96
97
97
for ( const source of iconSearchPath ) {
@@ -109,8 +109,8 @@ function addPlugin (_nuxt, options, moduleContainer) {
109
109
110
110
if ( options . plugin ) {
111
111
moduleContainer . addPlugin ( {
112
- src : path . resolve ( __dirname , './ plugin.js' ) ,
113
- fileName : 'pwa/icons .js' ,
112
+ src : resolve ( PKG_DIR , 'templates/icon. plugin.js' ) ,
113
+ fileName : 'pwa/icon.plugin .js' ,
114
114
options : {
115
115
pluginName : options . pluginName ,
116
116
icons
@@ -173,14 +173,14 @@ function emitAssets (nuxt, options) {
173
173
const resizePromise = resizeIcons ( nuxt , options )
174
174
175
175
for ( const { name, target } of options . _assets ) {
176
- const srcFileName = path . join ( options . cacheDir , `${ name } .png` )
176
+ const srcFileName = join ( options . cacheDir , `${ name } .png` )
177
177
emitAsset ( nuxt , target , resizePromise . then ( ( ) => fs . readFile ( srcFileName ) ) )
178
178
}
179
179
}
180
180
181
181
async function resizeIcons ( _nuxt , options ) {
182
182
const resizeOpts = JSON . stringify ( {
183
- version,
183
+ version : PKG . version ,
184
184
input : options . source ,
185
185
distDir : options . cacheDir ,
186
186
sizes : [
@@ -189,18 +189,19 @@ async function resizeIcons (_nuxt, options) {
189
189
]
190
190
} )
191
191
192
- const integrityFile = path . join ( options . cacheDir , '.' + hasha ( resizeOpts ) . substr ( 0 , 8 ) )
192
+ const integrityFile = join ( options . cacheDir , '.' + hasha ( resizeOpts ) . substr ( 0 , 8 ) )
193
193
194
194
if ( fs . existsSync ( integrityFile ) ) {
195
195
return
196
196
}
197
197
await fs . remove ( options . cacheDir )
198
198
await fs . mkdirp ( options . cacheDir )
199
199
200
- await new Promise ( ( resolve , reject ) => {
201
- const child = fork ( require . resolve ( './resize' ) , [ resizeOpts ] , { execArgv : [ ] } )
200
+ // eslint-disable-next-line promise/param-names
201
+ await new Promise ( ( _resolve , _reject ) => {
202
+ const child = fork ( resolve ( PKG_DIR , 'lib/resize.js' ) , [ resizeOpts ] , { execArgv : [ ] } )
202
203
child . on ( 'exit' , ( code ) => {
203
- return code ? reject ( code ) : resolve ( )
204
+ return code ? _reject ( code ) : _resolve ( )
204
205
} )
205
206
} )
206
207
0 commit comments