Skip to content

[1.x] Hot file monitoring #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: 1.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -195,6 +199,22 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
configResolved(config) {
resolvedConfig = config
},
async handleHotUpdate(update) {
if (update.file !== pluginConfig.hotFile) {
return
}

const content = new Promise<string>((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)
Expand Down Expand Up @@ -359,7 +379,7 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
ssr: config.ssr ?? config.input,
ssrOutputDirectory: config.ssrOutputDirectory ?? 'bootstrap/ssr',
refresh: config.refresh ?? false,
hotFile: config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot'),
hotFile: path.resolve(config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot')),
valetTls: config.valetTls ?? null,
detectTls: config.detectTls ?? config.valetTls ?? null,
transformOnServe: config.transformOnServe ?? ((code) => code),
Expand Down