mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
fix(nuxt): handle tsx code when extracting pageMeta/routeRules (#27583)
This commit is contained in:
parent
d202669611
commit
ab887d90d7
@ -18,11 +18,12 @@ export async function extractRouteRules (code: string): Promise<NitroRouteConfig
|
||||
}
|
||||
if (!ROUTE_RULE_RE.test(code)) { return null }
|
||||
|
||||
code = extractScriptContent(code) || code
|
||||
const script = extractScriptContent(code)
|
||||
code = script?.code || code
|
||||
|
||||
let rule: NitroRouteConfig | null = null
|
||||
|
||||
const js = await transform(code, { loader: 'ts' })
|
||||
const js = await transform(code, { loader: script?.loader || 'ts' })
|
||||
walk(parse(js.code, {
|
||||
sourceType: 'module',
|
||||
ecmaVersion: 'latest',
|
||||
|
@ -154,12 +154,15 @@ export async function augmentPages (routes: NuxtPage[], vfs: Record<string, stri
|
||||
return augmentedPages
|
||||
}
|
||||
|
||||
const SFC_SCRIPT_RE = /<script[^>]*>([\s\S]*?)<\/script[^>]*>/i
|
||||
const SFC_SCRIPT_RE = /<script(?<attrs>[^>]*)>(?<content>[\s\S]*?)<\/script[^>]*>/i
|
||||
export function extractScriptContent (html: string) {
|
||||
const match = html.match(SFC_SCRIPT_RE)
|
||||
const groups = html.match(SFC_SCRIPT_RE)?.groups || {}
|
||||
|
||||
if (match && match[1]) {
|
||||
return match[1].trim()
|
||||
if (groups.content) {
|
||||
return {
|
||||
loader: groups.attrs.includes('tsx') ? 'tsx' : 'ts',
|
||||
code: groups.content.trim(),
|
||||
} as const
|
||||
}
|
||||
|
||||
return null
|
||||
@ -185,12 +188,12 @@ async function getRouteMeta (contents: string, absolutePath: string): Promise<Pa
|
||||
return {}
|
||||
}
|
||||
|
||||
if (!PAGE_META_RE.test(script)) {
|
||||
if (!PAGE_META_RE.test(script.code)) {
|
||||
metaCache[absolutePath] = {}
|
||||
return {}
|
||||
}
|
||||
|
||||
const js = await transform(script, { loader: 'ts' })
|
||||
const js = await transform(script.code, { loader: script.loader })
|
||||
const ast = parse(js.code, {
|
||||
sourceType: 'module',
|
||||
ecmaVersion: 'latest',
|
||||
|
9
test/fixtures/basic/pages/tsx-page-meta.vue
vendored
Normal file
9
test/fixtures/basic/pages/tsx-page-meta.vue
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
<template>
|
||||
<PageContent />
|
||||
</template>
|
||||
|
||||
<script setup lang="tsx">
|
||||
definePageMeta({})
|
||||
defineRouteRules({})
|
||||
const PageContent = () => (<div>Home Page</div>)
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user