Skip to content

Commit d285b3b

Browse files
authored
feat: defaultAssets.icons all icons providers support (#85)
* feat: defaultAssets.icons all icons providers support * chore: add co-author Co-authored-by: ricardogobbosouza <[email protected]> * fix: fa4 link * chore: add warning if wrong value * fix: coverage ~ add test
1 parent 7ca2ca9 commit d285b3b

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default {
9393
```js
9494
{
9595
font: true,
96-
icons: true
96+
icons: 'mdi'
9797
}
9898
```
9999

@@ -104,10 +104,22 @@ These assets are handled automatically by default to provide a zero-configuratio
104104
`defaultAssets.font` automatically adds the **Roboto** font stylesheet from official google fonts to load the font with `font-display: swap`.
105105
You can disable it if you plan to use different font or manually handle font loading.
106106

107-
`defaultAssets.icons` automatically adds the icons stylesheet from [Material Design Icons](https://materialdesignicons.com) CDN to load all the icons.
108-
You can disable it and choose and setup your preferred icons preset by following [Vuetify Icons documentation](https://vuetifyjs.com/en/customization/icons)
107+
`defaultAssets.icons` automatically adds the icons stylesheet from a CDN to load all the icons (**not optimized for production**).
108+
Here are the accepted values for this option :
109109

110-
You can also set `defaultAssets` to `false` to prevent any automatic add of these two assets.
110+
| Value | Icons |
111+
|-------|-------|
112+
| `'mdi'` (default) | [Material Designs Icons](https://materialdesignicons.com/) ([CDN](https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css))
113+
| `'md'` | [Material Icons](https://material.io/resources/icons/) ([CDN](https://fonts.googleapis.com/css?family=Material+Icons))
114+
| `'fa'` | [Font Awesome 5](https://fontawesome.com/icons) ([CDN](https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@latest/css/all.min.css))
115+
| `'fa4'` | [Font Awesome 4](https://fontawesome.com/v4.7.0/icons/) ([CDN](https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css))
116+
| `false` | Disable auto add of the icons stylesheet
117+
118+
> This option (if not set to `false`) will automatically override `icons.iconfont` Vuetify option so that Vuetify components use these icons.
119+
120+
Please refer to [Vuetify Icons documentation](https://vuetifyjs.com/en/customization/icons) for more information about icons, notably for using only bunch of SVG icons instead of including all icons in your app.
121+
122+
You can also set the whole `defaultAssets` option to `false` to prevent any automatic add of these two assets.
111123

112124
### `treeShake`
113125

lib/module.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ const defaults = {
55
customVariables: [],
66
defaultAssets: {
77
font: true,
8-
icons: true
8+
icons: 'mdi'
99
},
1010
treeShake: process.env.NODE_ENV === 'production'
1111
}
1212

13+
const cdn = {
14+
'mdi': 'https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css',
15+
'md': 'https://fonts.googleapis.com/css?family=Material+Icons',
16+
'fa': 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@latest/css/all.min.css',
17+
'fa4': 'https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css'
18+
}
19+
1320
// See https://github.com/vuetifyjs/vuetify/releases/tag/v2.0.0-alpha.12
1421
const sassLoaderOptions = {
1522
implementation: require('sass'),
@@ -59,13 +66,21 @@ module.exports = function (moduleOptions) {
5966
})
6067
}
6168

62-
// Add Material Design Icons font
63-
if (options.defaultAssets.icons) {
64-
this.options.head.link.push({
65-
rel: 'stylesheet',
66-
type: 'text/css',
67-
href: 'https://cdn.materialdesignicons.com/3.8.95/css/materialdesignicons.min.css'
68-
})
69+
const defaultIconPreset = options.defaultAssets.icons
70+
71+
// Add Icons font
72+
if (defaultIconPreset) {
73+
if (cdn[defaultIconPreset]) {
74+
this.options.head.link.push({
75+
rel: 'stylesheet',
76+
type: 'text/css',
77+
href: cdn[defaultIconPreset]
78+
})
79+
} else {
80+
const wrapValue = val => typeof val === 'string' ? `\`'${val}'\`` : `\`${val}\``
81+
const supportedValues = [...Object.keys(cdn), false].map(wrapValue)
82+
console.warn(`[@nuxtjs/vuetify] Value ${wrapValue(defaultIconPreset)} for \`defaultAssets.icons\` option is not supported (Supported values : ${supportedValues.join(', ')})`)
83+
}
6984
}
7085

7186
// Enable tree-shaking with VuetifyLoader (https://github.com/vuetifyjs/vuetify-loader)
@@ -90,8 +105,9 @@ module.exports = function (moduleOptions) {
90105
src: path.resolve(__dirname, 'plugin.js'),
91106
fileName: 'vuetify.js',
92107
options: {
93-
vuetifyOptions,
94-
treeShake: options.treeShake
108+
defaultIconPreset,
109+
treeShake: options.treeShake,
110+
vuetifyOptions
95111
}
96112
})
97113
})

lib/plugin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import Vue from 'vue'
2-
<% if (options.treeShake) { %>
3-
import Vuetify from 'vuetify/lib'
4-
<% } else { %>
5-
import Vuetify from 'vuetify'
6-
<% } %>
2+
import Vuetify from '<%= options.treeShake ? 'vuetify/lib' : 'vuetify' %>'
73

84
Vue.use(Vuetify)
95

6+
const options = <%= serializeFunction(options.vuetifyOptions) %>
7+
8+
<% if (options.defaultIconPreset) { %>
9+
options.icons = options.icons || {}
10+
options.icons.iconfont = '<%= options.defaultIconPreset %>'
11+
<% } %>
12+
1013
export default (ctx) => {
11-
const vuetify = new Vuetify(<%= serializeFunction(options.vuetifyOptions) %>)
14+
const vuetify = new Vuetify(options)
1215

1316
ctx.app.vuetify = vuetify
1417
ctx.$vuetify = vuetify.framework

test/module.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@ describe('disable all default assets', () => {
4949
})
5050
})
5151

52+
describe('set wrong value to defaultAssets.icons', () => {
53+
let consoleWarnSpy
54+
let nuxt
55+
56+
beforeAll(async () => {
57+
consoleWarnSpy = jest.spyOn(console, 'warn')
58+
nuxt = new Nuxt({
59+
...config,
60+
vuetify: {
61+
defaultAssets: {
62+
icons: 'wrong'
63+
}
64+
}
65+
})
66+
await nuxt.ready()
67+
await new Builder(nuxt).build()
68+
})
69+
70+
afterAll(async () => {
71+
await nuxt.close()
72+
})
73+
74+
test('should have warned user', () => {
75+
expect(consoleWarnSpy).toHaveBeenCalledWith("[@nuxtjs/vuetify] Value `'wrong'` for `defaultAssets.icons` option is not supported (Supported values : `'mdi'`, `'md'`, `'fa'`, `'fa4'`, `false`)")
76+
})
77+
})
78+
5279
describe.skip('enable treeShake', () => {
5380
let nuxt
5481

0 commit comments

Comments
 (0)