Navigation Menu

Skip to content

Commit

Permalink
feat(icon): expose options.cacheDir
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 14, 2020
1 parent 014a525 commit f8dbf1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/content/en/icon.md
Expand Up @@ -59,3 +59,9 @@ purpose: 'maskable'
```

More detail of "purpose": [https://w3c.github.io/manifest/#purpose-member](https://w3c.github.io/manifest/#purpose-member)


**cacheDir**
- Default: `{rootDir}/node_modules/.cache/icon`

Cache dir for generated icons
18 changes: 7 additions & 11 deletions lib/icon/module.js
@@ -1,5 +1,6 @@
const path = require('path')
const { fork } = require('child_process')
const { join } = require('path')
const fs = require('fs-extra')
const hasha = require('hasha')
const { joinUrl, getRouteParams, sizeName } = require('../utils')
Expand Down Expand Up @@ -36,6 +37,7 @@ async function run (pwa, _emitAssets) {
fileName: 'icon.png',
source: null,
purpose: ['any', 'maskable'],
cacheDir: join(this.nuxt.options.rootDir, 'node_modules/.cache/pwa/icon'),

targetDir: 'icons',

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

_iconHash: null,
_cacheDir: null,
_assets: null,
_manifestIcons: null,
_iosSplash: null
Expand Down Expand Up @@ -132,11 +133,6 @@ async function generateIcons (options) {
options.iconHash = await hasha.fromFile(options.source).then(h => h.substring(0, 6))
}

// Resize cache dir
if (!options.cacheDir) {
options.cacheDir = path.join(__dirname, '.cache', options.iconHash)
}

// Icons to be emited by webpack
options._assets = []

Expand Down Expand Up @@ -192,7 +188,7 @@ function emitAssets (options) {
compiler.hooks.emit.tapPromise('nuxt-pwa-icon', async (compilation) => {
await resizePromise
await Promise.all(options._assets.map(async ({ name, target }) => {
const srcFileName = path.join(options._cacheDir, `${name}.png`)
const srcFileName = path.join(options.cacheDir, `${name}.png`)
const src = await fs.readFile(srcFileName)
compilation.assets[target] = { source: () => src, size: () => src.length }
}))
Expand All @@ -207,20 +203,20 @@ async function resizeIcons (options) {
const resizeOpts = JSON.stringify({
version,
input: options.source,
distDir: options._cacheDir,
distDir: options.cacheDir,
sizes: [
...options.sizes,
...options.iosSizes
]
})

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

if (await fs.exists(integrityFile)) {
return
}
await fs.remove(options._cacheDir)
await fs.mkdirp(options._cacheDir)
await fs.remove(options.cacheDir)
await fs.mkdirp(options.cacheDir)

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

0 comments on commit f8dbf1d

Please sign in to comment.