Skip to content

Commit

Permalink
refactor(icon): rename iconSrc, iconFileName, iconPlugin and iconProp…
Browse files Browse the repository at this point in the history
…erty to source, fileName, plugin and pluginName
  • Loading branch information
pi0 committed Aug 14, 2020
1 parent ab20e61 commit faba975
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
8 changes: 4 additions & 4 deletions docs/content/en/icon.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pwa: {

## options

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

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

**sizes**
Expand All @@ -34,15 +34,15 @@ Array of sizes to be generated (Square).
**targetDir**
- Default: `icons`

**iconPlugin**
**plugin**
- Default: true

Make icons accessible through `ctx` or Vue instances.

Example: `ctx.$icon(512)` will return the url for the icon with the size of `512px`.
Will return an empty string when no icon in the given size is available (eg. when the size is not in `sizes` array).

**iconProperty**
**pluginName**
- Default: '$icon'

Name of property for accessible icons.
Expand Down
47 changes: 26 additions & 21 deletions lib/icon/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ async function run (pwa, _emitAssets) {

// Defaults
const defaults = {
iconFileName: 'icon.png',
sizes: [64, 120, 144, 152, 192, 384, 512],

iosSizes: [
[1536, 2048, 'ipad'], // Ipad
[1536, 2048, 'ipadpro9'], // Ipad Pro 9.7"
Expand All @@ -32,12 +32,17 @@ async function run (pwa, _emitAssets) {
[828, 1792, 'iphonexr'], // Iphone XR
[1242, 2688, 'iphonexsmax'] // Iphone XS Max
],

fileName: 'icon.png',
source: null,
purpose: ['any', 'maskable'],

targetDir: 'icons',
iconPlugin: true,
iconProperty: '$icon',

plugin: true,
pluginName: '$icon',

publicPath,
iconSrc: null,
purpose: ['any', 'maskable'],

_iconHash: null,
_cacheDir: null,
Expand All @@ -52,11 +57,11 @@ async function run (pwa, _emitAssets) {
...pwa.icon
}

// Find iconSrc
options.iconSrc = await findIcon.call(this, options)
// Find source
options.source = await findIcon.call(this, options)

// Disable module if no icon specified
if (!options.iconSrc) {
if (!options.source) {
return
}

Expand All @@ -79,7 +84,7 @@ async function run (pwa, _emitAssets) {
addManifest.call(this, options, pwa)

// Add plugin
if (options.iconPlugin) {
if (options.plugin) {
addPlugin.call(this, options)
}

Expand All @@ -91,14 +96,14 @@ async function run (pwa, _emitAssets) {

async function findIcon (options) {
const iconSearchPath = [
options.iconSrc,
path.resolve(this.options.srcDir, this.options.dir.static, options.iconFileName),
path.resolve(this.options.srcDir, this.options.dir.assets, options.iconFileName)
options.source,
path.resolve(this.options.srcDir, this.options.dir.static, options.fileName),
path.resolve(this.options.srcDir, this.options.dir.assets, options.fileName)
].filter(p => p)

for (const iconSrc of iconSearchPath) {
if (await fs.exists(iconSrc)) {
return iconSrc
for (const source of iconSearchPath) {
if (await fs.exists(source)) {
return source
}
}
}
Expand All @@ -109,12 +114,12 @@ function addPlugin (options) {
icons[asset.name] = joinUrl(options.publicPath, asset.target)
}

if (options.iconPlugin) {
if (options.plugin) {
this.addPlugin({
src: path.resolve(__dirname, './plugin.js'),
fileName: 'nuxt-icons.js',
options: {
iconProperty: options.iconProperty,
pluginName: options.pluginName,
icons
}
})
Expand All @@ -124,12 +129,12 @@ function addPlugin (options) {
async function generateIcons (options) {
// Get hash of source image
if (!options.iconHash) {
options.iconHash = await hasha.fromFile(options.iconSrc).then(h => h.substring(0, 6))
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)
if (!options.cacheDir) {
options.cacheDir = path.join(__dirname, '.cache', options.iconHash)
}

// Icons to be emited by webpack
Expand Down Expand Up @@ -201,7 +206,7 @@ function emitAssets (options) {
async function resizeIcons (options) {
const resizeOpts = JSON.stringify({
version,
input: options.iconSrc,
input: options.source,
distDir: options._cacheDir,
sizes: [
...options.sizes,
Expand Down
2 changes: 1 addition & 1 deletion lib/icon/plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default async function (ctx, inject) {
const icons = <%= JSON.stringify(options.icons) %>
const getIcon = size => icons[size + 'x' + size] || ''
inject('<%= options.iconProperty.replace('$', '') %>', getIcon)
inject('<%= options.pluginName.replace('$', '') %>', getIcon)
}

0 comments on commit faba975

Please sign in to comment.