2016-11-07 01:34:58 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import Router from 'vue-router'
|
2020-12-22 17:20:41 +00:00
|
|
|
import { normalizeURL, decode } from 'ufo'
|
2019-01-06 07:56:59 +00:00
|
|
|
import { interopDefault } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
|
2019-07-24 11:35:50 +00:00
|
|
|
import scrollBehavior from './router.scrollBehavior.js'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
<% function recursiveRoutes(routes, tab, components, indentCount) {
|
2019-03-19 10:25:20 +00:00
|
|
|
let res = ''
|
2018-10-24 13:46:06 +00:00
|
|
|
const baseIndent = tab.repeat(indentCount)
|
|
|
|
const firstIndent = '\n' + tab.repeat(indentCount + 1)
|
|
|
|
const nextIndent = ',' + firstIndent
|
2016-12-11 00:46:04 +00:00
|
|
|
routes.forEach((route, i) => {
|
2019-03-19 10:25:20 +00:00
|
|
|
let resMap = ''
|
2018-12-20 15:50:22 +00:00
|
|
|
// If need to handle named views
|
|
|
|
if (route.components) {
|
|
|
|
let _name = '_' + hash(route.components.default)
|
|
|
|
if (splitChunks.pages) {
|
|
|
|
resMap += `${firstIndent}${tab}default: ${_name}`
|
|
|
|
} else {
|
|
|
|
resMap += `${firstIndent}${tab}default: () => ${_name}.default || ${_name}`
|
|
|
|
}
|
|
|
|
for (const k in route.components) {
|
|
|
|
_name = '_' + hash(route.components[k])
|
|
|
|
const component = { _name, component: route.components[k] }
|
|
|
|
if (k === 'default') {
|
|
|
|
components.push({
|
|
|
|
...component,
|
|
|
|
name: route.name,
|
|
|
|
chunkName: route.chunkName
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
components.push({
|
|
|
|
...component,
|
|
|
|
name: `${route.name}-${k}`,
|
|
|
|
chunkName: route.chunkNames[k]
|
|
|
|
})
|
|
|
|
if (splitChunks.pages) {
|
|
|
|
resMap += `${nextIndent}${tab}${k}: ${_name}`
|
|
|
|
} else {
|
|
|
|
resMap += `${nextIndent}${tab}${k}: () => ${_name}.default || ${_name}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
route.component = false
|
|
|
|
} else {
|
|
|
|
route._name = '_' + hash(route.component)
|
|
|
|
components.push({ _name: route._name, component: route.component, name: route.name, chunkName: route.chunkName })
|
|
|
|
}
|
2018-09-01 04:33:28 +00:00
|
|
|
// @see: https://router.vuejs.org/api/#router-construction-options
|
2018-10-24 13:46:06 +00:00
|
|
|
res += '{'
|
2020-12-01 15:01:25 +00:00
|
|
|
res += firstIndent + 'path: ' + JSON.stringify(route.path)
|
2018-12-20 15:50:22 +00:00
|
|
|
res += (route.components) ? nextIndent + 'components: {' + resMap + '\n' + baseIndent + tab + '}' : ''
|
2019-11-19 09:27:54 +00:00
|
|
|
res += (route.component) ? nextIndent + 'component: ' + route._name : ''
|
2018-10-24 13:46:06 +00:00
|
|
|
res += (route.redirect) ? nextIndent + 'redirect: ' + JSON.stringify(route.redirect) : ''
|
|
|
|
res += (route.meta) ? nextIndent + 'meta: ' + JSON.stringify(route.meta) : ''
|
|
|
|
res += (typeof route.props !== 'undefined') ? nextIndent + 'props: ' + (typeof route.props === 'function' ? serialize(route.props) : JSON.stringify(route.props)) : ''
|
|
|
|
res += (typeof route.caseSensitive !== 'undefined') ? nextIndent + 'caseSensitive: ' + JSON.stringify(route.caseSensitive) : ''
|
|
|
|
res += (route.alias) ? nextIndent + 'alias: ' + JSON.stringify(route.alias) : ''
|
|
|
|
res += (route.pathToRegexpOptions) ? nextIndent + 'pathToRegexpOptions: ' + JSON.stringify(route.pathToRegexpOptions) : ''
|
|
|
|
res += (route.name) ? nextIndent + 'name: ' + JSON.stringify(route.name) : ''
|
|
|
|
if (route.beforeEnter) {
|
|
|
|
if(isTest) { res += ',\n/* eslint-disable indent, semi */' }
|
|
|
|
res += (isTest ? firstIndent : nextIndent) + 'beforeEnter: ' + serialize(route.beforeEnter)
|
|
|
|
if(isTest) { res += firstIndent + '/* eslint-enable indent, semi */' }
|
|
|
|
}
|
|
|
|
res += (route.children) ? nextIndent + 'children: [' + recursiveRoutes(routes[i].children, tab, components, indentCount + 1) + ']' : ''
|
|
|
|
res += '\n' + baseIndent + '}' + (i + 1 === routes.length ? '' : ', ')
|
2016-12-11 00:46:04 +00:00
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
2017-07-09 22:40:09 +00:00
|
|
|
const _components = []
|
2019-09-10 09:51:14 +00:00
|
|
|
const _routes = recursiveRoutes(router.routes, ' ', _components, 1)
|
2018-10-24 13:46:06 +00:00
|
|
|
%><%= uniqBy(_components, '_name').map((route) => {
|
2018-03-15 11:56:50 +00:00
|
|
|
if (!route.component) return ''
|
2018-03-13 14:36:49 +00:00
|
|
|
const path = relativeToBuild(route.component)
|
|
|
|
const chunkName = wChunk(route.chunkName)
|
|
|
|
const name = route._name
|
|
|
|
|
2018-03-19 18:40:33 +00:00
|
|
|
if (splitChunks.pages) {
|
2018-10-16 21:56:43 +00:00
|
|
|
return `const ${name} = () => interopDefault(import('${path}' /* webpackChunkName: "${chunkName}" */))`
|
2018-03-13 14:36:49 +00:00
|
|
|
} else {
|
|
|
|
return `import ${name} from '${path}'`
|
|
|
|
}
|
|
|
|
}).join('\n')%>
|
|
|
|
|
2019-11-26 22:41:19 +00:00
|
|
|
const emptyFn = () => {}
|
|
|
|
|
2018-03-13 14:36:49 +00:00
|
|
|
Vue.use(Router)
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2019-08-14 19:38:25 +00:00
|
|
|
export const routerOptions = {
|
|
|
|
mode: '<%= router.mode %>',
|
2020-11-30 22:10:02 +00:00
|
|
|
base: '<%= router.base %>',
|
2019-08-14 19:38:25 +00:00
|
|
|
linkActiveClass: '<%= router.linkActiveClass %>',
|
|
|
|
linkExactActiveClass: '<%= router.linkExactActiveClass %>',
|
|
|
|
scrollBehavior,
|
2019-11-26 22:42:39 +00:00
|
|
|
<%= isTest ? '/* eslint-disable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
|
2019-08-14 19:38:25 +00:00
|
|
|
routes: [<%= _routes %>],
|
2019-11-26 22:42:39 +00:00
|
|
|
<%= isTest ? '/* eslint-enable array-bracket-spacing, quotes, quote-props, object-curly-spacing, key-spacing */' : '' %>
|
2019-08-14 19:38:25 +00:00
|
|
|
<% if (router.parseQuery) { %>parseQuery: <%= serializeFunction(router.parseQuery) %>,<% } %>
|
|
|
|
<% if (router.stringifyQuery) { %>stringifyQuery: <%= serializeFunction(router.stringifyQuery) %>,<% } %>
|
|
|
|
fallback: <%= router.fallback %>
|
|
|
|
}
|
|
|
|
|
2020-12-06 17:32:39 +00:00
|
|
|
function decodeObj(obj) {
|
|
|
|
for (const key in obj) {
|
|
|
|
if (typeof obj[key] === 'string') {
|
2020-12-16 13:12:16 +00:00
|
|
|
obj[key] = decode(obj[key])
|
2020-12-06 17:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:15:59 +00:00
|
|
|
export function createRouter (ssrContext, config) {
|
|
|
|
const base = (config.app && config.app.basePath) || routerOptions.base
|
|
|
|
const router = new Router({ ...routerOptions, base })
|
2020-11-30 22:10:02 +00:00
|
|
|
|
2020-12-20 11:21:20 +00:00
|
|
|
// TODO: remove in Nuxt 3
|
|
|
|
const originalPush = router.push
|
|
|
|
router.push = function push (location, onComplete = emptyFn, onAbort) {
|
|
|
|
return originalPush.call(this, location, onComplete, onAbort)
|
|
|
|
}
|
|
|
|
|
2020-12-06 17:32:39 +00:00
|
|
|
const resolve = router.resolve.bind(router)
|
2020-11-30 22:10:02 +00:00
|
|
|
router.resolve = (to, current, append) => {
|
|
|
|
if (typeof to === 'string') {
|
2020-12-06 17:32:39 +00:00
|
|
|
to = normalizeURL(to)
|
|
|
|
}
|
|
|
|
const r = resolve(to, current, append)
|
|
|
|
if (r && r.resolved && r.resolved.query) {
|
|
|
|
decodeObj(r.resolved.query)
|
2020-11-30 22:10:02 +00:00
|
|
|
}
|
2020-12-06 17:32:39 +00:00
|
|
|
return r
|
2020-11-30 22:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return router
|
2017-05-02 06:57:39 +00:00
|
|
|
}
|