Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 000b030

Browse files
committed
fix(nuxt): detect non-functional imports within page meta
1 parent be80360 commit 000b030

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

packages/nuxt/src/pages/page-meta.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { pathToFileURL } from 'node:url'
22
import { createUnplugin } from 'unplugin'
33
import { parseQuery, parseURL, stringifyQuery } from 'ufo'
44
import { findStaticImports, findExports, StaticImport, parseStaticImport } from 'mlly'
5-
import type { CallExpression, Expression } from 'estree'
5+
import type { CallExpression, Identifier, Expression } from 'estree'
66
import { walk } from 'estree-walker'
77
import MagicString from 'magic-string'
88
import { isAbsolute, normalize } from 'pathe'
@@ -93,6 +93,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
9393
}
9494

9595
const importMap = new Map<string, StaticImport>()
96+
const addedImports = new Set()
9697
for (const i of imports) {
9798
const parsed = parseStaticImport(i)
9899
for (const name of [
@@ -118,14 +119,25 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
118119

119120
let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || '{}'}\nexport default __nuxt_page_meta`
120121

122+
function addImport (name: string | false) {
123+
if (name && importMap.has(name)) {
124+
const importValue = importMap.get(name)!.code
125+
if (!addedImports.has(importValue)) {
126+
contents = importMap.get(name)!.code + '\n' + contents
127+
addedImports.add(importValue)
128+
}
129+
}
130+
}
131+
121132
walk(meta, {
122133
enter (_node) {
123134
if (_node.type === 'CallExpression') {
124135
const node = _node as CallExpression & { start: number, end: number }
125-
const name = 'name' in node.callee && node.callee.name
126-
if (name && importMap.has(name)) {
127-
contents = importMap.get(name)!.code + '\n' + contents
128-
}
136+
addImport('name' in node.callee && node.callee.name)
137+
}
138+
if (_node.type === 'Identifier') {
139+
const node = _node as Identifier & { start: number, end: number }
140+
addImport(node.name)
129141
}
130142
}
131143
})

test/fixtures/basic/pages/index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
<script setup lang="ts">
2424
import { setupDevtoolsPlugin } from '@vue/devtools-api'
2525
import { useRuntimeConfig } from '#imports'
26+
import { importedValue, importedRE } from '~/some-exports'
2627
2728
setupDevtoolsPlugin({}, () => {}) as any
2829
2930
const config = useRuntimeConfig()
3031
3132
definePageMeta({
3233
alias: '/some-alias',
33-
other: ref('test')
34+
other: ref('test'),
35+
imported: importedValue,
36+
something: importedRE.test('an imported regex')
3437
})
3538
3639
// reset title template example

test/fixtures/basic/some-exports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const importedValue = 'an imported value'
2+
export const importedRE = /an imported regex/

0 commit comments

Comments
 (0)