2021-05-20 11:42:41 +00:00
|
|
|
import { existsSync } from 'fs'
|
2021-07-28 11:35:24 +00:00
|
|
|
import { defineNuxtModule, addTemplate, addPlugin } from '@nuxt/kit'
|
2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-08-11 21:26:47 +00:00
|
|
|
import { distDir } from '../dirs'
|
2021-10-20 18:49:15 +00:00
|
|
|
import { resolveLayouts, resolvePagesRoutes, addComponentToRoutes } from './utils'
|
2021-05-20 11:42:41 +00:00
|
|
|
|
|
|
|
export default defineNuxtModule({
|
|
|
|
name: 'router',
|
|
|
|
setup (_options, nuxt) {
|
|
|
|
const pagesDir = resolve(nuxt.options.srcDir, nuxt.options.dir.pages)
|
2021-08-11 21:26:47 +00:00
|
|
|
const runtimeDir = resolve(distDir, 'pages/runtime')
|
2021-07-28 11:35:24 +00:00
|
|
|
|
|
|
|
// Disable module if pages dir do not exists
|
|
|
|
if (!existsSync(pagesDir)) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2021-11-02 09:39:42 +00:00
|
|
|
// Add $router types
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
references.push({ types: 'vue-router' })
|
|
|
|
})
|
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Regenerate templates when adding or removing pages
|
2021-05-20 11:42:41 +00:00
|
|
|
nuxt.hook('builder:watch', async (event, path) => {
|
2021-06-30 16:32:22 +00:00
|
|
|
const pathPattern = new RegExp(`^(${nuxt.options.dir.pages}|${nuxt.options.dir.layouts})/`)
|
|
|
|
if (event !== 'change' && path.match(pathPattern)) {
|
2021-05-20 11:42:41 +00:00
|
|
|
await nuxt.callHook('builder:generateApp')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
nuxt.hook('app:resolve', (app) => {
|
2021-10-12 12:51:41 +00:00
|
|
|
// Remove default root with Suspense
|
|
|
|
app.rootComponent = resolve(runtimeDir, 'root.vue')
|
|
|
|
// Add default layout for pages
|
|
|
|
if (app.mainComponent.includes('nuxt-welcome')) {
|
|
|
|
app.mainComponent = resolve(runtimeDir, 'app.vue')
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Add router plguin
|
|
|
|
addPlugin(resolve(runtimeDir, 'router'))
|
2021-06-30 16:32:22 +00:00
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Add routes template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'routes.mjs',
|
|
|
|
async getContents () {
|
|
|
|
const routes = await resolvePagesRoutes(nuxt)
|
2021-10-20 18:49:15 +00:00
|
|
|
const serializedRoutes = addComponentToRoutes(routes)
|
2021-07-28 11:35:24 +00:00
|
|
|
return `export default ${JSON.stringify(serializedRoutes, null, 2).replace(/"{(.+)}"/g, '$1')}`
|
|
|
|
}
|
|
|
|
})
|
2021-06-30 16:32:22 +00:00
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Add layouts template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'layouts.mjs',
|
|
|
|
async getContents () {
|
|
|
|
const layouts = await resolveLayouts(nuxt)
|
|
|
|
const layoutsObject = Object.fromEntries(layouts.map(({ name, file }) => {
|
|
|
|
return [name, `{defineAsyncComponent({ suspensible: false, loader: () => import('${file}') })}`]
|
|
|
|
}))
|
|
|
|
return [
|
|
|
|
'import { defineAsyncComponent } from \'vue\'',
|
|
|
|
`export default ${JSON.stringify(layoutsObject, null, 2).replace(/"{(.+)}"/g, '$1')}`
|
|
|
|
].join('\n')
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|