From faab283f5537152e226201bcc20c9ea804e5707f Mon Sep 17 00:00:00 2001 From: Benny Yen Date: Sun, 12 Oct 2025 21:09:35 +0800 Subject: [PATCH] feat: restore configurable hotfile option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add back the hotfile feature that was removed in commit 588863c. The hotfile is now configurable and can be disabled by setting hotFile to false. The hotfile allows Spring applications to detect when the Vite dev server is running by checking for the existence of the file, which contains the dev server URL. Changes: - Add hotFile option to VitePluginJavaConfig (string | false) - Write hotfile on server start with dev server URL - Clean up hotfile on process exit - Update .gitignore to ignore hot file - Update README with hotfile documentation Closes #2 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .gitignore | 3 +++ packages/vite-plugin-java/README.md | 4 ++++ packages/vite-plugin-java/src/index.ts | 9 +++++++++ packages/vite-plugin-java/src/vite-plugin-java.ts | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/.gitignore b/.gitignore index 5d2ba28..536f375 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,9 @@ dist-ssr *.sln *.sw? +# Plugin hot file +hot + # Java target/ build/ \ No newline at end of file diff --git a/packages/vite-plugin-java/README.md b/packages/vite-plugin-java/README.md index b18dcad..47509c5 100644 --- a/packages/vite-plugin-java/README.md +++ b/packages/vite-plugin-java/README.md @@ -39,6 +39,7 @@ export default defineConfig({ target: 'es2015' } }, + hotFile: 'public/hot', transformOnServe: (code, url) => code.replace('__PLACEHOLDER__', url) }) ] @@ -55,6 +56,7 @@ export default defineConfig({ | `outputDirectory` | `string` | `'dist'` | Directory where the bundle should be written | | `tsCompiler` | `'esbuild'` \| `'swc'` | `'esbuild'` | @experimental TypeScript compiler to use | | `swcOptions` | `SwcOptions` | `{}` | @experimental Options to pass to the SWC compiler | +| `hotFile` | `string` \| `false` | `'public/hot'` | Path to the "hot" file. Set to `false` to disable | | `transformOnServe` | `(code: string, url: DevServerUrl) => string` | `code => code` | Transform the code while serving | ### Example @@ -72,6 +74,7 @@ export default { publicDirectory: 'static', buildDirectory: 'assets', outputDirectory: 'build', + hotFile: 'static/hot', transformOnServe: (code, url) => code.replace('__VITE_URL__', url) }) ] @@ -96,6 +99,7 @@ export interface VitePluginJavaConfig { outputDirectory?: string tsCompiler?: SupportedTSCompiler swcOptions?: SwcOptions + hotFile?: string | false transformOnServe?: (code: string, url: DevServerUrl) => string } diff --git a/packages/vite-plugin-java/src/index.ts b/packages/vite-plugin-java/src/index.ts index 04845a9..3f4364a 100644 --- a/packages/vite-plugin-java/src/index.ts +++ b/packages/vite-plugin-java/src/index.ts @@ -32,6 +32,15 @@ export interface VitePluginJavaConfig { */ javaProjectBase?: string + /** + * The path to the "hot" file. + * + * Set to `false` to disable hot file generation. + * + * @default `${publicDirectory}/hot` + */ + hotFile?: string | false + /** * Transform the code while serving. */ diff --git a/packages/vite-plugin-java/src/vite-plugin-java.ts b/packages/vite-plugin-java/src/vite-plugin-java.ts index 159b376..061fe44 100644 --- a/packages/vite-plugin-java/src/vite-plugin-java.ts +++ b/packages/vite-plugin-java/src/vite-plugin-java.ts @@ -126,6 +126,10 @@ function resolveJavaPlugin(pluginConfig: Required): [JavaP if (isAddressInfo(address)) { viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin as DevServerUrl : resolveDevServerUrl(address, server.config) + if (pluginConfig.hotFile) { + fs.writeFileSync(pluginConfig.hotFile, `${viteDevServerUrl}${server.config.base.replace(/\/$/, '')}`) + } + setTimeout(() => { server.config.logger.info(`\n ${colors.red(`${colors.bold('JAVA')} ${javaVersion(pluginConfig.javaProjectBase)}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`) server.config.logger.info('') @@ -138,6 +142,13 @@ function resolveJavaPlugin(pluginConfig: Required): [JavaP }) if (!exitHandlersBound) { + const clean = () => { + if (pluginConfig.hotFile && fs.existsSync(pluginConfig.hotFile)) { + fs.rmSync(pluginConfig.hotFile) + } + } + + process.on('exit', clean) process.on('SIGINT', () => process.exit()) process.on('SIGTERM', () => process.exit()) process.on('SIGHUP', () => process.exit()) @@ -203,6 +214,7 @@ function resolvePluginConfig(config: string | string[] | VitePluginJavaConfig): publicDirectory: config.publicDirectory ?? 'public', outputDirectory: config.outputDirectory ?? 'dist', javaProjectBase: config.javaProjectBase ?? '.', + hotFile: config.hotFile === false ? false : (config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot')), transformOnServe: config.transformOnServe ?? (code => code), } }