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

Commit

Permalink
fix(nuxt): improve hmr for pages macros (#8940)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
antfu and danielroe authored Nov 15, 2022
1 parent 3fc29b9 commit f1ae40f
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions packages/nuxt/src/pages/page-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ export interface PageMetaPluginOptions {
sourcemap?: boolean
}

const CODE_EMPTY = `
const __nuxt_page_meta = {}
export default __nuxt_page_meta
`

const CODE_HMR = `
// Vite
if (import.meta.hot) {
import.meta.hot.accept(mod => {
Object.assign(__nuxt_page_meta, mod)
})
}
// Webpack
if (import.meta.webpackHot) {
import.meta.webpackHot.accept((err) => {
if (err) { window.location = window.location.href }
})
}`

export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) => {
return {
name: 'nuxt:pages-macros-transform',
Expand Down Expand Up @@ -88,7 +107,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
}

if (!hasMacro && !code.includes('export { default }') && !code.includes('__nuxt_page_meta')) {
s.overwrite(0, code.length, 'export default {}')
s.overwrite(0, code.length, CODE_EMPTY + (options.dev ? CODE_HMR : ''))
return result()
}

Expand Down Expand Up @@ -117,7 +136,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>

const meta = node.arguments[0] as Expression & { start: number, end: number }

let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || '{}'}\nexport default __nuxt_page_meta`
let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || '{}'}\nexport default __nuxt_page_meta` + (options.dev ? CODE_HMR : '')

function addImport (name: string | false) {
if (name && importMap.has(name)) {
Expand Down Expand Up @@ -147,10 +166,22 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
})

if (!s.hasChanged() && !code.includes('__nuxt_page_meta')) {
s.overwrite(0, code.length, 'export default {}')
s.overwrite(0, code.length, CODE_EMPTY + (options.dev ? CODE_HMR : ''))
}

return result()
},
vite: {
handleHotUpdate: {
order: 'pre',
handler: ({ modules }) => {
// Remove macro file from modules list to prevent HMR overrides
const index = modules.findIndex(i => i.id?.includes('?macro=true'))
if (index !== -1) {
modules.splice(index, 1)
}
}
}
}
}
})
Expand Down

0 comments on commit f1ae40f

Please sign in to comment.