2021-05-20 11:42:41 +00:00
|
|
|
import { existsSync } from 'fs'
|
2022-01-17 18:27:23 +00:00
|
|
|
import { defineNuxtModule, addTemplate, addPlugin, templateUtils, addVitePlugin, addWebpackPlugin } 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'
|
2022-01-25 12:29:11 +00:00
|
|
|
import { resolveLayouts, resolvePagesRoutes, normalizeRoutes, resolveMiddleware, getImportName } from './utils'
|
2022-01-17 18:27:23 +00:00
|
|
|
import { TransformMacroPlugin, TransformMacroPluginOptions } from './macros'
|
2021-05-20 11:42:41 +00:00
|
|
|
|
|
|
|
export default defineNuxtModule({
|
2022-01-05 18:09:53 +00:00
|
|
|
meta: {
|
|
|
|
name: 'router'
|
|
|
|
},
|
2021-05-20 11:42:41 +00:00
|
|
|
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
|
|
|
// 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-12-17 09:15:03 +00:00
|
|
|
nuxt.hook('autoImports:extend', (autoImports) => {
|
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
2022-01-25 12:29:11 +00:00
|
|
|
const composables = [
|
|
|
|
'useRouter',
|
|
|
|
'useRoute',
|
|
|
|
'defineNuxtRouteMiddleware',
|
|
|
|
'definePageMeta',
|
|
|
|
'navigateTo',
|
|
|
|
'abortNavigation',
|
|
|
|
'addRouteMiddleware'
|
|
|
|
]
|
|
|
|
for (const composable of composables) {
|
|
|
|
autoImports.push({ name: composable, as: composable, from: composablesFile })
|
|
|
|
}
|
2021-12-17 09:15:03 +00:00
|
|
|
})
|
|
|
|
|
2022-01-17 18:27:23 +00:00
|
|
|
// Extract macros from pages
|
|
|
|
const macroOptions: TransformMacroPluginOptions = {
|
2022-01-19 18:07:54 +00:00
|
|
|
dev: nuxt.options.dev,
|
2022-01-17 18:27:23 +00:00
|
|
|
macros: {
|
|
|
|
definePageMeta: 'meta'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addVitePlugin(TransformMacroPlugin.vite(macroOptions))
|
|
|
|
addWebpackPlugin(TransformMacroPlugin.webpack(macroOptions))
|
|
|
|
|
2021-11-17 11:28:36 +00:00
|
|
|
// Add router plugin
|
2021-07-28 11:35:24 +00:00
|
|
|
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 () {
|
2021-11-09 10:16:23 +00:00
|
|
|
const pages = await resolvePagesRoutes(nuxt)
|
|
|
|
await nuxt.callHook('pages:extend', pages)
|
2022-01-17 18:27:23 +00:00
|
|
|
const { routes: serializedRoutes, imports } = normalizeRoutes(pages)
|
|
|
|
return [...imports, `export default ${templateUtils.serialize(serializedRoutes)}`].join('\n')
|
2021-07-28 11:35:24 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-30 16:32:22 +00:00
|
|
|
|
2022-01-25 12:29:11 +00:00
|
|
|
// Add middleware template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'middleware.mjs',
|
|
|
|
async getContents () {
|
|
|
|
const middleware = await resolveMiddleware()
|
|
|
|
await nuxt.callHook('pages:middleware:extend', middleware)
|
|
|
|
const middlewareObject = Object.fromEntries(middleware.map(mw => [mw.name, `{() => import('${mw.path}')}`]))
|
|
|
|
const globalMiddleware = middleware.filter(mw => mw.global)
|
|
|
|
return [
|
|
|
|
...globalMiddleware.map(mw => `import ${getImportName(mw.name)} from '${mw.path}'`),
|
|
|
|
`export const globalMiddleware = [${globalMiddleware.map(mw => getImportName(mw.name)).join(', ')}]`,
|
|
|
|
`export const namedMiddleware = ${templateUtils.serialize(middlewareObject)}`
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
addTemplate({
|
|
|
|
filename: 'middleware.d.ts',
|
|
|
|
write: true,
|
|
|
|
getContents: async () => {
|
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
|
|
|
const middleware = await resolveMiddleware()
|
|
|
|
return [
|
|
|
|
'import type { NavigationGuard } from \'vue-router\'',
|
|
|
|
`export type MiddlewareKey = ${middleware.map(mw => `"${mw.name}"`).join(' | ') || 'string'}`,
|
|
|
|
`declare module '${composablesFile}' {`,
|
|
|
|
' interface PageMeta {',
|
|
|
|
' middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-26 11:56:24 +00:00
|
|
|
addTemplate({
|
|
|
|
filename: 'layouts.d.ts',
|
|
|
|
write: true,
|
|
|
|
getContents: async () => {
|
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
|
|
|
const layouts = await resolveLayouts(nuxt)
|
|
|
|
return [
|
|
|
|
'import { ComputedRef, Ref } from \'vue\'',
|
|
|
|
`export type LayoutKey = ${layouts.map(layout => `"${layout.name}"`).join(' | ') || 'string'}`,
|
|
|
|
`declare module '${composablesFile}' {`,
|
|
|
|
' interface PageMeta {',
|
|
|
|
' layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-25 12:29:11 +00:00
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'middleware.d.ts') })
|
2022-01-26 11:56:24 +00:00
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'layouts.d.ts') })
|
2022-01-25 12:29:11 +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\'',
|
2021-11-15 19:20:51 +00:00
|
|
|
`export default ${templateUtils.serialize(layoutsObject)}`
|
2021-07-28 11:35:24 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|