2023-03-08 21:13:06 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import type { ComponentsOptions } from '@nuxt/schema'
|
|
|
|
import MagicString from 'magic-string'
|
|
|
|
import { isAbsolute, relative } from 'pathe'
|
|
|
|
import { hash } from 'ohash'
|
2023-04-19 18:17:36 +00:00
|
|
|
import { isVue } from '../core/utils'
|
2023-03-08 21:13:06 +00:00
|
|
|
interface LoaderOptions {
|
|
|
|
sourcemap?: boolean
|
|
|
|
transform?: ComponentsOptions['transform'],
|
|
|
|
rootDir: string
|
|
|
|
}
|
|
|
|
const CLIENT_FALLBACK_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/
|
|
|
|
const CLIENT_FALLBACK_GLOBAL_RE = /<(NuxtClientFallback|nuxt-client-fallback)( [^>]*)?>/g
|
|
|
|
export const clientFallbackAutoIdPlugin = createUnplugin((options: LoaderOptions) => {
|
|
|
|
const exclude = options.transform?.exclude || []
|
|
|
|
const include = options.transform?.include || []
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: 'nuxt:client-fallback-auto-id',
|
|
|
|
enforce: 'pre',
|
|
|
|
transformInclude (id) {
|
|
|
|
if (exclude.some(pattern => id.match(pattern))) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (include.some(pattern => id.match(pattern))) {
|
|
|
|
return true
|
|
|
|
}
|
2023-05-13 21:39:50 +00:00
|
|
|
return isVue(id)
|
2023-03-08 21:13:06 +00:00
|
|
|
},
|
|
|
|
transform (code, id) {
|
|
|
|
if (!CLIENT_FALLBACK_RE.test(code)) { return }
|
|
|
|
|
|
|
|
const s = new MagicString(code)
|
|
|
|
const relativeID = isAbsolute(id) ? relative(options.rootDir, id) : id
|
|
|
|
let count = 0
|
|
|
|
|
|
|
|
s.replace(CLIENT_FALLBACK_GLOBAL_RE, (full, name, attrs) => {
|
|
|
|
count++
|
|
|
|
if (/ :?uid=/g.test(attrs)) { return full }
|
|
|
|
return `<${name} :uid="'${hash(relativeID)}' + JSON.stringify($props) + '${count}'" ${attrs ?? ''}>`
|
|
|
|
})
|
|
|
|
|
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
|
|
|
map: options.sourcemap
|
2023-04-14 17:21:08 +00:00
|
|
|
? s.generateMap({ hires: true })
|
2023-03-08 21:13:06 +00:00
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|