index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_axios_4eb16350 from 'nuxt_plugin_axios_4eb16350' // Source: .\\axios.js (mode: 'all')
  15. import nuxt_plugin_router_1ee1ef52 from 'nuxt_plugin_router_1ee1ef52' // Source: .\\router.js (mode: 'all')
  16. import nuxt_plugin_common_9f35b6a0 from 'nuxt_plugin_common_9f35b6a0' // Source: ..\\plugins\\common (mode: 'all')
  17. import nuxt_plugin_element_7f070652 from 'nuxt_plugin_element_7f070652' // Source: ..\\plugins\\element (mode: 'all')
  18. import nuxt_plugin_nuxtAxios_67b4924e from 'nuxt_plugin_nuxtAxios_67b4924e' // Source: ..\\plugins\\nuxtAxios (mode: 'all')
  19. import nuxt_plugin_deviceType_39fdd216 from 'nuxt_plugin_deviceType_39fdd216' // Source: ..\\plugins\\deviceType (mode: 'all')
  20. import nuxt_plugin_vant_61b705b6 from 'nuxt_plugin_vant_61b705b6' // Source: ..\\plugins\\vant (mode: 'all')
  21. import nuxt_plugin_router_6bfcdbe4 from 'nuxt_plugin_router_6bfcdbe4' // Source: ..\\plugins\\router (mode: 'all')
  22. import nuxt_plugin_vant_31825081 from 'nuxt_plugin_vant_31825081' // Source: ..\\plugins\\vant.js (mode: 'client')
  23. import nuxt_plugin_rem_9edb8816 from 'nuxt_plugin_rem_9edb8816' // Source: ..\\plugins\\rem (mode: 'client')
  24. import nuxt_plugin_vconsole_6a13f1a6 from 'nuxt_plugin_vconsole_6a13f1a6' // Source: ..\\plugins\\vconsole (mode: 'client')
  25. import nuxt_plugin_vueswiper_9e702eda from 'nuxt_plugin_vueswiper_9e702eda' // Source: ..\\plugins\\vue-swiper.js (mode: 'client')
  26. import nuxt_plugin_g2_577ffbf2 from 'nuxt_plugin_g2_577ffbf2' // Source: ..\\plugins\\g2.js (mode: 'client')
  27. import nuxt_plugin_vueLazyLoad_58be327b from 'nuxt_plugin_vueLazyLoad_58be327b' // Source: ..\\plugins\\vueLazyLoad (mode: 'client')
  28. import nuxt_plugin_cropper_24b16057 from 'nuxt_plugin_cropper_24b16057' // Source: ..\\plugins\\cropper (mode: 'client')
  29. import nuxt_plugin_directive_d4d4a366 from 'nuxt_plugin_directive_d4d4a366' // Source: ..\\plugins\\directive.js (mode: 'client')
  30. import nuxt_plugin_cnzz_5b46bf07 from 'nuxt_plugin_cnzz_5b46bf07' // Source: ..\\plugins\\cnzz.js (mode: 'client')
  31. // Component: <ClientOnly>
  32. Vue.component(ClientOnly.name, ClientOnly)
  33. // TODO: Remove in Nuxt 3: <NoSsr>
  34. Vue.component(NoSsr.name, {
  35. ...NoSsr,
  36. render (h, ctx) {
  37. if (process.client && !NoSsr._warned) {
  38. NoSsr._warned = true
  39. console.warn('<no-ssr> has been deprecated and will be removed in Nuxt 3, please use <client-only> instead')
  40. }
  41. return NoSsr.render(h, ctx)
  42. }
  43. })
  44. // Component: <NuxtChild>
  45. Vue.component(NuxtChild.name, NuxtChild)
  46. Vue.component('NChild', NuxtChild)
  47. // Component NuxtLink is imported in server.js or client.js
  48. // Component: <Nuxt>
  49. Vue.component(Nuxt.name, Nuxt)
  50. Vue.use(Meta, {"keyName":"head","attribute":"data-n-head","ssrAttribute":"data-n-head-ssr","tagIDKeyName":"hid"})
  51. const defaultTransition = {"name":"page","mode":"out-in","appear":false,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  52. const originalRegisterModule = Vuex.Store.prototype.registerModule
  53. const baseStoreOptions = { preserveState: process.client }
  54. function registerModule (path, rawModule, options = {}) {
  55. return originalRegisterModule.call(this, path, rawModule, { ...baseStoreOptions, ...options })
  56. }
  57. async function createApp(ssrContext, config = {}) {
  58. const router = await createRouter(ssrContext)
  59. const store = createStore(ssrContext)
  60. // Add this.$router into store actions/mutations
  61. store.$router = router
  62. // Fix SSR caveat https://github.com/nuxt/nuxt.js/issues/3757#issuecomment-414689141
  63. store.registerModule = registerModule
  64. // Create Root instance
  65. // here we inject the router and store to all child components,
  66. // making them available everywhere as `this.$router` and `this.$store`.
  67. const app = {
  68. head: {"title":"程序员客栈","meta":[{"charset":"utf-8"},{"name":"viewport","content":"width=device-width, initial-scale=1"},{"name":"applicable-device","content":"pc, mobile "},{"http-equiv":"X-UA-Compatible","content":"IE=edge,chrome=1"},{"http-equiv":"Cache-Control","content":"no-transform"},{"http-equiv":"Cache-Control","content":"no-siteapp"}],"link":[{"rel":"shortcut icon","type":"image\u002Fvnd.microsoft.icon","href":"https:\u002F\u002Fstacdn.proginn.com\u002Ffavicon_new.ico"}],"style":[],"script":[]},
  69. store,
  70. router,
  71. nuxt: {
  72. defaultTransition,
  73. transitions: [defaultTransition],
  74. setTransitions (transitions) {
  75. if (!Array.isArray(transitions)) {
  76. transitions = [transitions]
  77. }
  78. transitions = transitions.map((transition) => {
  79. if (!transition) {
  80. transition = defaultTransition
  81. } else if (typeof transition === 'string') {
  82. transition = Object.assign({}, defaultTransition, { name: transition })
  83. } else {
  84. transition = Object.assign({}, defaultTransition, transition)
  85. }
  86. return transition
  87. })
  88. this.$options.nuxt.transitions = transitions
  89. return transitions
  90. },
  91. err: null,
  92. dateErr: null,
  93. error (err) {
  94. err = err || null
  95. app.context._errored = Boolean(err)
  96. err = err ? normalizeError(err) : null
  97. let nuxt = app.nuxt // to work with @vue/composition-api, see https://github.com/nuxt/nuxt.js/issues/6517#issuecomment-573280207
  98. if (this) {
  99. nuxt = this.nuxt || this.$options.nuxt
  100. }
  101. nuxt.dateErr = Date.now()
  102. nuxt.err = err
  103. // Used in src/server.js
  104. if (ssrContext) {
  105. ssrContext.nuxt.error = err
  106. }
  107. return err
  108. }
  109. },
  110. ...App
  111. }
  112. // Make app available into store via this.app
  113. store.app = app
  114. const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  115. // Resolve route
  116. let route
  117. if (ssrContext) {
  118. route = router.resolve(ssrContext.url).route
  119. } else {
  120. const path = getLocation(router.options.base, router.options.mode)
  121. route = router.resolve(path).route
  122. }
  123. // Set context to app.context
  124. await setContext(app, {
  125. store,
  126. route,
  127. next,
  128. error: app.nuxt.error.bind(app),
  129. payload: ssrContext ? ssrContext.payload : undefined,
  130. req: ssrContext ? ssrContext.req : undefined,
  131. res: ssrContext ? ssrContext.res : undefined,
  132. beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined,
  133. ssrContext
  134. })
  135. function inject(key, value) {
  136. if (!key) {
  137. throw new Error('inject(key, value) has no key provided')
  138. }
  139. if (value === undefined) {
  140. throw new Error(`inject('${key}', value) has no value provided`)
  141. }
  142. key = '$' + key
  143. // Add into app
  144. app[key] = value
  145. // Add into context
  146. if (!app.context[key]) {
  147. app.context[key] = value
  148. }
  149. // Add into store
  150. store[key] = app[key]
  151. // Check if plugin not already installed
  152. const installKey = '__nuxt_' + key + '_installed__'
  153. if (Vue[installKey]) {
  154. return
  155. }
  156. Vue[installKey] = true
  157. // Call Vue.use() to install the plugin into vm
  158. Vue.use(() => {
  159. if (!Object.prototype.hasOwnProperty.call(Vue.prototype, key)) {
  160. Object.defineProperty(Vue.prototype, key, {
  161. get () {
  162. return this.$root.$options[key]
  163. }
  164. })
  165. }
  166. })
  167. }
  168. // Inject runtime config as $config
  169. inject('config', config)
  170. if (process.client) {
  171. // Replace store state before plugins execution
  172. if (window.__NUXT__ && window.__NUXT__.state) {
  173. store.replaceState(window.__NUXT__.state)
  174. }
  175. }
  176. // Add enablePreview(previewData = {}) in context for plugins
  177. if (process.static && process.client) {
  178. app.context.enablePreview = function (previewData = {}) {
  179. app.previewData = Object.assign({}, previewData)
  180. inject('preview', previewData)
  181. }
  182. }
  183. // Plugin execution
  184. if (typeof nuxt_plugin_axios_4eb16350 === 'function') {
  185. await nuxt_plugin_axios_4eb16350(app.context, inject)
  186. }
  187. if (typeof nuxt_plugin_router_1ee1ef52 === 'function') {
  188. await nuxt_plugin_router_1ee1ef52(app.context, inject)
  189. }
  190. if (typeof nuxt_plugin_common_9f35b6a0 === 'function') {
  191. await nuxt_plugin_common_9f35b6a0(app.context, inject)
  192. }
  193. if (typeof nuxt_plugin_element_7f070652 === 'function') {
  194. await nuxt_plugin_element_7f070652(app.context, inject)
  195. }
  196. if (typeof nuxt_plugin_nuxtAxios_67b4924e === 'function') {
  197. await nuxt_plugin_nuxtAxios_67b4924e(app.context, inject)
  198. }
  199. if (typeof nuxt_plugin_deviceType_39fdd216 === 'function') {
  200. await nuxt_plugin_deviceType_39fdd216(app.context, inject)
  201. }
  202. if (typeof nuxt_plugin_vant_61b705b6 === 'function') {
  203. await nuxt_plugin_vant_61b705b6(app.context, inject)
  204. }
  205. if (typeof nuxt_plugin_router_6bfcdbe4 === 'function') {
  206. await nuxt_plugin_router_6bfcdbe4(app.context, inject)
  207. }
  208. if (process.client && typeof nuxt_plugin_vant_31825081 === 'function') {
  209. await nuxt_plugin_vant_31825081(app.context, inject)
  210. }
  211. if (process.client && typeof nuxt_plugin_rem_9edb8816 === 'function') {
  212. await nuxt_plugin_rem_9edb8816(app.context, inject)
  213. }
  214. if (process.client && typeof nuxt_plugin_vconsole_6a13f1a6 === 'function') {
  215. await nuxt_plugin_vconsole_6a13f1a6(app.context, inject)
  216. }
  217. if (process.client && typeof nuxt_plugin_vueswiper_9e702eda === 'function') {
  218. await nuxt_plugin_vueswiper_9e702eda(app.context, inject)
  219. }
  220. if (process.client && typeof nuxt_plugin_g2_577ffbf2 === 'function') {
  221. await nuxt_plugin_g2_577ffbf2(app.context, inject)
  222. }
  223. if (process.client && typeof nuxt_plugin_vueLazyLoad_58be327b === 'function') {
  224. await nuxt_plugin_vueLazyLoad_58be327b(app.context, inject)
  225. }
  226. if (process.client && typeof nuxt_plugin_cropper_24b16057 === 'function') {
  227. await nuxt_plugin_cropper_24b16057(app.context, inject)
  228. }
  229. if (process.client && typeof nuxt_plugin_directive_d4d4a366 === 'function') {
  230. await nuxt_plugin_directive_d4d4a366(app.context, inject)
  231. }
  232. if (process.client && typeof nuxt_plugin_cnzz_5b46bf07 === 'function') {
  233. await nuxt_plugin_cnzz_5b46bf07(app.context, inject)
  234. }
  235. // Lock enablePreview in context
  236. if (process.static && process.client) {
  237. app.context.enablePreview = function () {
  238. console.warn('You cannot call enablePreview() outside a plugin.')
  239. }
  240. }
  241. // If server-side, wait for async component to be resolved first
  242. if (process.server && ssrContext && ssrContext.url) {
  243. await new Promise((resolve, reject) => {
  244. router.push(ssrContext.url, resolve, (err) => {
  245. // https://github.com/vuejs/vue-router/blob/v3.4.3/src/util/errors.js
  246. if (!err._isRouter) return reject(err)
  247. if (err.type !== 2 /* NavigationFailureType.redirected */) return resolve()
  248. // navigated to a different route in router guard
  249. const unregister = router.afterEach(async (to, from) => {
  250. ssrContext.url = to.fullPath
  251. app.context.route = await getRouteData(to)
  252. app.context.params = to.params || {}
  253. app.context.query = to.query || {}
  254. unregister()
  255. resolve()
  256. })
  257. })
  258. })
  259. }
  260. return {
  261. store,
  262. app,
  263. router
  264. }
  265. }
  266. export { createApp, NuxtError }