index.js 11 KB

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