From 2cb9e0f68b5fc14fb69c7bb9ec69cae904d55f5e Mon Sep 17 00:00:00 2001 From: Jonas Galvez Date: Mon, 3 Jan 2022 11:32:07 -0300 Subject: [PATCH] fix: ensure HMR stays working in SSR (#160) --- src/index.ts | 8 +------- src/main.ts | 7 ++----- test/unit.spec.ts | 2 -- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 72b2ea7..8397fdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -152,13 +152,7 @@ export function createVuePlugin(rawOptions: VueViteOptions = {}): Plugin { if (!query.vue) { // main request - return await transformMain( - code, - filename, - options, - transformOptions || {}, - this - ) + return await transformMain(code, filename, options, this) } const descriptor = getDescriptor( diff --git a/src/main.ts b/src/main.ts index 28aa138..e821d58 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,6 @@ export async function transformMain( code: string, filePath: string, options: ResolvedOptions, - transformOptions: any, pluginContext: TransformPluginContext ) { const descriptor = createDescriptor(code, filePath, options) @@ -87,7 +86,7 @@ function __vue2_injectStyles (context) { // code += `\n__component__.options.__file = ${JSON.stringify(filePath)}` // } - if (!transformOptions.ssr && options.devServer && !options.isProduction) { + if (options.devServer && !options.isProduction) { result += genHmrCode( options.root, descriptor.id, @@ -250,7 +249,7 @@ function genHmrCode( import __VUE_HMR_RUNTIME__ from ${JSON.stringify(vueHotReload)} import vue from "vue" __VUE_HMR_RUNTIME__.install(vue) -if(__VUE_HMR_RUNTIME__.compatible){ +if(!import.meta.env.SSR && __VUE_HMR_RUNTIME__.compatible){ if (!__VUE_HMR_RUNTIME__.isRecorded(${idJSON})) { __VUE_HMR_RUNTIME__.createRecord(${idJSON}, __component__.options) } @@ -268,8 +267,6 @@ if(__VUE_HMR_RUNTIME__.compatible){ })` : '' } -} else { - console.log("The hmr is not compatible.") }` } diff --git a/test/unit.spec.ts b/test/unit.spec.ts index 27f25b1..7562f97 100644 --- a/test/unit.spec.ts +++ b/test/unit.spec.ts @@ -20,8 +20,6 @@ describe('vite-plugin-vue2', () => { // @ts-ignore need mock if it will be used in the future devServer: {}, }, - // @ts-ignore need mock if it will be used in the future - {}, {} ) expect(scriptMap).toHaveProperty('file')