Created
September 9, 2023 23:35
-
-
Save yurique/bf20370060ff6c10065a0c23e2fd1a2f to your computer and use it in GitHub Desktop.
fix-double-refresh.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Plugin} from "vite"; | |
interface PluginConfig { | |
excludeExtensions?: string[] | |
} | |
interface ResolvedPluginConfig extends PluginConfig { | |
excludeExtensions: string[] | |
} | |
interface FixDoubleRefreshPlugin extends Plugin { | |
readonly pluginConfig: PluginConfig; | |
} | |
export const defaultConfig: PluginConfig = { | |
excludeExtensions: [] | |
} | |
function resolvePluginConfig(config?: PluginConfig): ResolvedPluginConfig { | |
if (typeof config === 'undefined') { | |
config = defaultConfig; | |
} | |
if (typeof config.excludeExtensions === 'undefined') { | |
config.excludeExtensions = [] | |
} | |
return config as ResolvedPluginConfig; | |
} | |
export default function fixDoubleRefresh(config?: PluginConfig): FixDoubleRefreshPlugin { | |
const pluginConfig = resolvePluginConfig(config); | |
return { | |
name: 'Fix Double Refresh Tailwind Plugin', | |
pluginConfig: pluginConfig, | |
handleHotUpdate(ctx) { | |
const extensionMatched = pluginConfig.excludeExtensions.some((e) => ctx.file.endsWith(e)) | |
if (extensionMatched) return [] | |
return [...ctx.modules[0]?.importers ?? [], ...ctx.modules]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by Vite Livewire Plugin