Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ dist-ssr
*.sln
*.sw?

# Plugin hot file
hot

# Java
target/
build/
4 changes: 4 additions & 0 deletions packages/vite-plugin-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default defineConfig({
target: 'es2015'
}
},
hotFile: 'public/hot',
transformOnServe: (code, url) => code.replace('__PLACEHOLDER__', url)
})
]
Expand All @@ -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
Expand All @@ -72,6 +74,7 @@ export default {
publicDirectory: 'static',
buildDirectory: 'assets',
outputDirectory: 'build',
hotFile: 'static/hot',
transformOnServe: (code, url) => code.replace('__VITE_URL__', url)
})
]
Expand All @@ -96,6 +99,7 @@ export interface VitePluginJavaConfig {
outputDirectory?: string
tsCompiler?: SupportedTSCompiler
swcOptions?: SwcOptions
hotFile?: string | false
transformOnServe?: (code: string, url: DevServerUrl) => string
}

Expand Down
9 changes: 9 additions & 0 deletions packages/vite-plugin-java/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/vite-plugin-java/src/vite-plugin-java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): [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('')
Expand All @@ -138,6 +142,13 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): [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())
Comment on lines 152 to 154
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup function should also be called for the other process exit handlers to ensure the hotfile is cleaned up in all exit scenarios.

Suggested change
process.on('SIGINT', () => process.exit())
process.on('SIGTERM', () => process.exit())
process.on('SIGHUP', () => process.exit())
process.on('SIGINT', () => { clean(); process.exit() })
process.on('SIGTERM', () => { clean(); process.exit() })
process.on('SIGHUP', () => { clean(); process.exit() })

Copilot uses AI. Check for mistakes.
Comment on lines 152 to 154
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These signal handlers should call the cleanup function before exiting to ensure the hotfile is removed when the process is terminated by signals.

Suggested change
process.on('SIGINT', () => process.exit())
process.on('SIGTERM', () => process.exit())
process.on('SIGHUP', () => process.exit())
process.on('SIGINT', () => { clean(); process.exit() })
process.on('SIGTERM', () => { clean(); process.exit() })
process.on('SIGHUP', () => { clean(); process.exit() })

Copilot uses AI. Check for mistakes.
Expand Down Expand Up @@ -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),
}
}
Expand Down