mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 01:15:58 +00:00
fix(nuxt): collect all identifiers before extracting page metadata (#30478)
This commit is contained in:
parent
5ba98e8cdb
commit
65380c6bc9
@ -173,7 +173,9 @@ export const PageMetaPlugin = (options: PageMetaPluginOptions = {}) => createUnp
|
||||
}
|
||||
}
|
||||
|
||||
const scopeTracker = new ScopeTracker()
|
||||
const scopeTracker = new ScopeTracker({
|
||||
keepExitedScopes: true,
|
||||
})
|
||||
|
||||
function processDeclaration (scopeTrackerNode: ScopeTrackerNode | null) {
|
||||
if (scopeTrackerNode?.type === 'Variable') {
|
||||
@ -210,7 +212,13 @@ export const PageMetaPlugin = (options: PageMetaPluginOptions = {}) => createUnp
|
||||
}
|
||||
}
|
||||
|
||||
parseAndWalk(code, id, {
|
||||
const ast = parseAndWalk(code, id, {
|
||||
scopeTracker,
|
||||
})
|
||||
|
||||
scopeTracker.freeze()
|
||||
|
||||
walk(ast, {
|
||||
scopeTracker,
|
||||
enter: (node) => {
|
||||
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') { return }
|
||||
@ -221,6 +229,7 @@ export const PageMetaPlugin = (options: PageMetaPluginOptions = {}) => createUnp
|
||||
if (!meta) { return }
|
||||
|
||||
walk(meta, {
|
||||
scopeTracker,
|
||||
enter (node, parent) {
|
||||
if (
|
||||
isNotReferencePosition(node, parent)
|
||||
|
@ -497,6 +497,8 @@ function recursive () {
|
||||
recursive()
|
||||
}
|
||||
|
||||
const route = useRoute()
|
||||
|
||||
definePageMeta({
|
||||
middleware: [
|
||||
() => {
|
||||
@ -513,9 +515,19 @@ definePageMeta({
|
||||
prop = 'prop'
|
||||
test () {}
|
||||
}
|
||||
|
||||
console.log(hoisted.value)
|
||||
},
|
||||
],
|
||||
validate: (route) => {
|
||||
return route.params.id === 'test'
|
||||
}
|
||||
})
|
||||
|
||||
// the order of a ref relative to the 'definePageMeta' call should be preserved (in contrast to a simple const)
|
||||
// this tests whether the extraction handles all variables in the upper scope
|
||||
const hoisted = ref('hoisted')
|
||||
|
||||
</script>
|
||||
`
|
||||
const res = compileScript(parse(sfc).descriptor, { id: 'component.vue' })
|
||||
@ -534,6 +546,7 @@ definePageMeta({
|
||||
function recursive () {
|
||||
recursive()
|
||||
}
|
||||
const hoisted = ref('hoisted')
|
||||
const __nuxt_page_meta = {
|
||||
middleware: [
|
||||
() => {
|
||||
@ -550,8 +563,13 @@ definePageMeta({
|
||||
prop = 'prop'
|
||||
test () {}
|
||||
}
|
||||
|
||||
console.log(hoisted.value)
|
||||
},
|
||||
],
|
||||
validate: (route) => {
|
||||
return route.params.id === 'test'
|
||||
}
|
||||
}
|
||||
export default __nuxt_page_meta"
|
||||
`)
|
||||
|
Loading…
Reference in New Issue
Block a user