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), } }