2021-09-23 17:57:37 +00:00
|
|
|
|
2021-09-29 09:55:55 +00:00
|
|
|
import { relative } from 'pathe'
|
2021-10-18 12:49:02 +00:00
|
|
|
import type { Component } from '@nuxt/kit'
|
2021-09-23 17:57:37 +00:00
|
|
|
|
|
|
|
export type ComponentsTemplateOptions = {
|
|
|
|
buildDir?: string
|
|
|
|
components: Component[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ImportMagicCommentsOptions = {
|
|
|
|
chunkName:string
|
|
|
|
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(', ')
|
|
|
|
}
|
|
|
|
|
|
|
|
export const componentsTemplate = {
|
|
|
|
filename: 'components.mjs',
|
2021-10-18 11:59:31 +00:00
|
|
|
getContents ({ options }: { options: ComponentsTemplateOptions }) {
|
2021-09-23 17:57:37 +00:00
|
|
|
return `import { defineAsyncComponent } from 'vue'
|
|
|
|
|
|
|
|
const components = {
|
|
|
|
${options.components.map((c) => {
|
|
|
|
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
|
|
|
|
const magicComments = createImportMagicComments(c)
|
|
|
|
|
|
|
|
return ` '${c.pascalName}': defineAsyncComponent(() => import('${c.filePath}' /* ${magicComments} */).then(c => ${exp}))`
|
|
|
|
}).join(',\n')}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (nuxt) {
|
|
|
|
for (const name in components) {
|
|
|
|
nuxt.app.component(name, components[name])
|
|
|
|
nuxt.app.component('Lazy' + name, components[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const componentsTypeTemplate = {
|
|
|
|
filename: 'components.d.ts',
|
|
|
|
write: true,
|
2021-10-18 11:59:31 +00:00
|
|
|
getContents: ({ options }: { options: ComponentsTemplateOptions }) => `// Generated by components discovery
|
2021-09-23 17:57:37 +00:00
|
|
|
declare module 'vue' {
|
|
|
|
export interface GlobalComponents {
|
|
|
|
${options.components.map(c => ` '${c.pascalName}': typeof import('${relative(options.buildDir, c.filePath)}')['${c.export}']`).join(',\n')}
|
|
|
|
}
|
2021-10-02 21:06:57 +00:00
|
|
|
}
|
|
|
|
export {}
|
|
|
|
`
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|