Advertisement
Guest User

Untitled

a guest
Feb 6th, 2018
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'es6-promise/auto'
  2. import Vue from 'vue'
  3. import Meta from 'vue-meta'
  4. import { createRouter } from './router.js'
  5. import NoSSR from './components/no-ssr.js'
  6. import NuxtChild from './components/nuxt-child.js'
  7. import NuxtLink from './components/nuxt-link.js'
  8. import NuxtError from './components/nuxt-error.vue'
  9. import Nuxt from './components/nuxt.js'
  10. import App from './App.js'
  11. import { setContext, getLocation, getRouteData } from './utils'
  12. import { createStore } from './store.js'
  13.  
  14. /* Plugins */
  15. import nuxt_plugin_authplugin_72500e98 from 'nuxt_plugin_authplugin_72500e98' // Source: ./auth\\auth.plugin.js
  16. import nuxt_plugin_axios_5bddae8a from 'nuxt_plugin_axios_5bddae8a' // Source: ./axios.js
  17.  
  18.  
  19. // Component: <no-ssr>
  20. Vue.component(NoSSR.name, NoSSR)
  21.  
  22. // Component: <nuxt-child>
  23. Vue.component(NuxtChild.name, NuxtChild)
  24.  
  25. // Component: <nuxt-link>
  26. Vue.component(NuxtLink.name, NuxtLink)
  27.  
  28. // Component: <nuxt>`
  29. Vue.component(Nuxt.name, Nuxt)
  30.  
  31. // vue-meta configuration
  32. Vue.use(Meta, {
  33.   keyName: 'head', // the component option name that vue-meta looks for meta info on.
  34.   attribute: 'data-n-head', // the attribute name vue-meta adds to the tags it observes
  35.   ssrAttribute: 'data-n-head-ssr', // the attribute name that lets vue-meta know that meta info has already been server-rendered
  36.   tagIDKeyName: 'hid' // the property name that vue-meta uses to determine whether to overwrite or append a tag
  37. })
  38.  
  39. const defaultTransition = {"name":"page","mode":"out-in","appear":false,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  40.  
  41. async function createApp (ssrContext) {
  42.   const router = createRouter()
  43.  
  44.  
  45.   const store = createStore()
  46.   // Add this.$router into store actions/mutations
  47.   store.$router = router
  48.  
  49.  
  50.   // Create Root instance
  51.   // here we inject the router and store to all child components,
  52.   // making them available everywhere as `this.$router` and `this.$store`.
  53.   const app = {
  54.     router,
  55.     store,
  56.     nuxt: {
  57.       defaultTransition,
  58.       transitions: [ defaultTransition ],
  59.       setTransitions (transitions) {
  60.         if (!Array.isArray(transitions)) {
  61.           transitions = [ transitions ]
  62.         }
  63.         transitions = transitions.map((transition) => {
  64.           if (!transition) {
  65.             transition = defaultTransition
  66.           } else if (typeof transition === 'string') {
  67.             transition = Object.assign({}, defaultTransition, { name: transition })
  68.           } else {
  69.             transition = Object.assign({}, defaultTransition, transition)
  70.           }
  71.           return transition
  72.         })
  73.         this.$options.nuxt.transitions = transitions
  74.         return transitions
  75.       },
  76.       err: null,
  77.       dateErr: null,
  78.       error (err) {
  79.         err = err || null
  80.         app.context._errored = !!err
  81.         if (typeof err === 'string') err = { statusCode: 500, message: err }
  82.         const nuxt = this.nuxt || this.$options.nuxt
  83.         nuxt.dateErr = Date.now()
  84.         nuxt.err = err
  85.         // Used in lib/server.js
  86.         if (ssrContext) ssrContext.nuxt.error = err
  87.         return err
  88.       }
  89.     },
  90.     ...App
  91.   }
  92.  
  93.   // Make app available into store via this.app
  94.   store.app = app
  95.  
  96.   const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  97.   // Resolve route
  98.   let route
  99.   if (ssrContext) {
  100.     route = router.resolve(ssrContext.url).route
  101.   } else {
  102.     const path = getLocation(router.options.base)
  103.     route = router.resolve(path).route
  104.   }
  105.  
  106.   // Set context to app.context
  107.   await setContext(app, {
  108.     route,
  109.     next,
  110.     error: app.nuxt.error.bind(app),
  111.     store,
  112.     payload: ssrContext ? ssrContext.payload : undefined,
  113.     req: ssrContext ? ssrContext.req : undefined,
  114.     res: ssrContext ? ssrContext.res : undefined,
  115.     beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined
  116.   })
  117.  
  118.   const inject = function (key, value) {
  119.     if (!key) throw new Error('inject(key, value) has no key provided')
  120.     if (!value) throw new Error('inject(key, value) has no value provided')
  121.     key = '$' + key
  122.     // Add into app
  123.     app[key] = value
  124.    
  125.     // Add into store
  126.     store[key] = app[key]
  127.    
  128.     // Check if plugin not already installed
  129.     const installKey = '__nuxt_' + key + '_installed__'
  130.     if (Vue[installKey]) return
  131.     Vue[installKey] = true
  132.     // Call Vue.use() to install the plugin into vm
  133.     Vue.use(() => {
  134.       if (!Vue.prototype.hasOwnProperty(key)) {
  135.         Object.defineProperty(Vue.prototype, key, {
  136.           get () {
  137.             return this.$root.$options[key]
  138.           }
  139.         })
  140.       }
  141.     })
  142.   }
  143.  
  144.  
  145.   if (process.browser) {
  146.     // Replace store state before plugins execution
  147.     if (window.__NUXT__ && window.__NUXT__.state) {
  148.       store.replaceState(window.__NUXT__.state)
  149.     }
  150.   }
  151.  
  152.  
  153.   // Plugin execution
  154.  
  155.   if (typeof nuxt_plugin_authplugin_72500e98 === 'function') await nuxt_plugin_authplugin_72500e98(app.context, inject)
  156.   if (typeof nuxt_plugin_axios_5bddae8a === 'function') await nuxt_plugin_axios_5bddae8a(app.context, inject)
  157.  
  158.  
  159.   // If server-side, wait for async component to be resolved first
  160.   if (process.server && ssrContext && ssrContext.url) {
  161.     await new Promise((resolve, reject) => {
  162.       router.push(ssrContext.url, resolve, () => {
  163.         // navigated to a different route in router guard
  164.         const unregister = router.afterEach(async (to, from, next) => {
  165.           ssrContext.url = to.fullPath
  166.           app.context.route = await getRouteData(to)
  167.           app.context.params = to.params || {}
  168.           app.context.query = to.query || {}
  169.           unregister()
  170.           resolve()
  171.         })
  172.       })
  173.     })
  174.   }
  175.  
  176.   return {
  177.     app,
  178.     router,
  179.     store
  180.   }
  181. }
  182.  
  183. export { createApp, NuxtError }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement