From 7c870e318c2bcbfd32c3caa1488b8368ad00c1b9 Mon Sep 17 00:00:00 2001 From: hemengke1997 <23536175@qq.com> Date: Mon, 30 Oct 2023 10:31:58 +0800 Subject: [PATCH] fix: avoid Plugin type mismatches when there are multiple Vite versions installed --- package.json | 2 +- playground/ssr/package.json | 3 ++- pnpm-lock.yaml | 3 +++ src/node/helper/utils.ts | 4 ++-- src/node/index.ts | 14 +++----------- src/node/plugins/inject-script.ts | 3 ++- src/node/plugins/server.ts | 2 +- src/node/plugins/virtual.ts | 2 +- 8 files changed, 15 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 95d2856..2ba7f3b 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "bump": "bumpp package.json -c -p -t --all -x \"pnpm run changelog\"" }, "peerDependencies": { - "vite": ">=4" + "vite": ">=4.0.0" }, "dependencies": { "debug": "^4.3.4", diff --git a/playground/ssr/package.json b/playground/ssr/package.json index d3b8fa9..563c4fc 100644 --- a/playground/ssr/package.json +++ b/playground/ssr/package.json @@ -24,6 +24,7 @@ "@vitejs/plugin-react": "^4.1.0", "compression": "^1.7.4", "express": "^4.18.2", - "serve-static": "^1.15.0" + "serve-static": "^1.15.0", + "vite": "^4.5.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a4b4263..d6f780e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -191,6 +191,9 @@ importers: serve-static: specifier: ^1.15.0 version: 1.15.0 + vite: + specifier: ^4.5.0 + version: 4.5.0(@types/node@20.8.9) packages: diff --git a/src/node/helper/utils.ts b/src/node/helper/utils.ts index f384758..11cf907 100644 --- a/src/node/helper/utils.ts +++ b/src/node/helper/utils.ts @@ -14,9 +14,9 @@ import { disableManifestHmr } from './server' const debug = createDebug('vite-plugin-public-typescript:util ===> ') -type PartialExclude = Omit & Partial> +type PartialBy = Omit & Partial> -export type OptionsTypeWithDefault = PartialExclude, 'base' | 'sideEffects'> +export type OptionsTypeWithDefault = PartialBy, 'base' | 'sideEffects'> export { pkgName } diff --git a/src/node/index.ts b/src/node/index.ts index d7a0384..8f7b9fb 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -100,8 +100,6 @@ export const DEFAULT_OPTIONS: OptionsTypeWithDefault = { cacheDir: 'node_modules/.vite-plugin-public-typescript', } -// let previousOpts: VPPTPluginOptions - export default function publicTypescript(options: VPPTPluginOptions = {}) { const opts = { ...DEFAULT_OPTIONS, @@ -114,7 +112,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { let viteConfig: ResolvedConfig - const plugins: PluginOption = [ + const plugins: PluginOption[] = [ { name: 'vite:public-typescript', enforce: 'post', @@ -131,13 +129,6 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { initWatcher((file) => reloadPage(ws, file)) }, async buildStart() { - // skip server restart when options not changed - // if (eq(previousOpts, opts)) { - // return - // } - - // previousOpts = opts - const manifestPath = manifestCache.manifestPath fs.ensureFileSync(manifestPath) @@ -204,7 +195,8 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) { pluginVirtual(), ] - return plugins + // Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed + return plugins as any } export { injectTagsToHtml } from './helper/html' diff --git a/src/node/plugins/inject-script.ts b/src/node/plugins/inject-script.ts index c4022d9..ff7751a 100644 --- a/src/node/plugins/inject-script.ts +++ b/src/node/plugins/inject-script.ts @@ -37,5 +37,6 @@ export function injectScripts(scripts: Scripts) { }, } - return plugin + // Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed + return plugin as any } diff --git a/src/node/plugins/server.ts b/src/node/plugins/server.ts index 20c53dd..67d9628 100644 --- a/src/node/plugins/server.ts +++ b/src/node/plugins/server.ts @@ -5,7 +5,7 @@ import { getScriptInfo, nodeIsElement, traverseHtml } from '../helper/html' import { addCodeHeader } from '../helper/server' import { manifestCache } from '../manifest-cache' -export function pluginServer() { +export function pluginServer(): PluginOption { const plugin: PluginOption = { name: 'vite:public-typescript:server', apply: 'serve', diff --git a/src/node/plugins/virtual.ts b/src/node/plugins/virtual.ts index 89739d8..dd85066 100644 --- a/src/node/plugins/virtual.ts +++ b/src/node/plugins/virtual.ts @@ -2,7 +2,7 @@ import { type PluginOption } from 'vite' import { resolvedVirtualModuleId, virtualModuleId } from '../helper/virtual' import { manifestCache } from '../manifest-cache' -export function pluginVirtual() { +export function pluginVirtual(): PluginOption { const plugin: PluginOption = { name: 'vite:public-typescript:virtual', enforce: 'post',