fix(nuxt): make pageMeta ast undefineable in transformation

This commit is contained in:
julien huang 2024-03-19 20:59:08 +01:00
parent f55f747984
commit dfd935bee6

View File

@ -122,10 +122,10 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
const name = 'name' in node.callee && node.callee.name const name = 'name' in node.callee && node.callee.name
if (name !== 'definePageMeta') { return } if (name !== 'definePageMeta') { return }
const meta = node.arguments[0] as Expression & { start: number, end: number } const meta = node.arguments[0] as (Expression & { start: number, end: number }) | undefined
let contents = `const __nuxt_page_meta = ${meta ? code!.slice(meta.start, meta.end) || 'null' : 'null'}\nexport default __nuxt_page_meta` + (options.dev ? CODE_HMR : '')
let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || 'null'}\nexport default __nuxt_page_meta` + (options.dev ? CODE_HMR : '')
if(meta) {
function addImport (name: string | false) { function addImport (name: string | false) {
if (name && importMap.has(name)) { if (name && importMap.has(name)) {
const importValue = importMap.get(name)!.code const importValue = importMap.get(name)!.code
@ -148,6 +148,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
} }
} }
}) })
}
s.overwrite(0, code.length, contents) s.overwrite(0, code.length, contents)
} }