1 | import Vue from 'vue'
|
2 | import Router from 'vue-router'
|
3 | import { interopDefault } from './utils'
|
4 |
|
5 | <% function recursiveRoutes(routes, tab, components, indentCount) {
|
6 | let res = ''
|
7 | const baseIndent = tab.repeat(indentCount)
|
8 | const firstIndent = '\n' + tab.repeat(indentCount + 1)
|
9 | const nextIndent = ',' + firstIndent
|
10 | routes.forEach((route, i) => {
|
11 | route._name = '_' + hash(route.component)
|
12 | components.push({ _name: route._name, component: route.component, name: route.name, chunkName: route.chunkName })
|
13 |
|
14 | res += '{'
|
15 | res += firstIndent + 'path: ' + JSON.stringify(route.path)
|
16 | res += (route.component) ? nextIndent + 'component: ' + (splitChunks.pages ? route._name : `() => ${route._name}.default || ${route._name}`) : ''
|
17 | res += (route.redirect) ? nextIndent + 'redirect: ' + JSON.stringify(route.redirect) : ''
|
18 | res += (route.meta) ? nextIndent + 'meta: ' + JSON.stringify(route.meta) : ''
|
19 | res += (typeof route.props !== 'undefined') ? nextIndent + 'props: ' + (typeof route.props === 'function' ? serialize(route.props) : JSON.stringify(route.props)) : ''
|
20 | res += (typeof route.caseSensitive !== 'undefined') ? nextIndent + 'caseSensitive: ' + JSON.stringify(route.caseSensitive) : ''
|
21 | res += (route.alias) ? nextIndent + 'alias: ' + JSON.stringify(route.alias) : ''
|
22 | res += (route.pathToRegexpOptions) ? nextIndent + 'pathToRegexpOptions: ' + JSON.stringify(route.pathToRegexpOptions) : ''
|
23 | res += (route.name) ? nextIndent + 'name: ' + JSON.stringify(route.name) : ''
|
24 | if (route.beforeEnter) {
|
25 | if(isTest) { res += ',\n/* eslint-disable indent, semi */' }
|
26 | res += (isTest ? firstIndent : nextIndent) + 'beforeEnter: ' + serialize(route.beforeEnter)
|
27 | if(isTest) { res += firstIndent + '/* eslint-enable indent, semi */' }
|
28 | }
|
29 | res += (route.children) ? nextIndent + 'children: [' + recursiveRoutes(routes[i].children, tab, components, indentCount + 1) + ']' : ''
|
30 | res += '\n' + baseIndent + '}' + (i + 1 === routes.length ? '' : ', ')
|
31 | })
|
32 | return res
|
33 | }
|
34 | const _components = []
|
35 | const _routes = recursiveRoutes(router.routes, ' ', _components, 2)
|
36 | %><%= uniqBy(_components, '_name').map((route) => {
|
37 | if (!route.component) return ''
|
38 | const path = relativeToBuild(route.component)
|
39 | const chunkName = wChunk(route.chunkName)
|
40 | const name = route._name
|
41 |
|
42 | if (splitChunks.pages) {
|
43 | return `const ${name} = () => interopDefault(import('${path}' /* webpackChunkName: "${chunkName}" */))`
|
44 | } else {
|
45 | return `import ${name} from '${path}'`
|
46 | }
|
47 | }).join('\n')%>
|
48 |
|
49 | Vue.use(Router)
|
50 |
|
51 | <% if (router.scrollBehavior) { %>
|
52 | const scrollBehavior = <%= serialize(router.scrollBehavior).replace(/scrollBehavior\s*\(/, 'function(').replace('function function', 'function') %>
|
53 | <% } else { %>
|
54 | if (process.client || process.browser) {
|
55 | <% if (isDev) { %>
|
56 | if (process.browser) {
|
57 | console.warn('process.browser is deprecated, use process.client instead.')
|
58 | }
|
59 | <% } %>
|
60 | window.history.scrollRestoration = 'manual'
|
61 | }
|
62 | const scrollBehavior = function (to, from, savedPosition) {
|
63 | // if the returned position is falsy or an empty object,
|
64 | // will retain current scroll position.
|
65 | let position = false
|
66 |
|
67 | // if no children detected
|
68 | if (to.matched.length < 2) {
|
69 | // scroll to the top of the page
|
70 | position = { x: 0, y: 0 }
|
71 | } else if (to.matched.some(r => r.components.default.options.scrollToTop)) {
|
72 | // if one of the children has scrollToTop option set to true
|
73 | position = { x: 0, y: 0 }
|
74 | }
|
75 |
|
76 | // savedPosition is only available for popstate navigations (back button)
|
77 | if (savedPosition) {
|
78 | position = savedPosition
|
79 | }
|
80 |
|
81 | return new Promise((resolve) => {
|
82 | // wait for the out transition to complete (if necessary)
|
83 | window.<%= globals.nuxt %>.$once('triggerScroll', () => {
|
84 | // coords will be used if no selector is provided,
|
85 | // or if the selector didn't match any element.
|
86 | if (to.hash) {
|
87 | let hash = to.hash
|
88 | // CSS.escape() is not supported with IE and Edge.
|
89 | if (typeof window.CSS !== 'undefined' && typeof window.CSS.escape !== 'undefined') {
|
90 | hash = '#' + window.CSS.escape(hash.substr(1))
|
91 | }
|
92 | try {
|
93 | if (document.querySelector(hash)) {
|
94 | // scroll to anchor by returning the selector
|
95 | position = { selector: hash }
|
96 | }
|
97 | } catch (e) {
|
98 | console.warn('Failed to save scroll position. Please add CSS.escape() polyfill (https://github.com/mathiasbynens/CSS.escape).')
|
99 | }
|
100 | }
|
101 | resolve(position)
|
102 | })
|
103 | })
|
104 | }
|
105 | <% } %>
|
106 |
|
107 | export function createRouter() {
|
108 | return new Router({
|
109 | mode: '<%= router.mode %>',
|
110 | base: '<%= router.base %>',
|
111 | linkActiveClass: '<%= router.linkActiveClass %>',
|
112 | linkExactActiveClass: '<%= router.linkExactActiveClass %>',
|
113 | scrollBehavior,
|
114 | <%= isTest ? '/* eslint-disable quotes */' : '' %>
|
115 | routes: [<%= _routes %>],
|
116 | <%= isTest ? '/* eslint-enable quotes */' : '' %>
|
117 | <% if (router.parseQuery) { %>parseQuery: <%= serialize(router.parseQuery).replace('parseQuery(', 'function(') %>,<% } %>
|
118 | <% if (router.stringifyQuery) { %>stringifyQuery: <%= serialize(router.stringifyQuery).replace('stringifyQuery(', 'function(') %>,<% } %>
|
119 | fallback: <%= router.fallback %>
|
120 | })
|
121 | }
|
122 |
|
\ | No newline at end of file |