2021-09-23 17:57:37 +00:00
|
|
|
|
2022-02-07 10:20:01 +00:00
|
|
|
import { isAbsolute, join, relative } from 'pathe'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Component } from '@nuxt/schema'
|
2022-02-07 13:45:47 +00:00
|
|
|
import { genDynamicImport, genObjectFromRawEntries } from 'knitwork'
|
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'
|
|
|
|
|
2022-02-07 13:45:47 +00:00
|
|
|
const components = ${genObjectFromRawEntries(options.components.filter(c => c.global !== false).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
|
|
|
|
2021-10-18 18:31:37 +00:00
|
|
|
export default function (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
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const componentsTypeTemplate = {
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/components.d.ts',
|
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 {
|
2022-02-07 13:45:47 +00:00
|
|
|
${options.components.map(c => ` '${c.pascalName}': typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(join(options.buildDir, 'types'), c.filePath) : c.filePath, { wrapper: false })}['${c.export}']`).join(',\n')}
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|
2021-10-02 21:06:57 +00:00
|
|
|
}
|
|
|
|
export {}
|
|
|
|
`
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|