2021-05-20 11:42:41 +00:00
|
|
|
import { existsSync } from 'fs'
|
2022-02-07 13:45:47 +00:00
|
|
|
import { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
|
2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2022-02-07 13:45:47 +00:00
|
|
|
import { genDynamicImport, genString, genArrayFromRaw, genImport, genObjectFromRawEntries } from 'knitwork'
|
2022-01-27 11:13:32 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
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
|
|
|
|
2022-02-21 13:03:42 +00:00
|
|
|
// Disable module (and use universal router) if pages dir do not exists
|
2021-07-28 11:35:24 +00:00
|
|
|
if (!existsSync(pagesDir)) {
|
2022-02-21 13:03:42 +00:00
|
|
|
addPlugin(resolve(distDir, 'app/plugins/router'))
|
2021-07-28 11:35:24 +00:00
|
|
|
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) => {
|
2022-01-27 11:13:32 +00:00
|
|
|
const dirs = [
|
|
|
|
nuxt.options.dir.pages,
|
|
|
|
nuxt.options.dir.layouts,
|
|
|
|
nuxt.options.dir.middleware
|
|
|
|
].filter(Boolean)
|
|
|
|
|
|
|
|
const pathPattern = new RegExp(`^(${dirs.map(escapeRE).join('|')})/`)
|
2021-06-30 16:32:22 +00:00
|
|
|
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) => {
|
2022-02-21 13:03:42 +00:00
|
|
|
autoImports.push({ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') })
|
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-02-07 13:45:47 +00:00
|
|
|
const { routes, imports } = normalizeRoutes(pages)
|
|
|
|
return [...imports, `export default ${routes}`].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()
|
|
|
|
const globalMiddleware = middleware.filter(mw => mw.global)
|
2022-01-27 11:13:32 +00:00
|
|
|
const namedMiddleware = middleware.filter(mw => !mw.global)
|
2022-02-07 13:45:47 +00:00
|
|
|
const namedMiddlewareObject = genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))
|
2022-01-25 12:29:11 +00:00
|
|
|
return [
|
2022-02-07 13:45:47 +00:00
|
|
|
...globalMiddleware.map(mw => genImport(mw.path, getImportName(mw.name))),
|
|
|
|
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => getImportName(mw.name)))}`,
|
|
|
|
`export const namedMiddleware = ${namedMiddlewareObject}`
|
2022-01-25 12:29:11 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
addTemplate({
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/middleware.d.ts',
|
2022-01-25 12:29:11 +00:00
|
|
|
getContents: async () => {
|
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
|
|
|
const middleware = await resolveMiddleware()
|
2022-01-27 11:13:32 +00:00
|
|
|
const namedMiddleware = middleware.filter(mw => !mw.global)
|
2022-01-25 12:29:11 +00:00
|
|
|
return [
|
|
|
|
'import type { NavigationGuard } from \'vue-router\'',
|
2022-02-07 13:45:47 +00:00
|
|
|
`export type MiddlewareKey = ${namedMiddleware.map(mw => genString(mw.name)).join(' | ') || 'string'}`,
|
|
|
|
`declare module ${genString(composablesFile)} {`,
|
2022-01-25 12:29:11 +00:00
|
|
|
' interface PageMeta {',
|
|
|
|
' middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-26 11:56:24 +00:00
|
|
|
addTemplate({
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/layouts.d.ts',
|
2022-01-26 11:56:24 +00:00
|
|
|
getContents: async () => {
|
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
|
|
|
const layouts = await resolveLayouts(nuxt)
|
|
|
|
return [
|
|
|
|
'import { ComputedRef, Ref } from \'vue\'',
|
2022-02-07 13:45:47 +00:00
|
|
|
`export type LayoutKey = ${layouts.map(layout => genString(layout.name)).join(' | ') || 'string'}`,
|
|
|
|
`declare module ${genString(composablesFile)} {`,
|
2022-01-26 11:56:24 +00:00
|
|
|
' interface PageMeta {',
|
|
|
|
' layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Add layouts template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'layouts.mjs',
|
|
|
|
async getContents () {
|
|
|
|
const layouts = await resolveLayouts(nuxt)
|
2022-02-07 13:45:47 +00:00
|
|
|
const layoutsObject = genObjectFromRawEntries(layouts.map(({ name, file }) => {
|
|
|
|
return [name, `defineAsyncComponent({ suspensible: false, loader: ${genDynamicImport(file)} })`]
|
2021-07-28 11:35:24 +00:00
|
|
|
}))
|
|
|
|
return [
|
|
|
|
'import { defineAsyncComponent } from \'vue\'',
|
2022-02-07 13:45:47 +00:00
|
|
|
`export default ${layoutsObject}`
|
2021-07-28 11:35:24 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
})
|
2022-01-27 11:13:32 +00:00
|
|
|
|
|
|
|
// Add declarations for middleware and layout keys
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
2022-02-07 10:20:01 +00:00
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'types/middleware.d.ts') })
|
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'types/layouts.d.ts') })
|
2022-01-27 11:13:32 +00:00
|
|
|
})
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
|
|
|
})
|