2023-04-28 09:14:42 +00:00
|
|
|
import { isIgnored } from '@nuxt/kit'
|
|
|
|
import type { Nuxt } from 'nuxt/schema'
|
|
|
|
import type { Import } from 'unimport'
|
|
|
|
import { createUnimport } from 'unimport'
|
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import { parseURL } from 'ufo'
|
|
|
|
import { parseQuery } from 'vue-router'
|
|
|
|
import type { getComponentsT } from './module'
|
|
|
|
|
|
|
|
export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT, mode: 'client' | 'server' | 'all') {
|
|
|
|
const componentUnimport = createUnimport({
|
|
|
|
imports: [
|
|
|
|
{
|
|
|
|
name: 'componentNames',
|
|
|
|
from: '#build/component-names'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
virtualImports: ['#components']
|
|
|
|
})
|
|
|
|
|
|
|
|
function getComponentsImports (): Import[] {
|
|
|
|
const components = getComponents(mode)
|
|
|
|
return components.flatMap((c): Import[] => {
|
2023-04-30 09:08:08 +00:00
|
|
|
const withMode = (mode: string | undefined) => mode
|
|
|
|
? `${c.filePath}${c.filePath.includes('?') ? '&' : '?'}nuxt_component=${mode}`
|
|
|
|
: c.filePath
|
|
|
|
|
2023-04-28 09:14:42 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
as: c.pascalName,
|
2023-04-30 09:08:08 +00:00
|
|
|
from: withMode(c.mode === 'client' ? 'client' : undefined),
|
2023-04-28 09:14:42 +00:00
|
|
|
name: 'default'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
as: 'Lazy' + c.pascalName,
|
2023-04-30 09:08:08 +00:00
|
|
|
from: withMode([c.mode === 'client' ? 'client' : '', 'async'].filter(Boolean).join(',')),
|
2023-04-28 09:14:42 +00:00
|
|
|
name: 'default'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return createUnplugin(() => ({
|
|
|
|
name: 'nuxt:components:imports',
|
|
|
|
transformInclude (id) {
|
|
|
|
return !isIgnored(id)
|
|
|
|
},
|
|
|
|
async transform (code, id) {
|
|
|
|
// Virtual component wrapper
|
2023-04-30 09:08:08 +00:00
|
|
|
if (id.match(/[?&]nuxt_component=/)) {
|
2023-04-28 09:14:42 +00:00
|
|
|
const { search } = parseURL(id)
|
|
|
|
const query = parseQuery(search)
|
2023-04-30 09:08:08 +00:00
|
|
|
const mode = query.nuxt_component
|
2023-04-28 09:14:42 +00:00
|
|
|
const bare = id.replace(/\?.*/, '')
|
|
|
|
if (mode === 'async') {
|
2023-05-09 15:57:23 +00:00
|
|
|
return {
|
|
|
|
code: [
|
|
|
|
'import { defineAsyncComponent } from "vue"',
|
|
|
|
`export default defineAsyncComponent(() => import(${JSON.stringify(bare)}).then(r => r.default))`
|
|
|
|
].join('\n'),
|
|
|
|
map: null
|
|
|
|
}
|
2023-04-28 09:14:42 +00:00
|
|
|
} else if (mode === 'client') {
|
2023-05-09 15:57:23 +00:00
|
|
|
return {
|
|
|
|
code: [
|
|
|
|
`import __component from ${JSON.stringify(bare)}`,
|
|
|
|
'import { createClientOnly } from "#app/components/client-only"',
|
|
|
|
'export default createClientOnly(__component)'
|
|
|
|
].join('\n'),
|
|
|
|
map: null
|
|
|
|
}
|
2023-04-28 09:14:42 +00:00
|
|
|
} else if (mode === 'client,async') {
|
2023-05-09 15:57:23 +00:00
|
|
|
return {
|
|
|
|
code: [
|
|
|
|
'import { defineAsyncComponent } from "vue"',
|
|
|
|
'import { createClientOnly } from "#app/components/client-only"',
|
|
|
|
`export default defineAsyncComponent(() => import(${JSON.stringify(bare)}).then(r => createClientOnly(r.default)))`
|
|
|
|
].join('\n'),
|
|
|
|
map: null
|
|
|
|
}
|
2023-04-28 09:14:42 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown component mode: ${mode}, this might be an internal bug of Nuxt.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 15:57:23 +00:00
|
|
|
if (!code.includes('#components')) { return }
|
2023-04-28 09:14:42 +00:00
|
|
|
|
|
|
|
componentUnimport.modifyDynamicImports((imports) => {
|
|
|
|
imports.length = 0
|
|
|
|
imports.push(...getComponentsImports())
|
|
|
|
return imports
|
|
|
|
})
|
|
|
|
|
|
|
|
const result = await componentUnimport.injectImports(code, id, { autoImport: false, transformVirtualImports: true })
|
2023-05-09 15:57:23 +00:00
|
|
|
if (!result) { return }
|
|
|
|
|
2023-04-28 09:14:42 +00:00
|
|
|
return {
|
|
|
|
code: result.code,
|
2023-05-09 15:57:23 +00:00
|
|
|
map: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client
|
2023-04-28 09:14:42 +00:00
|
|
|
? result.s.generateMap({ hires: true })
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|