Skip to content

Commit f8dbf1d

Browse files
committed
feat(icon): expose options.cacheDir
1 parent 014a525 commit f8dbf1d

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

docs/content/en/icon.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ purpose: 'maskable'
5959
```
6060

6161
More detail of "purpose": [https://w3c.github.io/manifest/#purpose-member](https://w3c.github.io/manifest/#purpose-member)
62+
63+
64+
**cacheDir**
65+
- Default: `{rootDir}/node_modules/.cache/icon`
66+
67+
Cache dir for generated icons

lib/icon/module.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const { fork } = require('child_process')
3+
const { join } = require('path')
34
const fs = require('fs-extra')
45
const hasha = require('hasha')
56
const { joinUrl, getRouteParams, sizeName } = require('../utils')
@@ -36,6 +37,7 @@ async function run (pwa, _emitAssets) {
3637
fileName: 'icon.png',
3738
source: null,
3839
purpose: ['any', 'maskable'],
40+
cacheDir: join(this.nuxt.options.rootDir, 'node_modules/.cache/pwa/icon'),
3941

4042
targetDir: 'icons',
4143

@@ -45,7 +47,6 @@ async function run (pwa, _emitAssets) {
4547
publicPath,
4648

4749
_iconHash: null,
48-
_cacheDir: null,
4950
_assets: null,
5051
_manifestIcons: null,
5152
_iosSplash: null
@@ -132,11 +133,6 @@ async function generateIcons (options) {
132133
options.iconHash = await hasha.fromFile(options.source).then(h => h.substring(0, 6))
133134
}
134135

135-
// Resize cache dir
136-
if (!options.cacheDir) {
137-
options.cacheDir = path.join(__dirname, '.cache', options.iconHash)
138-
}
139-
140136
// Icons to be emited by webpack
141137
options._assets = []
142138

@@ -192,7 +188,7 @@ function emitAssets (options) {
192188
compiler.hooks.emit.tapPromise('nuxt-pwa-icon', async (compilation) => {
193189
await resizePromise
194190
await Promise.all(options._assets.map(async ({ name, target }) => {
195-
const srcFileName = path.join(options._cacheDir, `${name}.png`)
191+
const srcFileName = path.join(options.cacheDir, `${name}.png`)
196192
const src = await fs.readFile(srcFileName)
197193
compilation.assets[target] = { source: () => src, size: () => src.length }
198194
}))
@@ -207,20 +203,20 @@ async function resizeIcons (options) {
207203
const resizeOpts = JSON.stringify({
208204
version,
209205
input: options.source,
210-
distDir: options._cacheDir,
206+
distDir: options.cacheDir,
211207
sizes: [
212208
...options.sizes,
213209
...options.iosSizes
214210
]
215211
})
216212

217-
const integrityFile = path.join(options._cacheDir, '.' + hasha(resizeOpts).substr(0, 8))
213+
const integrityFile = path.join(options.cacheDir, '.' + hasha(resizeOpts).substr(0, 8))
218214

219215
if (await fs.exists(integrityFile)) {
220216
return
221217
}
222-
await fs.remove(options._cacheDir)
223-
await fs.mkdirp(options._cacheDir)
218+
await fs.remove(options.cacheDir)
219+
await fs.mkdirp(options.cacheDir)
224220

225221
await new Promise((resolve, reject) => {
226222
const child = fork(require.resolve('./resize'), [resizeOpts])

0 commit comments

Comments
 (0)