From 55db6e336f644a01dca16f1c0f4b6d5dcc53db74 Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Fri, 14 Apr 2017 11:57:08 +0200 Subject: [PATCH] Add injectAs feature --- lib/app/index.js | 38 +++++++++++++++++--------------------- lib/app/server.js | 8 ++------ lib/app/utils.js | 11 +++++++++++ 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 26dd7fd8c2..17b7269867 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -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 // here we inject the router and store to all child components, // making them available everywhere as `this.$router` and `this.$store`. @@ -100,15 +87,24 @@ let app = { ...App } -// Inject external plugins in app + +// Includes & Inject external plugins <% plugins.forEach(function (plugin) { - if (plugin.injectAs && plugin.ssr) { %> - app.<%= plugin.injectAs %> = <%= plugin.injectAs %> - <% } else if (plugin.injectAs) { %> - if (process.browser) { - app.<%= plugin.injectAs %> = <%= plugin.injectAs %> - } +if (plugin.ssr) { %> +<%= (plugin.injectAs ? 'let ' + plugin.injectAs + ' = ' : '') %>require('<%= plugin.src %>') +<% if (plugin.injectAs) { %> +<%= plugin.injectAs + ' = ' + plugin.injectAs + '.default || ' + 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 } diff --git a/lib/app/server.js b/lib/app/server.js index ddc0849849..2ce018ac35 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -220,14 +220,10 @@ export default context => { .then(() => _app) }) .catch(function (error) { - if (!componentsLoaded && error instanceof Error) { + if (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1) { return Promise.reject(error) } - if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) { - let statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500 - error = { statusCode, message: error.message } - } - else if (typeof error === 'string') { + if (typeof error === 'string') { error = { statusCode: 500, message: error } } context.nuxt.error = context.error(error) diff --git a/lib/app/utils.js b/lib/app/utils.js index f6b34f0739..934ccedc33 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -1,4 +1,5 @@ 'use strict' +import { app } from './index' export function getMatchedComponents (route) { 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.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 }