Files
ui/src/plugins/nuxt-environment.ts
renovate[bot] 127e06ae83 chore(deps): update all non-major dependencies (v3) (#4443)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
2025-07-02 11:39:23 +02:00

42 lines
1.3 KiB
TypeScript

import type { UnpluginOptions } from 'unplugin'
import { normalize } from 'pathe'
import { resolvePathSync } from 'mlly'
import MagicString from 'magic-string'
import { runtimeDir } from '../unplugin'
import type { NuxtUIOptions } from '../unplugin'
/**
* This plugin normalises Nuxt environment (#imports) and `import.meta.client` within the Nuxt UI components.
*/
export default function NuxtEnvironmentPlugin(options: NuxtUIOptions) {
const stubPath = resolvePathSync(options.inertia ? '../runtime/inertia/stubs' : '../runtime/vue/stubs', { extensions: ['.ts', '.mjs', '.js'], url: import.meta.url })
return {
name: 'nuxt:ui',
enforce: 'pre',
resolveId(id) {
// this is implemented here rather than in a vite `config` hook for cross-builder support
if (id === '#imports') {
return stubPath
}
},
transformInclude(id) {
return normalize(id).includes(runtimeDir)
},
transform(code) {
if (code.includes('import.meta.client')) {
const s = new MagicString(code)
s.replaceAll('import.meta.client', 'true')
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ hires: true })
}
}
}
}
} satisfies UnpluginOptions
}