Skip to content

Commit 96177a4

Browse files
authored
feat: optionsPath + function behavior (#99)
* feat: optionsPath + function behavior * fix: typo * fix: typo * chore: add directory aliases notice * chore: remove useless option given to plugin template
1 parent 32a7549 commit 96177a4

File tree

5 files changed

+50
-47
lines changed

5 files changed

+50
-47
lines changed

README.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ yarn add --dev @nuxtjs/vuetify # or npm install --save-dev @nuxtjs/vuetify
5353
}
5454
```
5555

56-
## Module Options
56+
## Options
5757

5858
### `customVariables`
5959

@@ -121,68 +121,67 @@ Please refer to [Vuetify Icons documentation](https://vuetifyjs.com/en/customiza
121121

122122
You can also set the whole `defaultAssets` option to `false` to prevent any automatic add of these two assets.
123123

124-
### `treeShake`
124+
### `optionsPath`
125125

126-
- Type: `Boolean`
127-
- Default: `process.env.NODE_ENV === 'production'`
126+
- Type: `String`
128127

129-
Uses [vuetify-loader](https://github.com/vuetifyjs/vuetify-loader) to enable automatic [tree-shaking](https://vuetifyjs.com/en/customization/a-la-carte).
130-
Enabled only for production by default.
128+
Location of the Vuetify options that will be passed to Vuetify.
131129

132-
## Vuetify Options
130+
This file will be compiled by **webpack**, which means you'll benefit fast hot reload when changing these options, but also be able to use TypeScript without being forced to use TypeScript runtime.
133131

134-
Except the prior `customVariables`, `defaultAssets` and `treeShaking` options, all other options will be passed to Vuetify.
132+
```js
133+
// nuxt.config.js
134+
export default {
135+
vuetify: {
136+
optionsPath: './vuetify.options.js'
137+
}
138+
}
139+
```
135140

141+
> Note that you can also use [Directory Aliases](https://nuxtjs.org/guide/directory-structure#aliases) like `'~/path/to/option.js'`
136142
137-
It includes :
143+
All vuetify options are supported, it includes :
138144
- [**Breakpoints**](https://vuetifyjs.com/en/customization/breakpoints)
139145
- [**Icons**](https://vuetifyjs.com/en/customization/icons)
140146
- [**Internationalization (i18n)**]()
141147
- [**RTL (bidirectionality)**](https://vuetifyjs.com/en/customization/rtl)
142148
- [**Theme**](https://vuetifyjs.com/en/customization/theme)
143149

150+
144151
```js
145-
// nuxt.config.js
152+
// vuetify.options.js
146153
export default {
147-
vuetify: {
148-
breakpoint: {},
149-
icons: {},
150-
lang: {},
151-
rtl: true,
152-
theme: {}
153-
}
154+
breakpoint: {},
155+
icons: {},
156+
lang: {},
157+
rtl: true,
158+
theme: {}
154159
}
155160
```
156161

157-
### Hot reload of options in development
158-
159-
In development mode, passing Vuetify options through module options in your configuration file means Nuxt will have to do a full rebuild of your application when changing them. That being said, it may not be ideal if you're tweaking a lot these options to find the ones that fits best for you.
162+
> Notice that passing the Vuetify options directly to Module options is still supported, but it will trigger Nuxt entire rebuild if options are changed.
160163
161-
That's why we implemented a feature that will in a close future (Nuxt 2.9) used by more modules, that consist of being able to customize options through an external file compiled by Webpack instead of runtime.
164+
If you need to access Nuxt context within the options file, you need to export a function instead :
162165

163-
In the case of this module, the options need to sit in `~/app/vuetify/options.js`
164166

165167
```js
166-
// app/vuetify/options.js
167-
export default {
168-
theme: {
169-
dark: true
168+
// vuetify.options.js
169+
export default function ({ app }) {
170+
return {
171+
lang: {
172+
t: (key, ...params) => app.i18n.t(key, params)
173+
}
170174
}
171175
}
172176
```
173177

174-
The `app` folder can be customized in `nuxt.config.js` this way :
178+
### `treeShake`
175179

176-
```js
177-
// nuxt.config.js
178-
export default {
179-
dir: {
180-
app: 'somewhere' // Nuxt will then look for 'somewhere/vuetify/options.js'
181-
}
182-
}
183-
```
180+
- Type: `Boolean`
181+
- Default: `process.env.NODE_ENV === 'production'`
184182

185-
> Note that if you don't think having to change a lot Vuetify options, keeping the options in your `nuxt.config.js` is totally fine. This feature is above all present to fill the need of fast hot reload in development when changing the Vuetify options.
183+
Uses [vuetify-loader](https://github.com/vuetifyjs/vuetify-loader) to enable automatic [tree-shaking](https://vuetifyjs.com/en/customization/a-la-carte).
184+
Enabled only for production by default.
186185

187186
## TypeScript
188187

lib/module.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const defaults = {
88
font: true,
99
icons: 'mdi'
1010
},
11+
optionsPath: undefined,
1112
treeShake: process.env.NODE_ENV === 'production'
1213
}
1314

@@ -99,15 +100,16 @@ module.exports = function (moduleOptions) {
99100
const vuetifyOptions = { ...options }
100101
delete vuetifyOptions.customVariables
101102
delete vuetifyOptions.defaultAssets
103+
delete vuetifyOptions.optionsPath
102104
delete vuetifyOptions.treeShake
103105

104-
const app = this.options.dir.app || 'app'
105-
const userOptions = path.join(this.options.srcDir, app, 'vuetify', 'options.js')
106-
const userOptionsExists = fs.existsSync(userOptions)
106+
const optionsPath = this.nuxt.resolver.resolveAlias(options.optionsPath ||
107+
path.join(this.options.dir.app || 'app', 'vuetify', 'options.js'))
107108

109+
// Register options template
108110
this.addTemplate({
109111
fileName: 'vuetify/options.js',
110-
src: userOptionsExists ? userOptions : path.join(__dirname, 'templates/options.js'),
112+
src: fs.existsSync(optionsPath) ? optionsPath : path.join(__dirname, 'templates/options.js'),
111113
options: vuetifyOptions
112114
})
113115

@@ -117,8 +119,7 @@ module.exports = function (moduleOptions) {
117119
src: path.resolve(__dirname, 'templates/plugin.js'),
118120
options: {
119121
defaultIconPreset,
120-
treeShake: options.treeShake,
121-
vuetifyOptions
122+
treeShake: options.treeShake
122123
}
123124
})
124125
})

lib/templates/plugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import options from './options'
44

55
Vue.use(Vuetify)
66

7+
export default (ctx) => {
8+
const vuetifyOptions = typeof options === 'function' ? options(ctx) : options
9+
710
<% if (options.defaultIconPreset) { %>
8-
options.icons = options.icons || {}
9-
options.icons.iconfont = '<%= options.defaultIconPreset %>'
11+
vuetifyOptions.icons = vuetifyOptions.icons || {}
12+
vuetifyOptions.icons.iconfont = '<%= options.defaultIconPreset %>'
1013
<% } %>
1114

12-
export default (ctx) => {
13-
const vuetify = new Vuetify(options)
15+
const vuetify = new Vuetify(vuetifyOptions)
1416

1517
ctx.app.vuetify = vuetify
1618
ctx.$vuetify = vuetify.framework

test/fixture/nuxt.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212
modules: [
1313
[
1414
require('../..'), {
15-
customVariables: ['~/assets/variables.scss']
15+
customVariables: ['~/assets/variables.scss'],
16+
optionsPath: './vuetify.options.js'
1617
}
1718
]
1819
]
File renamed without changes.

0 commit comments

Comments
 (0)