mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): conditionally use tsx
parser (#26314)
This commit is contained in:
parent
4c8793587b
commit
5b29dd3f3c
@ -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) {
|
||||
|
@ -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'
|
||||
|
@ -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(`
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user