2022-04-07 11:03:37 +00:00
|
|
|
import { isAbsolute, relative } from 'pathe'
|
2022-08-22 10:12:02 +00:00
|
|
|
import type { Component, Nuxt, NuxtPluginTemplate, NuxtTemplate } from '@nuxt/schema'
|
2022-04-07 11:03:37 +00:00
|
|
|
import { genDynamicImport, genExport, genObjectFromRawEntries } from 'knitwork'
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-07-27 13:05:34 +00:00
|
|
|
export interface ComponentsTemplateContext {
|
|
|
|
nuxt: Nuxt
|
|
|
|
options: {
|
|
|
|
getComponents: (mode?: 'client' | 'server' | 'all') => Component[]
|
|
|
|
mode?: 'client' | 'server'
|
|
|
|
}
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type ImportMagicCommentsOptions = {
|
2022-08-23 19:12:22 +00:00
|
|
|
chunkName: string
|
2021-09-23 17:57:37 +00:00
|
|
|
prefetch?: boolean | number
|
|
|
|
preload?: boolean | number
|
|
|
|
}
|
|
|
|
|
|
|
|
const createImportMagicComments = (options: ImportMagicCommentsOptions) => {
|
|
|
|
const { chunkName, prefetch, preload } = options
|
|
|
|
return [
|
|
|
|
`webpackChunkName: "${chunkName}"`,
|
|
|
|
prefetch === true || typeof prefetch === 'number' ? `webpackPrefetch: ${prefetch}` : false,
|
|
|
|
preload === true || typeof preload === 'number' ? `webpackPreload: ${preload}` : false
|
|
|
|
].filter(Boolean).join(', ')
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
export const componentsPluginTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = {
|
2022-04-07 11:03:37 +00:00
|
|
|
filename: 'components.plugin.mjs',
|
2022-08-22 10:12:02 +00:00
|
|
|
getContents ({ options }) {
|
2022-07-27 13:05:34 +00:00
|
|
|
const globalComponents = options.getComponents().filter(c => c.global === true)
|
|
|
|
|
2021-09-23 17:57:37 +00:00
|
|
|
return `import { defineAsyncComponent } from 'vue'
|
2022-07-06 19:15:00 +00:00
|
|
|
import { defineNuxtPlugin } from '#app'
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-07-27 13:05:34 +00:00
|
|
|
const components = ${genObjectFromRawEntries(globalComponents.map((c) => {
|
2021-09-23 17:57:37 +00:00
|
|
|
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
|
2022-02-07 13:45:47 +00:00
|
|
|
const comment = createImportMagicComments(c)
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-02-07 13:45:47 +00:00
|
|
|
return [c.pascalName, `defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`]
|
|
|
|
}))}
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-07-06 19:15:00 +00:00
|
|
|
export default defineNuxtPlugin(nuxtApp => {
|
2021-09-23 17:57:37 +00:00
|
|
|
for (const name in components) {
|
2021-10-18 18:31:37 +00:00
|
|
|
nuxtApp.vueApp.component(name, components[name])
|
|
|
|
nuxtApp.vueApp.component('Lazy' + name, components[name])
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|
2022-07-06 19:15:00 +00:00
|
|
|
})
|
2021-09-23 17:57:37 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
export const componentsTemplate: NuxtTemplate<ComponentsTemplateContext> = {
|
2022-07-27 13:05:34 +00:00
|
|
|
// components.[server|client].mjs'
|
2022-08-22 10:12:02 +00:00
|
|
|
getContents ({ options }) {
|
2022-04-07 11:03:37 +00:00
|
|
|
return [
|
|
|
|
'import { defineAsyncComponent } from \'vue\'',
|
2022-07-27 13:05:34 +00:00
|
|
|
...options.getComponents(options.mode).flatMap((c) => {
|
2022-04-07 11:03:37 +00:00
|
|
|
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
|
|
|
|
const comment = createImportMagicComments(c)
|
|
|
|
|
|
|
|
return [
|
2022-07-27 13:05:34 +00:00
|
|
|
genExport(c.filePath, [{ name: c.export, as: c.pascalName }]),
|
|
|
|
`export const Lazy${c.pascalName} = defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`
|
2022-04-07 11:03:37 +00:00
|
|
|
]
|
|
|
|
}),
|
2022-07-27 13:05:34 +00:00
|
|
|
`export const componentNames = ${JSON.stringify(options.getComponents().map(c => c.pascalName))}`
|
2022-04-07 11:03:37 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
export const componentsTypeTemplate: NuxtTemplate<ComponentsTemplateContext> = {
|
2022-04-07 11:03:37 +00:00
|
|
|
filename: 'components.d.ts',
|
2022-08-22 10:12:02 +00:00
|
|
|
getContents: ({ options, nuxt }) => {
|
2022-07-27 13:05:34 +00:00
|
|
|
const buildDir = nuxt.options.buildDir
|
|
|
|
const componentTypes = options.getComponents().map(c => [
|
|
|
|
c.pascalName,
|
|
|
|
`typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(buildDir, c.filePath) : c.filePath, { wrapper: false })}['${c.export}']`
|
|
|
|
])
|
|
|
|
|
|
|
|
return `// Generated by components discovery
|
2022-09-12 13:40:44 +00:00
|
|
|
declare module '@vue/runtime-core' {
|
2021-09-23 17:57:37 +00:00
|
|
|
export interface GlobalComponents {
|
2022-08-12 19:59:08 +00:00
|
|
|
${componentTypes.map(([pascalName, type]) => ` '${pascalName}': ${type}`).join('\n')}
|
|
|
|
${componentTypes.map(([pascalName, type]) => ` 'Lazy${pascalName}': ${type}`).join('\n')}
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|
2021-10-02 21:06:57 +00:00
|
|
|
}
|
2022-07-27 13:05:34 +00:00
|
|
|
|
2022-08-12 19:59:08 +00:00
|
|
|
${componentTypes.map(([pascalName, type]) => `export const ${pascalName}: ${type}`).join('\n')}
|
|
|
|
${componentTypes.map(([pascalName, type]) => `export const Lazy${pascalName}: ${type}`).join('\n')}
|
2022-07-27 13:05:34 +00:00
|
|
|
|
2022-04-07 11:03:37 +00:00
|
|
|
export const componentNames: string[]
|
2021-10-02 21:06:57 +00:00
|
|
|
`
|
2022-07-27 13:05:34 +00:00
|
|
|
}
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|