Add injectAs feature

This commit is contained in:
Sebastien Chopin 2017-04-14 11:57:08 +02:00
parent 7a6bc85b62
commit 55db6e336f
3 changed files with 30 additions and 27 deletions

View File

@ -40,19 +40,6 @@ if (process.browser) {
} }
} }
// Includes external plugins
<% plugins.forEach(function (plugin) {
if (plugin.ssr) { %>
<%= (plugin.injectAs ? 'let ' + plugin.injectAs + ' = ' : '') %>require('<%= plugin.src %>')
<%= (plugin.injectAs ? plugin.injectAs + ' = ' + plugin.injectAs + '.default || ' + plugin.injectAs : '') %>
<% } else { %>
if (process.browser) {
<%= (plugin.injectAs ? 'let ' + plugin.injectAs + ' = ' : '') %>require('<%= plugin.src %>')
<%= (plugin.injectAs ? plugin.injectAs + ' = ' + plugin.injectAs + '.default || ' + plugin.injectAs : '') %>
}
<% } %>
<% }) %>
// root instance // root instance
// here we inject the router and store to all child components, // here we inject the router and store to all child components,
// making them available everywhere as `this.$router` and `this.$store`. // making them available everywhere as `this.$router` and `this.$store`.
@ -100,15 +87,24 @@ let app = {
...App ...App
} }
// Inject external plugins in app
// Includes & Inject external plugins
<% plugins.forEach(function (plugin) { <% plugins.forEach(function (plugin) {
if (plugin.injectAs && plugin.ssr) { %> if (plugin.ssr) { %>
app.<%= plugin.injectAs %> = <%= plugin.injectAs %> <%= (plugin.injectAs ? 'let ' + plugin.injectAs + ' = ' : '') %>require('<%= plugin.src %>')
<% } else if (plugin.injectAs) { %> <% if (plugin.injectAs) { %>
if (process.browser) { <%= plugin.injectAs + ' = ' + plugin.injectAs + '.default || ' + plugin.injectAs %>
app.<%= plugin.injectAs %> = <%= plugin.injectAs %> app['<%= plugin.injectAs %>'] = <%= plugin.injectAs %>
} <% }
} else { %>
if (process.browser) {
<%= (plugin.injectAs ? 'let ' + plugin.injectAs + ' = ' : '') %>require('<%= plugin.src %>')
<% if (plugin.injectAs) { %>
<%= plugin.injectAs + ' = ' + plugin.injectAs + '.default || ' + plugin.injectAs %>
app['<%= plugin.injectAs %>'] = <%= plugin.injectAs %>
<% } %> <% } %>
<% }) %> }
<% }
}) %>
export { app, router<%= (store ? ', store' : '') %>, NuxtError } export { app, router<%= (store ? ', store' : '') %>, NuxtError }

View File

@ -220,14 +220,10 @@ export default context => {
.then(() => _app) .then(() => _app)
}) })
.catch(function (error) { .catch(function (error) {
if (!componentsLoaded && error instanceof Error) { if (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1) {
return Promise.reject(error) return Promise.reject(error)
} }
if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) { if (typeof error === 'string') {
let statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
error = { statusCode, message: error.message }
}
else if (typeof error === 'string') {
error = { statusCode: 500, message: error } error = { statusCode: 500, message: error }
} }
context.nuxt.error = context.error(error) context.nuxt.error = context.error(error)

View File

@ -1,4 +1,5 @@
'use strict' 'use strict'
import { app } from './index'
export function getMatchedComponents (route) { export function getMatchedComponents (route) {
return [].concat.apply([], route.matched.map(function (m) { return [].concat.apply([], route.matched.map(function (m) {
@ -55,6 +56,16 @@ export function getContext (context) {
} }
if (context.req) ctx.req = context.req if (context.req) ctx.req = context.req
if (context.res) ctx.res = context.res if (context.res) ctx.res = context.res
// Inject external plugins in context
<% plugins.forEach(function (plugin) {
if (plugin.injectAs && plugin.ssr) { %>
ctx['<%= plugin.injectAs %>'] = app['<%= plugin.injectAs %>']
<% } else if (plugin.injectAs) { %>
if (process.browser) {
ctx['<%= plugin.injectAs %>'] = app['<%= plugin.injectAs %>']
}
<% } %>
<% }) %>
return ctx return ctx
} }