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

Commit

Permalink
fix: use dev option to treeshake hmr code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 15, 2022
1 parent f7f0a95 commit 152cfe4
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/nuxt/src/pages/page-meta.ts
Expand Up @@ -19,19 +19,17 @@ export default __nuxt_page_meta
`

const CODE_HMR = `
if (process.dev) {
// 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 }
})
}
// 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) => {
Expand Down Expand Up @@ -109,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, CODE_EMPTY + CODE_HMR)
s.overwrite(0, code.length, CODE_EMPTY + (options.dev ? CODE_HMR : ''))
return result()
}

Expand Down Expand Up @@ -138,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` + CODE_HMR
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 @@ -168,7 +166,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
})

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

return result()
Expand Down

0 comments on commit 152cfe4

Please sign in to comment.