From 5b29dd3f3c929a69e5f647190a64e67be92892f7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 17 Mar 2024 17:57:11 +0100 Subject: [PATCH] fix(nuxt): conditionally use `tsx` parser (#26314) --- packages/nuxt/src/core/app.ts | 4 +++- packages/nuxt/src/core/plugins/plugin-metadata.ts | 4 ++-- packages/nuxt/test/plugin-metadata.test.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 86b3e10b3c..255ef0d061 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -204,13 +204,15 @@ function resolvePaths> (items: Item[], key: { [ })) } +const IS_TSX = /\.[jt]sx$/ + export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) { const _plugins: Array> = [] 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) { diff --git a/packages/nuxt/src/core/plugins/plugin-metadata.ts b/packages/nuxt/src/core/plugins/plugin-metadata.ts index bd29f61a38..596372f83f 100644 --- a/packages/nuxt/src/core/plugins/plugin-metadata.ts +++ b/packages/nuxt/src/core/plugins/plugin-metadata.ts @@ -42,12 +42,12 @@ export const orderMap: Record, number> = { } const metaCache: Record> = {} -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' diff --git a/packages/nuxt/test/plugin-metadata.test.ts b/packages/nuxt/test/plugin-metadata.test.ts index 09a88f0827..b06b3f2371 100644 --- a/packages/nuxt/test/plugin-metadata.test.ts +++ b/packages/nuxt/test/plugin-metadata.test.ts @@ -21,7 +21,7 @@ describe('plugin-metadata', () => { 'export default defineNuxtPlugin({', ...obj.map(([key, value]) => `${key}: ${typeof value === 'function' ? value.toString().replace('"[JSX]"', '() => JSX') : JSON.stringify(value)},`), '})' - ].join('\n')) + ].join('\n'), 'tsx') expect(meta).toMatchInlineSnapshot(` {