Skip to content

Commit

Permalink
fix: import.meta.hot workaround for Vite >= 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Dec 31, 2021
1 parent d4c8d71 commit f29c1b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
6 changes: 0 additions & 6 deletions examples/react/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import App from './App'
import routes from './routes'
import vitedge from 'vitedge'

// Workaround for Vite 2.6.x
// https://github.com/vitejs/vite/issues/5270
if (import.meta.hot) {
globalThis.__hot = import.meta.hot
}

export default vitedge(App, { routes }, ({ url, isClient, ...context }) => {
/* Custom hook */
})
6 changes: 0 additions & 6 deletions examples/vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import App from './App.vue'
import routes from './routes'
import vitedge from 'vitedge'

// Workaround for Vite 2.6.x
// https://github.com/vitejs/vite/issues/5270
if (import.meta.hot) {
globalThis.__hot = import.meta.hot
}

export default vitedge(
App,
{ routes },
Expand Down
25 changes: 24 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import viteSSR from 'vite-ssr/plugin.js'
import { getEntryPoint } from 'vite-ssr/config.js'
import { configureServer, getRenderContext } from './dev/middleware.js'

const pluginName = 'vitedge'
Expand All @@ -7,6 +8,8 @@ const entryClient = '/entry-client'

export default (options = {}) => {
let lib
let entryPoint
let resolvedConfig

return [
viteSSR({
Expand Down Expand Up @@ -48,7 +51,8 @@ export default (options = {}) => {
},
}
},
configResolved: (config) => {
configResolved: async (config) => {
resolvedConfig = config
const libPath = `/${lib}`

// config.alias is pre-beta.69
Expand All @@ -67,6 +71,25 @@ export default (options = {}) => {
pluginName + libPath + entryClient,
pluginName + libPath + entryServer
)

entryPoint = await getEntryPoint(config)
},

// Fix import.meta.hot in Vite >= 2.6
// https://github.com/vitejs/vite/issues/5270
transform(code, id, options) {
if (
resolvedConfig.command === 'serve' &&
!(options || {}).ssr &&
entryPoint &&
id.startsWith(entryPoint)
) {
// Copy import.meta.hot globally so it can be used
// by Vitedge in src/dev/hmr.js in development
return code + '\nglobalThis.__hot=import.meta.hot;'
}

return null
},
},
]
Expand Down

0 comments on commit f29c1b5

Please sign in to comment.