diff --git a/src/index.ts b/src/index.ts index 45a3fe4..119d365 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,10 +4,14 @@ import os from 'os' import { fileURLToPath } from 'url' import path from 'path' import colors from 'picocolors' -import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite' +import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption, createLogger } from 'vite' import fullReload, { Config as FullReloadConfig } from 'vite-plugin-full-reload' import { InputOption } from "rollup" +const logger = createLogger('info', { + prefix: '[laravel-vite-plugin]' +}) + interface PluginConfig { /** * The path or paths of the entry points to compile. @@ -195,6 +199,22 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug configResolved(config) { resolvedConfig = config }, + async handleHotUpdate(update) { + if (update.file !== pluginConfig.hotFile) { + return + } + + const content = new Promise((resolve) => resolve(update.read())) + + viteDevServerUrl = (await content).trim() as DevServerUrl + + update.server.ws.send({ type: 'full-reload' }) + + logger.info(`${colors.green('page reload')} Hot file changed ${colors.dim(viteDevServerUrl)}`, { + timestamp: true, + clear: true, + }) + }, transform(code) { if (resolvedConfig.command === 'serve') { code = code.replace(/http:\/\/__laravel_vite_placeholder__\.test/g, viteDevServerUrl) @@ -359,7 +379,7 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required code),