Skip to content

Commit

Permalink
fix(nuxt): conditionally use tsx parser (#26314)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 17, 2024
1 parent 4c87935 commit 5b29dd3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/core/plugins/plugin-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/test/plugin-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
{
Expand Down

0 comments on commit 5b29dd3

Please sign in to comment.