fix(nuxt): conditionally use tsx parser (#26314)

This commit is contained in:
Daniel Roe 2024-03-17 17:57:11 +01:00 committed by GitHub
parent 4c8793587b
commit 5b29dd3f3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 4 deletions

View File

@ -204,13 +204,15 @@ function resolvePaths<Item extends Record<string, any>> (items: Item[], key: { [
}))
}
const IS_TSX = /\.[jt]sx$/
export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) {
const _plugins: Array<NuxtPlugin & Omit<PluginMeta, 'enforce'>> = []
for (const plugin of plugins) {
try {
const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src] : await fsp.readFile(plugin.src!, 'utf-8')
_plugins.push({
...await extractMetadata(code),
...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts'),
...plugin
})
} catch (e) {

View File

@ -42,12 +42,12 @@ export const orderMap: Record<NonNullable<ObjectPlugin['enforce']>, number> = {
}
const metaCache: Record<string, Omit<PluginMeta, 'enforce'>> = {}
export async function extractMetadata (code: string) {
export async function extractMetadata (code: string, loader = 'ts' as 'ts' | 'tsx') {
let meta: PluginMeta = {}
if (metaCache[code]) {
return metaCache[code]
}
const js = await transform(code, { loader: 'tsx' })
const js = await transform(code, { loader })
walk(parse(js.code, {
sourceType: 'module',
ecmaVersion: 'latest'

View File

@ -21,7 +21,7 @@ describe('plugin-metadata', () => {
'export default defineNuxtPlugin({',
...obj.map(([key, value]) => `${key}: ${typeof value === 'function' ? value.toString().replace('"[JSX]"', '() => <span>JSX</span>') : JSON.stringify(value)},`),
'})'
].join('\n'))
].join('\n'), 'tsx')
expect(meta).toMatchInlineSnapshot(`
{