Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): conditionally use tsx parser #26314

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
}))
}

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 All @@ -233,7 +235,7 @@
for (const plugin of _plugins) {
// Make sure dependency plugins are registered
if (plugin.dependsOn && plugin.dependsOn.some(name => !pluginNames.includes(name))) {
console.error(`Plugin \`${plugin.name}\` depends on \`${plugin.dependsOn.filter(name => !pluginNames.includes(name)).join(', ')}\` but they are not registered.`)

Check warning on line 238 in packages/nuxt/src/core/app.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
}
// Make graph to detect circular dependencies
if (plugin.name) {
Expand All @@ -242,7 +244,7 @@
}
const checkDeps = (name: string, visited: string[] = []): string[] => {
if (visited.includes(name)) {
console.error(`Circular dependency detected in plugins: ${visited.join(' -> ')} -> ${name}`)

Check warning on line 247 in packages/nuxt/src/core/app.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
return []
}
visited.push(name)
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 @@
'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 Expand Up @@ -83,7 +83,7 @@
src: ''
}
])
expect(console.error).toBeCalledWith('Plugin `B` depends on `D` but they are not registered.')

Check warning on line 86 in packages/nuxt/test/plugin-metadata.test.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
vi.restoreAllMocks()
})

Expand All @@ -106,9 +106,9 @@
src: ''
}
])
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: A -> B -> C -> A')

Check warning on line 109 in packages/nuxt/test/plugin-metadata.test.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: B -> C -> A -> B')

Check warning on line 110 in packages/nuxt/test/plugin-metadata.test.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: C -> A -> B -> C')

Check warning on line 111 in packages/nuxt/test/plugin-metadata.test.ts

View workflow job for this annotation

GitHub Actions / code

Unexpected console statement
vi.restoreAllMocks()
})
})