router.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import { normalizeURL, decode } from 'ufo'
  4. import { interopDefault } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
  5. import scrollBehavior from './router.scrollBehavior.js'
  6. <% function recursiveRoutes(routes, tab, components, indentCount) {
  7. let res = ''
  8. const baseIndent = tab.repeat(indentCount)
  9. const firstIndent = '\n' + tab.repeat(indentCount + 1)
  10. const nextIndent = ',' + firstIndent
  11. routes.forEach((route, i) => {
  12. let resMap = ''
  13. // If need to handle named views
  14. if (route.components) {
  15. let _name = '_' + hash(route.components.default)
  16. if (splitChunks.pages) {
  17. resMap += `${firstIndent}${tab}default: ${_name}`
  18. } else {
  19. resMap += `${firstIndent}${tab}default: () => ${_name}.default || ${_name}`
  20. }
  21. for (const k in route.components) {
  22. _name = '_' + hash(route.components[k])
  23. const component = { _name, component: route.components[k] }
  24. if (k === 'default') {
  25. components.push({
  26. ...component,
  27. name: route.name,
  28. chunkName: route.chunkName
  29. })
  30. } else {
  31. components.push({
  32. ...component,
  33. name: `${route.name}-${k}`,
  34. chunkName: route.chunkNames[k]
  35. })
  36. if (splitChunks.pages) {
  37. resMap += `${nextIndent}${tab}${k}: ${_name}`
  38. } else {
  39. resMap += `${nextIndent}${tab}${k}: () => ${_name}.default || ${_name}`
  40. }
  41. }
  42. }
  43. route.component = false
  44. } else {
  45. route._name = '_' + hash(route.component)
  46. components.push({ _name: route._name, component: route.component, name: route.name, chunkName: route.chunkName })
  47. }
  48. // @see: https://router.vuejs.org/api/#router-construction-options
  49. res += '{'
  50. res += firstIndent + 'path: ' + JSON.stringify(route.path)
  51. res += (route.components) ? nextIndent + 'components: {' + resMap + '\n' + baseIndent + tab + '}' : ''
  52. res += (route.component) ? nextIndent + 'component: ' + route._name : ''
  53. res += (route.redirect) ? nextIndent + 'redirect: ' + (typeof route.redirect === 'function' ? serialize(route.redirect) : JSON.stringify(route.redirect)) : ''
  54. res += (route.meta) ? nextIndent + 'meta: ' + JSON.stringify(route.meta) : ''
  55. res += (typeof route.props !== 'undefined') ? nextIndent + 'props: ' + (typeof route.props === 'function' ? serialize(route.props) : JSON.stringify(route.props)) : ''
  56. res += (typeof route.caseSensitive !== 'undefined') ? nextIndent + 'caseSensitive: ' + JSON.stringify(route.caseSensitive) : ''
  57. res += (route.alias) ? nextIndent + 'alias: ' + JSON.stringify(route.alias) : ''
  58. res += (route.pathToRegexpOptions) ? nextIndent + 'pathToRegexpOptions: ' + JSON.stringify(route.pathToRegexpOptions) : ''
  59. res += (route.name) ? nextIndent + 'name: ' + JSON.stringify(route.name) : ''
  60. if (route.beforeEnter) {
  61. if(isTest) { res += ',\n/* eslint-disable indent, semi */' }
  62. res += (isTest ? firstIndent : nextIndent) + 'beforeEnter: ' + serialize(route.beforeEnter)
  63. if(isTest) { res += firstIndent + '/* eslint-enable indent, semi */' }
  64. }
  65. res += (route.children) ? nextIndent + 'children: [' + recursiveRoutes(routes[i].children, tab, components, indentCount + 1) + ']' : ''
  66. res += '\n' + baseIndent + '}' + (i + 1 === routes.length ? '' : ', ')
  67. })
  68. return res
  69. }
  70. const _components = []
  71. const _routes = recursiveRoutes(router.routes, ' ', _components, 1)
  72. %><%= uniqBy(_components, '_name').map((route) => {
  73. if (!route.component) return ''
  74. const path = relativeToBuild(route.component)
  75. const chunkName = wChunk(route.chunkName)
  76. const name = route._name
  77. if (splitChunks.pages) {
  78. return `const ${name} = () => interopDefault(import('${path}' /* webpackChunkName: "${chunkName}" */))`
  79. } else {
  80. return `import ${name} from '${path}'`
  81. }
  82. }).join('\n')%>
  83. const emptyFn = () => {}
  84. Vue.use(Router)
  85. export const routerOptions = {
  86. mode: '<%= router.mode %>',
  87. base: '<%= router.base %>',
  88. linkActiveClass: '<%= router.linkActiveClass %>',
  89. linkExactActiveClass: '<%= router.linkExactActiveClass %>',
  90. scrollBehavior,
  91. <%= isTest ? '/* eslint-disable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
  92. routes: [<%= _routes %>],
  93. <%= isTest ? '/* eslint-enable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
  94. <% if (router.parseQuery) { %>parseQuery: <%= serializeFunction(router.parseQuery) %>,<% } %>
  95. <% if (router.stringifyQuery) { %>stringifyQuery: <%= serializeFunction(router.stringifyQuery) %>,<% } %>
  96. fallback: <%= router.fallback %>
  97. }
  98. export function createRouter (ssrContext, config) {
  99. const base = (config._app && config._app.basePath) || routerOptions.base
  100. const router = new Router({ ...routerOptions, base })
  101. // TODO: remove in Nuxt 3
  102. const originalPush = router.push
  103. router.push = function push (location, onComplete = emptyFn, onAbort) {
  104. return originalPush.call(this, location, onComplete, onAbort)
  105. }
  106. const resolve = router.resolve.bind(router)
  107. router.resolve = (to, current, append) => {
  108. if (typeof to === 'string') {
  109. to = normalizeURL(to)
  110. }
  111. return resolve(to, current, append)
  112. }
  113. return router
  114. }