mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
chore: polish app/index.js
This commit is contained in:
parent
f635b73a0b
commit
5da4e49d56
@ -1,17 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
import 'es6-promise/auto'
|
||||
import Vue from 'vue'
|
||||
import Meta from 'vue-meta'
|
||||
import { createRouter } from './router.js'
|
||||
<% if (store) { %>import { createStore } from './store.js'<% } %>
|
||||
import NuxtChild from './components/nuxt-child.js'
|
||||
import NuxtLink from './components/nuxt-link.js'
|
||||
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>'
|
||||
import Nuxt from './components/nuxt.vue'
|
||||
import App from '<%= appPath %>'
|
||||
|
||||
import { getContext } from './utils'
|
||||
<% if (store) { %>import { createStore } from './store.js'<% } %>
|
||||
|
||||
if (process.browser) {
|
||||
// window.onNuxtReady(() => console.log('Ready')) hook
|
||||
@ -22,11 +19,24 @@ if (process.browser) {
|
||||
}
|
||||
}
|
||||
|
||||
// Import SSR plugins
|
||||
<% plugins.forEach(function (plugin) { if (plugin.ssr)
|
||||
{ %>let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>')
|
||||
<% if (plugins.filter(p => p.ssr).length) { %>
|
||||
// Require plugins
|
||||
<% plugins.filter(p => p.ssr).forEach(plugin => { %>
|
||||
let <%= plugin.name %> = require('<%= relativeToBuild(plugin.src) %>')
|
||||
<%= 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>
|
||||
Vue.component(NuxtChild.name, NuxtChild)
|
||||
@ -48,35 +58,34 @@ const defaultTransition = <%=
|
||||
.replace('beforeEnter(', 'function(').replace('enter(', 'function(').replace('afterEnter(', 'function(')
|
||||
.replace('enterCancelled(', 'function(').replace('beforeLeave(', 'function(').replace('leave(', 'function(')
|
||||
.replace('afterLeave(', 'function(').replace('leaveCancelled(', 'function(')
|
||||
%>
|
||||
%>
|
||||
|
||||
async function createApp (ssrContext) {
|
||||
<% if (store) { %>
|
||||
const store = createStore()
|
||||
<% } %>
|
||||
const router = createRouter()
|
||||
|
||||
<% if (store) { %>const store = createStore()<% } %>
|
||||
|
||||
if (process.server && ssrContext && ssrContext.url) {
|
||||
await new Promise((resolve, reject) => {
|
||||
router.push(ssrContext.url, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
<% if (store) { %>
|
||||
if (process.browser) {
|
||||
<% if (store) { %>
|
||||
// Replace store state before calling plugins
|
||||
if (window.__NUXT__ && window.__NUXT__.state) {
|
||||
store.replaceState(window.__NUXT__.state)
|
||||
}
|
||||
<% } %>
|
||||
}
|
||||
<% } %>
|
||||
|
||||
// root instance
|
||||
// Create Root instance
|
||||
// here we inject the router and store to all child components,
|
||||
// making them available everywhere as `this.$router` and `this.$store`.
|
||||
let app = {
|
||||
const app = {
|
||||
router,
|
||||
<%= (store ? 'store,' : '') %>
|
||||
<% if(store) { %> store,<% } %>
|
||||
_nuxt: {
|
||||
defaultTransition,
|
||||
transitions: [ defaultTransition ],
|
||||
@ -113,36 +122,38 @@ async function createApp (ssrContext) {
|
||||
...App
|
||||
}
|
||||
|
||||
const next = ssrContext ? ssrContext.next : (location) => app.router.push(location)
|
||||
const next = ssrContext ? ssrContext.next : location => app.router.push(location)
|
||||
|
||||
const ctx = getContext({
|
||||
isServer: !!ssrContext,
|
||||
isClient: !ssrContext,
|
||||
route: router.currentRoute,
|
||||
next,
|
||||
error: app._nuxt.error.bind(app),
|
||||
<%= (store ? 'store,' : '') %>
|
||||
<% if(store) { %> store,<% } %>
|
||||
req: ssrContext ? ssrContext.req : undefined,
|
||||
res: ssrContext ? ssrContext.res : undefined,
|
||||
}, app)
|
||||
|
||||
// Inject external plugins
|
||||
<% plugins.forEach(function (plugin) {
|
||||
if (plugin.ssr) { %>
|
||||
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)
|
||||
}
|
||||
}
|
||||
<% }
|
||||
}) %>
|
||||
<% if (plugins.filter(p => p.ssr).length) { %>
|
||||
<% plugins.filter(p => p.ssr).forEach(plugin => { %>
|
||||
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 }
|
||||
|
Loading…
Reference in New Issue
Block a user