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