diff --git a/packages/nuxt/src/pages/plugins/page-meta.ts b/packages/nuxt/src/pages/plugins/page-meta.ts index 95350d48df..a5972f4e44 100644 --- a/packages/nuxt/src/pages/plugins/page-meta.ts +++ b/packages/nuxt/src/pages/plugins/page-meta.ts @@ -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) diff --git a/packages/nuxt/test/page-metadata.test.ts b/packages/nuxt/test/page-metadata.test.ts index 734e498a69..60247fd4e1 100644 --- a/packages/nuxt/test/page-metadata.test.ts +++ b/packages/nuxt/test/page-metadata.test.ts @@ -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') + ` 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" `)