index.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import Meta from 'vue-meta'
  4. import ClientOnly from 'vue-client-only'
  5. import NoSsr from 'vue-no-ssr'
  6. import { createRouter } from './router.js'
  7. import NuxtChild from './components/nuxt-child.js'
  8. import NuxtError from '../layouts/error.vue'
  9. import Nuxt from './components/nuxt.js'
  10. import App from './App.js'
  11. import { setContext, getLocation, getRouteData, normalizeError } from './utils'
  12. import { createStore } from './store.js'
  13. /* Plugins */
  14. import nuxt_plugin_plugin_666bcd44 from 'nuxt_plugin_plugin_666bcd44' // Source: ./components/plugin.js (mode: 'all')
  15. import nuxt_plugin_axios_939e3d18 from 'nuxt_plugin_axios_939e3d18' // Source: ./axios.js (mode: 'all')
  16. import nuxt_plugin_elementui_d905880e from 'nuxt_plugin_elementui_d905880e' // Source: ../plugins/element-ui (mode: 'all')
  17. import nuxt_plugin_axios_2228ef02 from 'nuxt_plugin_axios_2228ef02' // Source: ../plugins/axios (mode: 'all')
  18. import nuxt_plugin_router_3f7e063d from 'nuxt_plugin_router_3f7e063d' // Source: ../plugins/router (mode: 'all')
  19. import nuxt_plugin_videoplayer_233ef0a8 from 'nuxt_plugin_videoplayer_233ef0a8' // Source: ../plugins/videoplayer (mode: 'client')
  20. import nuxt_plugin_vueLazyLoad_0ac1aa32 from 'nuxt_plugin_vueLazyLoad_0ac1aa32' // Source: ../plugins/vueLazyLoad (mode: 'client')
  21. import nuxt_plugin_fun_2b54370c from 'nuxt_plugin_fun_2b54370c' // Source: ../plugins/fun.js (mode: 'client')
  22. // Component: <ClientOnly>
  23. Vue.component(ClientOnly.name, ClientOnly)
  24. // TODO: Remove in Nuxt 3: <NoSsr>
  25. Vue.component(NoSsr.name, {
  26. ...NoSsr,
  27. render (h, ctx) {
  28. if (process.client && !NoSsr._warned) {
  29. NoSsr._warned = true
  30. console.warn('<no-ssr> has been deprecated and will be removed in Nuxt 3, please use <client-only> instead')
  31. }
  32. return NoSsr.render(h, ctx)
  33. }
  34. })
  35. // Component: <NuxtChild>
  36. Vue.component(NuxtChild.name, NuxtChild)
  37. Vue.component('NChild', NuxtChild)
  38. // Component NuxtLink is imported in server.js or client.js
  39. // Component: <Nuxt>
  40. Vue.component(Nuxt.name, Nuxt)
  41. Object.defineProperty(Vue.prototype, '$nuxt', {
  42. get() {
  43. const globalNuxt = this.$root.$options.$nuxt
  44. if (process.client && !globalNuxt && typeof window !== 'undefined') {
  45. return window.$nuxt
  46. }
  47. return globalNuxt
  48. },
  49. configurable: true
  50. })
  51. Vue.use(Meta, {"keyName":"head","attribute":"data-n-head","ssrAttribute":"data-n-head-ssr","tagIDKeyName":"hid"})
  52. const defaultTransition = {"name":"page","mode":"out-in","appear":false,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  53. const originalRegisterModule = Vuex.Store.prototype.registerModule
  54. function registerModule (path, rawModule, options = {}) {
  55. const preserveState = process.client && (
  56. Array.isArray(path)
  57. ? !!path.reduce((namespacedState, path) => namespacedState && namespacedState[path], this.state)
  58. : path in this.state
  59. )
  60. return originalRegisterModule.call(this, path, rawModule, { preserveState, ...options })
  61. }
  62. async function createApp(ssrContext, config = {}) {
  63. const router = await createRouter(ssrContext, config)
  64. const store = createStore(ssrContext)
  65. // Add this.$router into store actions/mutations
  66. store.$router = router
  67. // Fix SSR caveat https://github.com/nuxt/nuxt.js/issues/3757#issuecomment-414689141
  68. store.registerModule = registerModule
  69. // Create Root instance
  70. // here we inject the router and store to all child components,
  71. // making them available everywhere as `this.$router` and `this.$store`.
  72. const app = {
  73. head: {"title":"商城","meta":[{"name":"renderer","content":"webkit"},{"name":"force-rendering","content":"webkit"},{"http-equiv":"X-UA-Compatible","content":"IE=Edge,chrome=1"},{"name":"baidu-site-verification","content":"mREHhDF8nW"},{"charset":"utf-8"},{"name":"format-detection","content":"telephone=no"},{"name":"viewport","content":"width=device-width, initial-scale=1.0, user-scalable=no"}],"link":[{"rel":"shortcut icon","type":"image\u002Fx-icon","href":"\u002Ffavicon.ico"},{"rel":"stylesheet","href":"\u002F\u002Fat.alicdn.com\u002Ft\u002Ffont_432132_ga7x83fcdac.css"}],"script":[{"src":"https:\u002F\u002Fwebapi.amap.com\u002Fmaps?v=1.4.15&key=2835a3407962cbefaeb29192951fda6e&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder"}],"style":[]},
  74. store,
  75. router,
  76. nuxt: {
  77. defaultTransition,
  78. transitions: [defaultTransition],
  79. setTransitions (transitions) {
  80. if (!Array.isArray(transitions)) {
  81. transitions = [transitions]
  82. }
  83. transitions = transitions.map((transition) => {
  84. if (!transition) {
  85. transition = defaultTransition
  86. } else if (typeof transition === 'string') {
  87. transition = Object.assign({}, defaultTransition, { name: transition })
  88. } else {
  89. transition = Object.assign({}, defaultTransition, transition)
  90. }
  91. return transition
  92. })
  93. this.$options.nuxt.transitions = transitions
  94. return transitions
  95. },
  96. err: null,
  97. dateErr: null,
  98. error (err) {
  99. err = err || null
  100. app.context._errored = Boolean(err)
  101. err = err ? normalizeError(err) : null
  102. let nuxt = app.nuxt // to work with @vue/composition-api, see https://github.com/nuxt/nuxt.js/issues/6517#issuecomment-573280207
  103. if (this) {
  104. nuxt = this.nuxt || this.$options.nuxt
  105. }
  106. nuxt.dateErr = Date.now()
  107. nuxt.err = err
  108. // Used in src/server.js
  109. if (ssrContext) {
  110. ssrContext.nuxt.error = err
  111. }
  112. return err
  113. }
  114. },
  115. ...App
  116. }
  117. // Make app available into store via this.app
  118. store.app = app
  119. const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  120. // Resolve route
  121. let route
  122. if (ssrContext) {
  123. route = router.resolve(ssrContext.url).route
  124. } else {
  125. const path = getLocation(router.options.base, router.options.mode)
  126. route = router.resolve(path).route
  127. }
  128. // Set context to app.context
  129. await setContext(app, {
  130. store,
  131. route,
  132. next,
  133. error: app.nuxt.error.bind(app),
  134. payload: ssrContext ? ssrContext.payload : undefined,
  135. req: ssrContext ? ssrContext.req : undefined,
  136. res: ssrContext ? ssrContext.res : undefined,
  137. beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined,
  138. ssrContext
  139. })
  140. function inject(key, value) {
  141. if (!key) {
  142. throw new Error('inject(key, value) has no key provided')
  143. }
  144. if (value === undefined) {
  145. throw new Error(`inject('${key}', value) has no value provided`)
  146. }
  147. key = '$' + key
  148. // Add into app
  149. app[key] = value
  150. // Add into context
  151. if (!app.context[key]) {
  152. app.context[key] = value
  153. }
  154. // Add into store
  155. store[key] = app[key]
  156. // Check if plugin not already installed
  157. const installKey = '__nuxt_' + key + '_installed__'
  158. if (Vue[installKey]) {
  159. return
  160. }
  161. Vue[installKey] = true
  162. // Call Vue.use() to install the plugin into vm
  163. Vue.use(() => {
  164. if (!Object.prototype.hasOwnProperty.call(Vue.prototype, key)) {
  165. Object.defineProperty(Vue.prototype, key, {
  166. get () {
  167. return this.$root.$options[key]
  168. }
  169. })
  170. }
  171. })
  172. }
  173. // Inject runtime config as $config
  174. inject('config', config)
  175. if (process.client) {
  176. // Replace store state before plugins execution
  177. if (window.__NUXT__ && window.__NUXT__.state) {
  178. store.replaceState(window.__NUXT__.state)
  179. }
  180. }
  181. // Add enablePreview(previewData = {}) in context for plugins
  182. if (process.static && process.client) {
  183. app.context.enablePreview = function (previewData = {}) {
  184. app.previewData = Object.assign({}, previewData)
  185. inject('preview', previewData)
  186. }
  187. }
  188. // Plugin execution
  189. if (typeof nuxt_plugin_plugin_666bcd44 === 'function') {
  190. await nuxt_plugin_plugin_666bcd44(app.context, inject)
  191. }
  192. if (typeof nuxt_plugin_axios_939e3d18 === 'function') {
  193. await nuxt_plugin_axios_939e3d18(app.context, inject)
  194. }
  195. if (typeof nuxt_plugin_elementui_d905880e === 'function') {
  196. await nuxt_plugin_elementui_d905880e(app.context, inject)
  197. }
  198. if (typeof nuxt_plugin_axios_2228ef02 === 'function') {
  199. await nuxt_plugin_axios_2228ef02(app.context, inject)
  200. }
  201. if (typeof nuxt_plugin_router_3f7e063d === 'function') {
  202. await nuxt_plugin_router_3f7e063d(app.context, inject)
  203. }
  204. if (process.client && typeof nuxt_plugin_videoplayer_233ef0a8 === 'function') {
  205. await nuxt_plugin_videoplayer_233ef0a8(app.context, inject)
  206. }
  207. if (process.client && typeof nuxt_plugin_vueLazyLoad_0ac1aa32 === 'function') {
  208. await nuxt_plugin_vueLazyLoad_0ac1aa32(app.context, inject)
  209. }
  210. if (process.client && typeof nuxt_plugin_fun_2b54370c === 'function') {
  211. await nuxt_plugin_fun_2b54370c(app.context, inject)
  212. }
  213. // Lock enablePreview in context
  214. if (process.static && process.client) {
  215. app.context.enablePreview = function () {
  216. console.warn('You cannot call enablePreview() outside a plugin.')
  217. }
  218. }
  219. // Wait for async component to be resolved first
  220. await new Promise((resolve, reject) => {
  221. // Ignore 404s rather than blindly replacing URL in browser
  222. if (process.client) {
  223. const { route } = router.resolve(app.context.route.fullPath)
  224. if (!route.matched.length) {
  225. return resolve()
  226. }
  227. }
  228. router.replace(app.context.route.fullPath, resolve, (err) => {
  229. // https://github.com/vuejs/vue-router/blob/v3.4.3/src/util/errors.js
  230. if (!err._isRouter) return reject(err)
  231. if (err.type !== 2 /* NavigationFailureType.redirected */) return resolve()
  232. // navigated to a different route in router guard
  233. const unregister = router.afterEach(async (to, from) => {
  234. if (process.server && ssrContext && ssrContext.url) {
  235. ssrContext.url = to.fullPath
  236. }
  237. app.context.route = await getRouteData(to)
  238. app.context.params = to.params || {}
  239. app.context.query = to.query || {}
  240. unregister()
  241. resolve()
  242. })
  243. })
  244. })
  245. return {
  246. store,
  247. app,
  248. router
  249. }
  250. }
  251. export { createApp, NuxtError }