chore: polish app/index.js

This commit is contained in:
Pooya Parsa 2017-07-10 02:59:27 +04:30
parent f635b73a0b
commit 5da4e49d56
1 changed files with 47 additions and 36 deletions

View File

@ -1,17 +1,14 @@
'use strict'
import 'es6-promise/auto' import 'es6-promise/auto'
import Vue from 'vue' import Vue from 'vue'
import Meta from 'vue-meta' import Meta from 'vue-meta'
import { createRouter } from './router.js' import { createRouter } from './router.js'
<% if (store) { %>import { createStore } from './store.js'<% } %>
import NuxtChild from './components/nuxt-child.js' import NuxtChild from './components/nuxt-child.js'
import NuxtLink from './components/nuxt-link.js' import NuxtLink from './components/nuxt-link.js'
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>' import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>'
import Nuxt from './components/nuxt.vue' import Nuxt from './components/nuxt.vue'
import App from '<%= appPath %>' import App from '<%= appPath %>'
import { getContext } from './utils' import { getContext } from './utils'
<% if (store) { %>import { createStore } from './store.js'<% } %>
if (process.browser) { if (process.browser) {
// window.onNuxtReady(() => console.log('Ready')) hook // window.onNuxtReady(() => console.log('Ready')) hook
@ -22,11 +19,24 @@ if (process.browser) {
} }
} }
// Import SSR plugins <% if (plugins.filter(p => p.ssr).length) { %>
<% plugins.forEach(function (plugin) { if (plugin.ssr) // Require plugins
{ %>let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>') <% plugins.filter(p => p.ssr).forEach(plugin => { %>
let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>')
<%= plugin.name %> = <%= plugin.name %>.default || <%= plugin.name %> <%= plugin.name %> = <%= plugin.name %>.default || <%= plugin.name %>
<% }}) %> <% }) %>
<% } %>
<% if (plugins.filter(p => !p.ssr).length) { %>
// Require browser-only plugins
if (process.browser) {
<% plugins.filter(p => !p.ssr).forEach(plugin => { %>
let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>')
<%= plugin.name %> = <%= plugin.name %>.default || <%= plugin.name %>
<% }) %>
}
<% } %>
// Component: <nuxt-child> // Component: <nuxt-child>
Vue.component(NuxtChild.name, NuxtChild) Vue.component(NuxtChild.name, NuxtChild)
@ -48,35 +58,34 @@ const defaultTransition = <%=
.replace('beforeEnter(', 'function(').replace('enter(', 'function(').replace('afterEnter(', 'function(') .replace('beforeEnter(', 'function(').replace('enter(', 'function(').replace('afterEnter(', 'function(')
.replace('enterCancelled(', 'function(').replace('beforeLeave(', 'function(').replace('leave(', 'function(') .replace('enterCancelled(', 'function(').replace('beforeLeave(', 'function(').replace('leave(', 'function(')
.replace('afterLeave(', 'function(').replace('leaveCancelled(', 'function(') .replace('afterLeave(', 'function(').replace('leaveCancelled(', 'function(')
%> %>
async function createApp (ssrContext) { async function createApp (ssrContext) {
<% if (store) { %>
const store = createStore()
<% } %>
const router = createRouter() const router = createRouter()
<% if (store) { %>const store = createStore()<% } %>
if (process.server && ssrContext && ssrContext.url) { if (process.server && ssrContext && ssrContext.url) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
router.push(ssrContext.url, resolve, reject) router.push(ssrContext.url, resolve, reject)
}) })
} }
<% if (store) { %>
if (process.browser) { if (process.browser) {
<% if (store) { %>
// Replace store state before calling plugins // Replace store state before calling plugins
if (window.__NUXT__ && window.__NUXT__.state) { if (window.__NUXT__ && window.__NUXT__.state) {
store.replaceState(window.__NUXT__.state) store.replaceState(window.__NUXT__.state)
} }
<% } %>
} }
<% } %>
// root instance // Create 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`.
let app = { const app = {
router, router,
<%= (store ? 'store,' : '') %> <% if(store) { %> store,<% } %>
_nuxt: { _nuxt: {
defaultTransition, defaultTransition,
transitions: [ defaultTransition ], transitions: [ defaultTransition ],
@ -113,36 +122,38 @@ async function createApp (ssrContext) {
...App ...App
} }
const next = ssrContext ? ssrContext.next : (location) => app.router.push(location) const next = ssrContext ? ssrContext.next : location => app.router.push(location)
const ctx = getContext({ const ctx = getContext({
isServer: !!ssrContext, isServer: !!ssrContext,
isClient: !ssrContext, isClient: !ssrContext,
route: router.currentRoute, route: router.currentRoute,
next, next,
error: app._nuxt.error.bind(app), error: app._nuxt.error.bind(app),
<%= (store ? 'store,' : '') %> <% if(store) { %> store,<% } %>
req: ssrContext ? ssrContext.req : undefined, req: ssrContext ? ssrContext.req : undefined,
res: ssrContext ? ssrContext.res : undefined, res: ssrContext ? ssrContext.res : undefined,
}, app) }, app)
// Inject external plugins <% if (plugins.filter(p => p.ssr).length) { %>
<% plugins.forEach(function (plugin) { <% plugins.filter(p => p.ssr).forEach(plugin => { %>
if (plugin.ssr) { %> if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx)
if (typeof <%= plugin.name %> === 'function') { <% }) %>
await <%= plugin.name %>(ctx) <% } %>
}
<% } else { %>
if (process.browser) {
let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>')
<%= plugin.name %> = <%= plugin.name %>.default || <%= plugin.name %>
if (typeof <%= plugin.name %> === 'function') {
await <%= plugin.name %>(ctx)
}
}
<% }
}) %>
return { app, router<%= (store ? ', store' : '') %> } <% if (plugins.filter(p => !p.ssr).length) { %>
if (process.browser) {
<% plugins.filter(p => !p.ssr).forEach(plugin => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx)
<% }) %>
}
<% } %>
return {
app,
router,
<% if(store) { %> store <% } %>
}
} }
export { createApp, NuxtError } export { createApp, NuxtError }