Add plugin ssr option & rename process.browser

This commit is contained in:
Sébastien Chopin 2017-03-01 17:54:44 +01:00
parent 9dfe293bd0
commit 8253f5e75b
8 changed files with 57 additions and 29 deletions

17
TODO.md
View File

@ -17,3 +17,20 @@ Release:
## Deprecated ## Deprecated
- `process.BROWSER_BUILD` is deprecated in favour of `process.browser` (`BROWSER_BUILD` will be removed for the 1.0) - `process.BROWSER_BUILD` is deprecated in favour of `process.browser` (`BROWSER_BUILD` will be removed for the 1.0)
- `process.SERVER_BUILD` is deprecated in favour of `process.server` (`SERVER_BUILD` will be removed for the 1.0) - `process.SERVER_BUILD` is deprecated in favour of `process.server` (`SERVER_BUILD` will be removed for the 1.0)
## Define `plugins` only for client-side
Some Vue plugins might only work for client-side, you can now use an `Object` instead of a string to use a plugin only for client-side:
`nuxt.config.js`
```js
module.exports = {
plugins: [
'~plugins/client-and-server.js',
{
src: '~plugins/only-client-side.js',
ssr: false // disable for server-side
}
]
}
```

View File

@ -3,6 +3,7 @@ module.exports = {
vendor: ['axios', 'mini-toastr', 'vue-notifications'] vendor: ['axios', 'mini-toastr', 'vue-notifications']
}, },
plugins: [ plugins: [
'~plugins/vue-notifications.js' // ssr: false to only include it on client-side
{ src: '~plugins/vue-notifications.js', ssr: false }
] ]
} }

View File

@ -7,7 +7,7 @@
<script> <script>
let miniToastr let miniToastr
if (process.BROWSER_BUILD) { if (process.browser) {
miniToastr = require('mini-toastr') miniToastr = require('mini-toastr')
} }

View File

@ -1,10 +1,8 @@
// This code will be injected before initializing the root App // This code will be injected before initializing the root App
import Vue from 'vue' import Vue from 'vue'
import VueNotifications from 'vue-notifications' import VueNotifications from 'vue-notifications'
if (process.BROWSER_BUILD) {
// Include mini-toaster (or any other UI-notification library // Include mini-toaster (or any other UI-notification library
const miniToastr = require('mini-toastr') import miniToastr from 'mini-toastr'
// Here we setup messages output to `mini-toastr` // Here we setup messages output to `mini-toastr`
const toast = function ({ title, message, type, timeout, cb }) { const toast = function ({ title, message, type, timeout, cb }) {
@ -23,4 +21,3 @@ if (process.BROWSER_BUILD) {
// Activate plugin // Activate plugin
Vue.use(VueNotifications, options) Vue.use(VueNotifications, options)
}

View File

@ -12,7 +12,7 @@ let layouts = {
<% <%
var layoutsKeys = Object.keys(layouts); var layoutsKeys = Object.keys(layouts);
layoutsKeys.forEach(function (key, i) { %> layoutsKeys.forEach(function (key, i) { %>
"_<%= key %>": process.BROWSER_BUILD ? () => System.import('<%= layouts[key] %>') : require('<%= layouts[key] %>')<%= (i + 1) < layoutsKeys.length ? ',' : '' %> "_<%= key %>": process.browser ? () => System.import('<%= layouts[key] %>') : require('<%= layouts[key] %>')<%= (i + 1) < layoutsKeys.length ? ',' : '' %>
<% }) %> <% }) %>
} }

View File

@ -25,7 +25,7 @@ Vue.use(Meta, {
tagIDKeyName: 'hid' // the property name that vue-meta uses to determine whether to overwrite or append a tag tagIDKeyName: 'hid' // the property name that vue-meta uses to determine whether to overwrite or append a tag
}) })
if (process.BROWSER_BUILD) { if (process.browser) {
// window.onNuxtReady(() => console.log('Ready')) hook // window.onNuxtReady(() => console.log('Ready')) hook
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading) // Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
window._nuxtReadyCbs = [] window._nuxtReadyCbs = []
@ -35,8 +35,16 @@ if (process.BROWSER_BUILD) {
} }
// Includes external plugins // Includes external plugins
<% plugins.forEach(function (pluginPath) { %> <% plugins.forEach(function (plugin) {
require('<%= pluginPath %>') if (typeof plugin === 'string') { %>
require('<%= plugin %>')
<% } else if (plugin.src && plugin.ssr !== false) { %>
require('<%= plugin.src %>')
<% } else if (plugin.src) { %>
if (process.browser) {
require('<%= plugin.src %>')
}
<% } %>
<% }) %> <% }) %>
// root instance // root instance

View File

@ -23,7 +23,7 @@ function recursiveRoutes(routes, tab, components) {
var _components = [] var _components = []
var _routes = recursiveRoutes(router.routes, '\t\t', _components) var _routes = recursiveRoutes(router.routes, '\t\t', _components)
uniqBy(_components, '_name').forEach((route) => { %> uniqBy(_components, '_name').forEach((route) => { %>
const <%= route._name %> = process.BROWSER_BUILD ? () => System.import('<%= route.component %>') : require('<%= route.component %>') const <%= route._name %> = process.browser ? () => System.import('<%= route.component %>') : require('<%= route.component %>')
<% }) %> <% }) %>
<% if (router.scrollBehavior) { %> <% if (router.scrollBehavior) { %>

View File

@ -198,7 +198,12 @@ function * generateRoutesAndFiles () {
middleware: this.options.middleware, middleware: this.options.middleware,
store: this.options.store, store: this.options.store,
css: this.options.css, css: this.options.css,
plugins: this.options.plugins.map((p) => r(this.srcDir, p)), plugins: this.options.plugins.map((p) => {
if (typeof p === 'string') {
return { src: r(this.srcDir, p), ssr: true }
}
return { src: r(this.srcDir, p.src), ssr: !!p.ssr }
}),
appPath: './App.vue', appPath: './App.vue',
layouts: layouts, layouts: layouts,
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading), loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),