2022-04-07 11:03:37 +00:00
|
|
|
import { isAbsolute, relative } from 'pathe'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Component } from '@nuxt/schema'
|
2022-04-07 11:03:37 +00:00
|
|
|
import { genDynamicImport, genExport, genObjectFromRawEntries } from 'knitwork'
|
2022-04-19 19:13:55 +00:00
|
|
|
import { upperFirst } from 'scule'
|
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(', ')
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:03:37 +00:00
|
|
|
export const componentsPluginTemplate = {
|
|
|
|
filename: 'components.plugin.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-07-06 19:15:00 +00:00
|
|
|
import { defineNuxtPlugin } from '#app'
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-02-18 09:37:11 +00:00
|
|
|
const components = ${genObjectFromRawEntries(options.components.filter(c => c.global === true).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-04-07 11:03:37 +00:00
|
|
|
export const componentsTemplate = {
|
|
|
|
filename: 'components.mjs',
|
|
|
|
getContents ({ options }: { options: ComponentsTemplateOptions }) {
|
|
|
|
return [
|
|
|
|
'import { defineAsyncComponent } from \'vue\'',
|
|
|
|
...options.components.flatMap((c) => {
|
|
|
|
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
|
|
|
|
const comment = createImportMagicComments(c)
|
2022-04-19 19:13:55 +00:00
|
|
|
const nameWithSuffix = `${c.pascalName}${c.mode !== 'all' ? upperFirst(c.mode) : ''}`
|
2022-04-07 11:03:37 +00:00
|
|
|
|
|
|
|
return [
|
2022-04-19 19:13:55 +00:00
|
|
|
genExport(c.filePath, [{ name: c.export, as: nameWithSuffix }]),
|
|
|
|
`export const Lazy${nameWithSuffix} = defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`
|
2022-04-07 11:03:37 +00:00
|
|
|
]
|
|
|
|
}),
|
|
|
|
`export const componentNames = ${JSON.stringify(options.components.map(c => c.pascalName))}`
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:57:37 +00:00
|
|
|
export const componentsTypeTemplate = {
|
2022-04-07 11:03:37 +00:00
|
|
|
filename: '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-04-07 11:03:37 +00:00
|
|
|
${options.components.map(c => ` '${c.pascalName}': typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(options.buildDir, c.filePath) : c.filePath, { wrapper: false })}['${c.export}']`).join(',\n')}
|
|
|
|
${options.components.map(c => ` 'Lazy${c.pascalName}': typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(options.buildDir, 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
|
|
|
}
|
2022-04-19 19:13:55 +00:00
|
|
|
${options.components.map(c => `export const ${c.pascalName}${c.mode !== 'all' ? upperFirst(c.mode) : ''}: typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(options.buildDir, c.filePath) : c.filePath, { wrapper: false })}['${c.export}']`).join('\n')}
|
|
|
|
${options.components.map(c => `export const Lazy${c.pascalName}${c.mode !== 'all' ? upperFirst(c.mode) : ''}: typeof ${genDynamicImport(isAbsolute(c.filePath) ? relative(options.buildDir, c.filePath) : c.filePath, { wrapper: false })}['${c.export}']`).join('\n')}
|
2022-04-07 11:03:37 +00:00
|
|
|
export const componentNames: string[]
|
2021-10-02 21:06:57 +00:00
|
|
|
`
|
2021-09-23 17:57:37 +00:00
|
|
|
}
|