2022-07-14 17:46:12 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
|
|
|
import { parseURL } from 'ufo'
|
|
|
|
import MagicString from 'magic-string'
|
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import type { Component } from '@nuxt/schema'
|
|
|
|
|
|
|
|
interface TreeShakeTemplatePluginOptions {
|
|
|
|
sourcemap?: boolean
|
|
|
|
getComponents(): Component[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TreeShakeTemplatePlugin = createUnplugin((options: TreeShakeTemplatePluginOptions) => {
|
|
|
|
const regexpMap = new WeakMap<Component[], RegExp>()
|
|
|
|
return {
|
|
|
|
name: 'nuxt:tree-shake-template',
|
|
|
|
enforce: 'pre',
|
|
|
|
transformInclude (id) {
|
|
|
|
const { pathname } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
|
|
|
return pathname.endsWith('.vue')
|
|
|
|
},
|
|
|
|
transform (code, id) {
|
|
|
|
const components = options.getComponents()
|
|
|
|
|
|
|
|
if (!regexpMap.has(components)) {
|
|
|
|
const clientOnlyComponents = components
|
|
|
|
.filter(c => c.mode === 'client' && !components.some(other => other.mode !== 'client' && other.pascalName === c.pascalName))
|
|
|
|
.map(c => `${c.pascalName}|${c.kebabName}`)
|
|
|
|
.concat('ClientOnly|client-only')
|
|
|
|
.map(component => `<(${component})[^>]*>[\\s\\S]*?<\\/(${component})>`)
|
|
|
|
|
|
|
|
regexpMap.set(components, new RegExp(`(${clientOnlyComponents.join('|')})`, 'g'))
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:12:02 +00:00
|
|
|
const COMPONENTS_RE = regexpMap.get(components)!
|
2022-07-14 17:46:12 +00:00
|
|
|
const s = new MagicString(code)
|
|
|
|
|
|
|
|
// Do not render client-only slots on SSR, but preserve attributes
|
2022-08-16 13:22:10 +00:00
|
|
|
s.replace(COMPONENTS_RE, r => r.replace(/<([^>]*[^/])\/?>[\s\S]*$/, '<$1 />'))
|
2022-07-14 17:46:12 +00:00
|
|
|
|
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
2022-08-22 10:12:02 +00:00
|
|
|
map: options.sourcemap
|
|
|
|
? s.generateMap({ source: id, includeContent: true })
|
|
|
|
: undefined
|
2022-07-14 17:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|