Skip to content

Commit

Permalink
feat(icon): new options (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and pi0 committed Jan 17, 2019
1 parent 17e4e0f commit 12e6576
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
8 changes: 7 additions & 1 deletion docs/modules/icon.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ icon: {
### options

**iconSrc**
- Default: `[srcDir]/static/icon.png`
- Default: `[srcDir]/[staticDir]/icon.png`

**iconFileName**
- Default: `icon.png`

**sizes**
- Default: `[16, 120, 144, 152, 192, 384, 512]`

**targetDir**
- Default: `icons`

Array of sizes to be generated (Square).

**accessibleIcons**
Expand Down
16 changes: 10 additions & 6 deletions packages/icon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ module.exports = function nuxtIcon (options) {

function generateIcons (moduleOptions) {
// Combine sources
const options = Object.assign({}, this.options.icon, moduleOptions)

const iconSrc = options.iconSrc || path.resolve(this.options.srcDir, 'static', 'icon.png')
const sizes = options.sizes || [64, 120, 144, 152, 192, 384, 512]
const defaults = {
iconSrc: null,
iconFileName: 'icon.png',
sizes: [64, 120, 144, 152, 192, 384, 512],
targetDir: 'icons'
}
const options = Object.assign({}, defaults, this.options.icon, moduleOptions)
const iconSrc = options.iconSrc || path.resolve(this.options.srcDir, this.options.dir.static, options.iconFileName)

// routerBase and publicPath
const routerBase = this.options.router.base
Expand All @@ -46,12 +50,12 @@ function generateIcons (moduleOptions) {
return Jimp.read(iconSrc).then(srcIcon => {
// get base64 phash of source image
const hash = srcIcon.hash()
return Promise.all(sizes.map(size => new Promise((resolve, reject) => {
return Promise.all(options.sizes.map(size => new Promise((resolve, reject) => {
srcIcon.clone().contain(size, size).getBuffer(Jimp.MIME_PNG, (err, buff) => {
if (err) {
return reject(err)
}
const fileName = `icons/icon_${size}.${hash}.png`
const fileName = `${options.targetDir}/icon_${size}.${hash}.png`
resolve({ size, buff, fileName })
})
}))).then(icons => {
Expand Down
3 changes: 2 additions & 1 deletion test/__snapshots__/pwa.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Array [
"fixture/.nuxt/components/no-ssr.js",
"fixture/.nuxt/components/nuxt-child.js",
"fixture/.nuxt/components/nuxt-error.vue",
"fixture/.nuxt/components/nuxt-link.js",
"fixture/.nuxt/components/nuxt-link.client.js",
"fixture/.nuxt/components/nuxt-link.server.js",
"fixture/.nuxt/components/nuxt-loading.vue",
"fixture/.nuxt/components/nuxt.js",
"fixture/.nuxt/dist",
Expand Down

0 comments on commit 12e6576

Please sign in to comment.