2022-04-07 11:03:37 +00:00
|
|
|
import { isAbsolute, relative } from 'pathe'
|
2023-04-28 09:14:42 +00:00
|
|
|
import { genDynamicImport } from 'knitwork'
|
2023-05-01 16:35:00 +00:00
|
|
|
import type { Component, Nuxt, NuxtApp, NuxtPluginTemplate, NuxtTemplate } from 'nuxt/schema'
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2022-07-27 13:05:34 +00:00
|
|
|
export interface ComponentsTemplateContext {
|
2023-05-01 16:35:00 +00:00
|
|
|
app: NuxtApp
|
2022-07-27 13:05:34 +00:00
|
|
|
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(', ')
|
|
|
|
}
|
|
|
|
|
2023-04-28 09:14:42 +00:00
|
|
|
const emptyComponentsPlugin = `
|
|
|
|
import { defineNuxtPlugin } from '#app/nuxt'
|
|
|
|
export default defineNuxtPlugin({
|
|
|
|
name: 'nuxt:global-components',
|
|
|
|
})
|
|
|
|
`
|
|
|
|
|
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',
|
2023-05-01 16:35:00 +00:00
|
|
|
getContents ({ app }) {
|
2023-05-10 12:57:27 +00:00
|
|
|
const globalComponents = new Set<string>()
|
|
|
|
for (const component of app.components) {
|
|
|
|
if (component.global) {
|
|
|
|
globalComponents.add(component.pascalName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!globalComponents.size) { return emptyComponentsPlugin }
|
|
|
|
|
|
|
|
const components = [...globalComponents]
|
2023-04-28 09:14:42 +00:00
|
|
|
|
2023-04-13 19:08:08 +00:00
|
|
|
return `import { defineNuxtPlugin } from '#app/nuxt'
|
2023-05-10 12:57:27 +00:00
|
|
|
import { ${components.map(c => 'Lazy' + c).join(', ')} } from '#components'
|
2023-04-28 09:14:42 +00:00
|
|
|
const lazyGlobalComponents = [
|
2023-05-10 12:57:27 +00:00
|
|
|
${components.map(c => `["${c}", Lazy${c}]`).join(',\n')}
|
2023-04-28 09:14:42 +00:00
|
|
|
]
|
2021-09-23 17:57:37 +00:00
|
|
|
|
2023-04-11 11:58:43 +00:00
|
|
|
export default defineNuxtPlugin({
|
2023-04-28 09:14:42 +00:00
|
|
|
name: 'nuxt:global-components',
|
2023-04-11 11:58:43 +00:00
|
|
|
setup (nuxtApp) {
|
2023-04-28 09:14:42 +00:00
|
|
|
for (const [name, component] of lazyGlobalComponents) {
|
|
|
|
nuxtApp.vueApp.component(name, component)
|
|
|
|
nuxtApp.vueApp.component('Lazy' + name, component)
|
2023-04-11 11:58:43 +00:00
|
|
|
}
|
2023-04-28 09:14:42 +00:00
|
|
|
}
|
2022-07-06 19:15:00 +00:00
|
|
|
})
|
2021-09-23 17:57:37 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 09:14:42 +00:00
|
|
|
export const componentNamesTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = {
|
|
|
|
filename: 'component-names.mjs',
|
2023-05-01 16:35:00 +00:00
|
|
|
getContents ({ app }) {
|
|
|
|
return `export const componentNames = ${JSON.stringify(app.components.filter(c => !c.island).map(c => c.pascalName))}`
|
2022-04-07 11:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
export const componentsIslandsTemplate: NuxtTemplate<ComponentsTemplateContext> = {
|
|
|
|
// components.islands.mjs'
|
2023-05-01 16:35:00 +00:00
|
|
|
getContents ({ app }) {
|
|
|
|
const components = app.components
|
2023-01-09 11:20:33 +00:00
|
|
|
const islands = components.filter(component =>
|
|
|
|
component.island ||
|
|
|
|
// .server components without a corresponding .client component will need to be rendered as an island
|
|
|
|
(component.mode === 'server' && !components.some(c => c.pascalName === component.pascalName && c.mode === 'client'))
|
|
|
|
)
|
2023-03-23 00:19:26 +00:00
|
|
|
return ['import { defineAsyncComponent } from \'vue\'', ...islands.map(
|
2022-11-24 12:24:14 +00:00
|
|
|
(c) => {
|
|
|
|
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
|
|
|
|
const comment = createImportMagicComments(c)
|
2023-02-16 15:00:40 +00:00
|
|
|
return `export const ${c.pascalName} = /* #__PURE__ */ defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
2023-03-23 00:19:26 +00:00
|
|
|
)].join('\n')
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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',
|
2023-05-01 16:35:00 +00:00
|
|
|
getContents: ({ app, nuxt }) => {
|
2022-07-27 13:05:34 +00:00
|
|
|
const buildDir = nuxt.options.buildDir
|
2023-05-01 16:35:00 +00:00
|
|
|
const componentTypes = app.components.filter(c => !c.island).map(c => [
|
2022-07-27 13:05:34 +00:00
|
|
|
c.pascalName,
|
2022-09-20 08:54:46 +00:00
|
|
|
`typeof ${genDynamicImport(isAbsolute(c.filePath)
|
|
|
|
? relative(buildDir, c.filePath).replace(/(?<=\w)\.(?!vue)\w+$/g, '')
|
|
|
|
: c.filePath.replace(/(?<=\w)\.(?!vue)\w+$/g, ''), { wrapper: false })}['${c.export}']`
|
2022-07-27 13:05:34 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
return `// Generated by components discovery
|
2023-01-25 15:59:02 +00:00
|
|
|
declare module 'vue' {
|
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
|
|
|
}
|