UNPKG

4.95 kBJavaScriptView Raw
1import Vue from 'vue'
2import Router from 'vue-router'
3import { interopDefault } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
4import scrollBehavior from './router.scrollBehavior.js'
5
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: ' + 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}
70const _components = []
71const _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
78 if (splitChunks.pages) {
79 return `const ${name} = () => interopDefault(import('${path}' /* webpackChunkName: "${chunkName}" */))`
80 } else {
81 return `import ${name} from '${path}'`
82 }
83}).join('\n')%>
84
85// TODO: remove in Nuxt 3
86const emptyFn = () => {}
87const originalPush = Router.prototype.push
88Router.prototype.push = function push (location, onComplete = emptyFn, onAbort) {
89 return originalPush.call(this, location, onComplete, onAbort)
90}
91
92Vue.use(Router)
93
94export const routerOptions = {
95 mode: '<%= router.mode %>',
96 base: decodeURI('<%= router.base %>'),
97 linkActiveClass: '<%= router.linkActiveClass %>',
98 linkExactActiveClass: '<%= router.linkExactActiveClass %>',
99 scrollBehavior,
100 <%= isTest ? '/* eslint-disable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
101 routes: [<%= _routes %>],
102 <%= isTest ? '/* eslint-enable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
103 <% if (router.parseQuery) { %>parseQuery: <%= serializeFunction(router.parseQuery) %>,<% } %>
104 <% if (router.stringifyQuery) { %>stringifyQuery: <%= serializeFunction(router.stringifyQuery) %>,<% } %>
105 fallback: <%= router.fallback %>
106}
107
108export function createRouter () {
109 return new Router(routerOptions)
110}
111
\No newline at end of file