Skip to content

Commit 92bccd3

Browse files
TheAlexLichtergalvez
authored andcommittedSep 1, 2018
feat(icon): make icons accessible (#51)
* Make icons accessible * Update interface and add tests * Update docs
1 parent 057c46d commit 92bccd3

File tree

7 files changed

+74
-9
lines changed

7 files changed

+74
-9
lines changed
 

‎.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
**/templates
22
**/node_modules
33
**/dist
4+
5+
# Contains Lodash templates
6+
packages/icon/plugin.js

‎docs/modules/icon.md

+13
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ icon: {
2222
- Default: `[16, 120, 144, 152, 192, 384, 512]`
2323

2424
Array of sizes to be generated (Square).
25+
26+
**accessibleIcons**
27+
- Default: true
28+
29+
Make icons accessible through `ctx` or Vue instances.
30+
31+
Example: `ctx.$icon(512)` will return the url for the icon with the size of `512px`.
32+
Will return an empty string when no icon in the given size is available (eg. when the size is not in `sizes` array).
33+
34+
**iconProperty**
35+
- Default: '$icon'
36+
37+
Name of property for accessible icons.

‎packages/icon/index.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,36 @@ function generateIcons (moduleOptions) {
6262
if (!this.options.manifest.icons) {
6363
this.options.manifest.icons = []
6464
}
65+
const assetIcons = []
66+
const exportIcons = {}
6567
icons.forEach(icon => {
66-
this.options.manifest.icons.push({
67-
src: fixUrl(`${publicPath}/${icon.fileName}`),
68+
const src = fixUrl(`${publicPath}/${icon.fileName}`)
69+
assetIcons.push({
70+
src,
6871
sizes: `${icon.size}x${icon.size}`,
6972
type: `image/png`
7073
})
74+
75+
exportIcons[icon.size] = src
7176
})
77+
78+
assetIcons.forEach(icon => { this.options.manifest.icons.push(icon) })
79+
80+
// Add plugin to Vue to access icons
81+
let moduleOptions = Object.assign({
82+
accessibleIcons: true,
83+
iconProperty: '$icon',
84+
icons: exportIcons
85+
}, options)
86+
87+
if (moduleOptions.accessibleIcons) {
88+
this.addPlugin({
89+
src: path.resolve(__dirname, './plugin.js'),
90+
fileName: 'nuxt-icons.js',
91+
options: moduleOptions
92+
})
93+
}
94+
7295
// Register webpack plugin to emit icons
7396
this.options.build.plugins.push({
7497
apply (compiler) {

‎packages/icon/plugin.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default async function (ctx, inject) {
2+
const moduleOptions = <%= serialize(options) %>
3+
inject(moduleOptions.iconProperty.replace('$', ''), retrieveIcons(moduleOptions.icons))
4+
}
5+
6+
const retrieveIcons = icons => size => icons[size] || ''

‎test/__snapshots__/pwa.test.js.snap

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Array [
2727
"fixture/.nuxt/dist/client/manifest.0fc4969c.json",
2828
"fixture/.nuxt/dist/client/ons.40ac840e.js",
2929
"fixture/.nuxt/dist/client/pages",
30+
"fixture/.nuxt/dist/client/pages/icons.js",
3031
"fixture/.nuxt/dist/client/pages/index.js",
3132
"fixture/.nuxt/dist/client/workbox.dev.42554690.js",
3233
"fixture/.nuxt/dist/server",
@@ -40,6 +41,7 @@ Array [
4041
"fixture/.nuxt/layouts/default.vue",
4142
"fixture/.nuxt/loading.html",
4243
"fixture/.nuxt/middleware.js",
44+
"fixture/.nuxt/nuxt-icons.js",
4345
"fixture/.nuxt/onesignal.js",
4446
"fixture/.nuxt/router.js",
4547
"fixture/.nuxt/server.js",
@@ -74,10 +76,13 @@ Array [
7476
"fixture/dist/_nuxt/manifest.0fc4969c.json",
7577
"fixture/dist/_nuxt/ons.40ac840e.js",
7678
"fixture/dist/_nuxt/pages",
79+
"fixture/dist/_nuxt/pages/icons.js",
7780
"fixture/dist/_nuxt/pages/index.js",
7881
"fixture/dist/_nuxt/workbox.dev.42554690.js",
7982
"fixture/dist/custom-sw.js",
8083
"fixture/dist/icon.png",
84+
"fixture/dist/icons",
85+
"fixture/dist/icons/index.html",
8186
"fixture/dist/index.html",
8287
"fixture/dist/sw.js",
8388
]

‎test/fixture/pages/icons.vue

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<pre v-text="$icon(512)"/>
3+
</template>
4+
5+
<script>
6+
export default {
7+
8+
}
9+
</script>

‎test/pwa.test.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ const getRelativePath = fileObj => path.relative(__dirname, fileObj.path)
1010
describe('pwa', () => {
1111
let nuxt
1212

13-
test('build and generate', async () => {
14-
nuxt = new Nuxt(require('./fixture/nuxt.config'))
13+
test(
14+
'build and generate', async () => {
15+
nuxt = new Nuxt(require('./fixture/nuxt.config'))
1516

16-
const builder = new Builder(nuxt)
17-
await builder.build()
17+
const builder = new Builder(nuxt)
18+
await builder.build()
1819

19-
const generator = new Generator(nuxt)
20-
await generator.generate({ build: false })
21-
})
20+
const generator = new Generator(nuxt)
21+
await generator.generate({ build: false })
22+
})
2223

2324
test('build files (.nuxt)', async () => {
2425
const buildFiles = klawSync(nuxt.options.buildDir).map(getRelativePath)
@@ -32,6 +33,11 @@ describe('pwa', () => {
3233
expect(generateFiles).toMatchSnapshot()
3334
})
3435

36+
test('accessible icons', async () => {
37+
const {html} = await nuxt.renderRoute('/icons')
38+
expect(html).toContain('/_nuxt/icons/icon_512.9mgd2ZDMcQu.png')
39+
})
40+
3541
test('sw.js', async () => {
3642
const swContents = await fs.readFile(path.resolve(nuxt.options.generate.dir, 'sw.js'), 'utf-8')
3743

0 commit comments

Comments
 (0)
Please sign in to comment.