@@ -2,7 +2,7 @@ import { pathToFileURL } from 'node:url'
2
2
import { createUnplugin } from 'unplugin'
3
3
import { parseQuery , parseURL , stringifyQuery } from 'ufo'
4
4
import { findStaticImports , findExports , StaticImport , parseStaticImport } from 'mlly'
5
- import type { CallExpression , Expression } from 'estree'
5
+ import type { CallExpression , Identifier , Expression } from 'estree'
6
6
import { walk } from 'estree-walker'
7
7
import MagicString from 'magic-string'
8
8
import { isAbsolute , normalize } from 'pathe'
@@ -93,6 +93,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
93
93
}
94
94
95
95
const importMap = new Map < string , StaticImport > ( )
96
+ const addedImports = new Set ( )
96
97
for ( const i of imports ) {
97
98
const parsed = parseStaticImport ( i )
98
99
for ( const name of [
@@ -118,14 +119,25 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
118
119
119
120
let contents = `const __nuxt_page_meta = ${ code ! . slice ( meta . start , meta . end ) || '{}' } \nexport default __nuxt_page_meta`
120
121
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
+
121
132
walk ( meta , {
122
133
enter ( _node ) {
123
134
if ( _node . type === 'CallExpression' ) {
124
135
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 )
129
141
}
130
142
}
131
143
} )
0 commit comments