Skip to content

Commit

Permalink
fix: avoid Plugin type mismatches when there are multiple Vite versio…
Browse files Browse the repository at this point in the history
…ns installed
  • Loading branch information
hemengke1997 committed Oct 30, 2023
1 parent 7a8bcd7 commit 7c870e3
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion playground/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/node/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { disableManifestHmr } from './server'

const debug = createDebug('vite-plugin-public-typescript:util ===> ')

type PartialExclude<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

export type OptionsTypeWithDefault = PartialExclude<Required<VPPTPluginOptions>, 'base' | 'sideEffects'>
export type OptionsTypeWithDefault = PartialBy<Required<VPPTPluginOptions>, 'base' | 'sideEffects'>

export { pkgName }

Expand Down
14 changes: 3 additions & 11 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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)
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/node/plugins/inject-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/node/plugins/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/node/plugins/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 7c870e3

Please sign in to comment.