Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2e7f49

Browse files
authored
feat: restore configurable hotfile option (#3)
## Summary This PR restores the hotfile feature that was removed in commit 588863c, addressing issue #2. The hotfile is now configurable and can be disabled by setting `hotFile` to `false`. ## Why is this needed? The hotfile allows Spring applications to detect when the Vite dev server is running. Spring can check for the existence of this file to determine whether to: - Load assets from the Vite dev server (development mode) - Load assets from the build directory (production mode) ## Changes - ✅ Add `hotFile` configuration option to `VitePluginJavaConfig` (`string | false`) - ✅ Write hotfile on server start containing the dev server URL - ✅ Clean up hotfile automatically on process exit - ✅ Update `.gitignore` to ignore the `hot` file - ✅ Update README with hotfile documentation and examples ## Configuration The hotfile is enabled by default at `public/hot`, but can be customized or disabled: \`\`\`typescript java({ input: 'src/main.ts', hotFile: 'static/hot', // Custom path // or hotFile: false, // Disable hotfile generation }) \`\`\` ## Test Plan - [x] Lint passes - [x] Type checks pass - [x] All tests pass - [x] Feature tested manually Closes #2 🤖 Generated with [Claude Code](https://claude.com/claude-code)
2 parents 9946044 + faab283 commit c2e7f49

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ dist-ssr
2323
*.sln
2424
*.sw?
2525

26+
# Plugin hot file
27+
hot
28+
2629
# Java
2730
target/
2831
build/

packages/vite-plugin-java/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default defineConfig({
3939
target: 'es2015'
4040
}
4141
},
42+
hotFile: 'public/hot',
4243
transformOnServe: (code, url) => code.replace('__PLACEHOLDER__', url)
4344
})
4445
]
@@ -55,6 +56,7 @@ export default defineConfig({
5556
| `outputDirectory` | `string` | `'dist'` | Directory where the bundle should be written |
5657
| `tsCompiler` | `'esbuild'` \| `'swc'` | `'esbuild'` | @experimental TypeScript compiler to use |
5758
| `swcOptions` | `SwcOptions` | `{}` | @experimental Options to pass to the SWC compiler |
59+
| `hotFile` | `string` \| `false` | `'public/hot'` | Path to the "hot" file. Set to `false` to disable |
5860
| `transformOnServe` | `(code: string, url: DevServerUrl) => string` | `code => code` | Transform the code while serving |
5961

6062
### Example
@@ -72,6 +74,7 @@ export default {
7274
publicDirectory: 'static',
7375
buildDirectory: 'assets',
7476
outputDirectory: 'build',
77+
hotFile: 'static/hot',
7578
transformOnServe: (code, url) => code.replace('__VITE_URL__', url)
7679
})
7780
]
@@ -96,6 +99,7 @@ export interface VitePluginJavaConfig {
9699
outputDirectory?: string
97100
tsCompiler?: SupportedTSCompiler
98101
swcOptions?: SwcOptions
102+
hotFile?: string | false
99103
transformOnServe?: (code: string, url: DevServerUrl) => string
100104
}
101105

packages/vite-plugin-java/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export interface VitePluginJavaConfig {
3232
*/
3333
javaProjectBase?: string
3434

35+
/**
36+
* The path to the "hot" file.
37+
*
38+
* Set to `false` to disable hot file generation.
39+
*
40+
* @default `${publicDirectory}/hot`
41+
*/
42+
hotFile?: string | false
43+
3544
/**
3645
* Transform the code while serving.
3746
*/

packages/vite-plugin-java/src/vite-plugin-java.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): [JavaP
126126
if (isAddressInfo(address)) {
127127
viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin as DevServerUrl : resolveDevServerUrl(address, server.config)
128128

129+
if (pluginConfig.hotFile) {
130+
fs.writeFileSync(pluginConfig.hotFile, `${viteDevServerUrl}${server.config.base.replace(/\/$/, '')}`)
131+
}
132+
129133
setTimeout(() => {
130134
server.config.logger.info(`\n ${colors.red(`${colors.bold('JAVA')} ${javaVersion(pluginConfig.javaProjectBase)}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`)
131135
server.config.logger.info('')
@@ -138,6 +142,13 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): [JavaP
138142
})
139143

140144
if (!exitHandlersBound) {
145+
const clean = () => {
146+
if (pluginConfig.hotFile && fs.existsSync(pluginConfig.hotFile)) {
147+
fs.rmSync(pluginConfig.hotFile)
148+
}
149+
}
150+
151+
process.on('exit', clean)
141152
process.on('SIGINT', () => process.exit())
142153
process.on('SIGTERM', () => process.exit())
143154
process.on('SIGHUP', () => process.exit())
@@ -203,6 +214,7 @@ function resolvePluginConfig(config: string | string[] | VitePluginJavaConfig):
203214
publicDirectory: config.publicDirectory ?? 'public',
204215
outputDirectory: config.outputDirectory ?? 'dist',
205216
javaProjectBase: config.javaProjectBase ?? '.',
217+
hotFile: config.hotFile === false ? false : (config.hotFile ?? path.join((config.publicDirectory ?? 'public'), 'hot')),
206218
transformOnServe: config.transformOnServe ?? (code => code),
207219
}
208220
}

0 commit comments

Comments
 (0)