diff --git a/.gitignore b/.gitignore index d044ec9..536f375 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,8 @@ dist-ssr *.sw? # Plugin hot file -hot \ No newline at end of file +hot + +# Java +target/ +build/ \ No newline at end of file diff --git a/examples/vanilla/.env b/examples/vanilla/.env index fb6e516..5d9f3ab 100644 --- a/examples/vanilla/.env +++ b/examples/vanilla/.env @@ -1,5 +1,2 @@ -# Custom app url -# APP_URL= - # Filter debug logs # VITE_DEBUG_FILTER= \ No newline at end of file diff --git a/examples/vanilla/src/main/resources/application.properties b/examples/vanilla/src/main/resources/application.properties new file mode 100644 index 0000000..9e8baa6 --- /dev/null +++ b/examples/vanilla/src/main/resources/application.properties @@ -0,0 +1,2 @@ +app.url=http://localhost:8080 +app.name=Vite Java App \ No newline at end of file diff --git a/examples/vanilla/vite.config.ts b/examples/vanilla/vite.config.ts index 4b6fbd7..228f56d 100644 --- a/examples/vanilla/vite.config.ts +++ b/examples/vanilla/vite.config.ts @@ -1,8 +1,8 @@ import { defineConfig } from 'vite' import java, { createRollupInputConfig } from 'vite-plugin-java' -export default defineConfig(() => ({ +export default defineConfig({ plugins: [java({ input: createRollupInputConfig(), })], -})) +}) diff --git a/packages/vite-plugin-java/src/index.ts b/packages/vite-plugin-java/src/index.ts index e1cbee4..3b5c0ad 100644 --- a/packages/vite-plugin-java/src/index.ts +++ b/packages/vite-plugin-java/src/index.ts @@ -60,6 +60,6 @@ export interface VitePluginJavaConfig { export type DevServerUrl = `${'http' | 'https'}://${string}:${number}` -export { PLUGIN_NAME, createRollupInputConfig } from './utils' +export { PLUGIN_NAME, createRollupInputConfig, readPropertiesFile } from './utils' export { java } export default java diff --git a/packages/vite-plugin-java/src/utils.ts b/packages/vite-plugin-java/src/utils.ts index cac8e33..e017160 100644 --- a/packages/vite-plugin-java/src/utils.ts +++ b/packages/vite-plugin-java/src/utils.ts @@ -4,6 +4,7 @@ import fs from 'node:fs' import { fileURLToPath } from 'node:url' import type { AddressInfo } from 'node:net' import debug from 'debug' +import type { GlobOptions } from 'glob' import { globSync } from 'glob' import 'dotenv/config' import { merge } from 'smob' @@ -101,3 +102,26 @@ export function isMavenProject(): boolean { export function isGradleProject(): boolean { return fs.existsSync(path.join(process.cwd(), 'build.gradle')) } + +/** + * Reads the properties from the project files. + * + * @returns A map containing the key-value pairs of the properties. + */ +export function readPropertiesFile(pattern?: string | string[], options?: GlobOptions): Map { + const properties: Map = new Map() + const _pattern = pattern || '**/*.properties' + const _options = merge({}, options || {}, { cwd: process.cwd() }) + + globSync(_pattern, _options).forEach((file) => { + const _file = typeof file === 'string' ? file : file.path + const content = fs.readFileSync(_file, 'utf-8') + content.split('\n').filter(c => !c?.trim().startsWith('#')).forEach((line) => { + const [key, value] = line.split('=') + if (key) + properties.set(key?.trim(), value?.trim()) + }) + }) + + return properties +} diff --git a/packages/vite-plugin-java/src/vite-plugin-java.ts b/packages/vite-plugin-java/src/vite-plugin-java.ts index a3f574a..aa02834 100644 --- a/packages/vite-plugin-java/src/vite-plugin-java.ts +++ b/packages/vite-plugin-java/src/vite-plugin-java.ts @@ -8,7 +8,7 @@ import { loadEnv } from 'vite' import { merge } from 'smob' import colors from 'picocolors' import swc from '@rollup/plugin-swc' -import { PLUGIN_NAME, createDebugger, dirname, isGradleProject, isIpv6, isMavenProject } from './utils' +import { PLUGIN_NAME, createDebugger, dirname, isGradleProject, isIpv6, isMavenProject, readPropertiesFile } from './utils' import type { DevServerUrl, VitePluginJavaConfig } from '.' const debug = createDebugger(`${PLUGIN_NAME}`) @@ -120,8 +120,9 @@ function resolveJavaPlugin(pluginConfig: Required): [JavaP } }, configureServer(server) { - const envDir = resolvedConfig.envDir || process.cwd() - const appUrl = loadEnv(resolvedConfig.mode, envDir, 'APP_URL').APP_URL ?? 'undefined' + // TODO: Add support for reading yaml files. + const properties = readPropertiesFile() + const appUrl = properties.get('app.url') ?? 'undefined' server.httpServer?.once('listening', () => { debug?.('server listening.') @@ -136,7 +137,10 @@ function resolveJavaPlugin(pluginConfig: Required): [JavaP setTimeout(() => { server.config.logger.info(`\n ${colors.red(`${colors.bold('JAVA')} ${javaVersion()}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`) server.config.logger.info('') - server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`) + + if (appUrl !== 'undefined') { + server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`) + } }, 100) } }) diff --git a/packages/vite-plugin-java/test/utils.test.ts b/packages/vite-plugin-java/test/utils.test.ts index 2f72e47..f23d19f 100644 --- a/packages/vite-plugin-java/test/utils.test.ts +++ b/packages/vite-plugin-java/test/utils.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import path from 'node:path' import { afterAll, beforeAll, describe, expect, it } from 'vitest' -import { createRollupInputConfig } from '../src/utils' +import { createRollupInputConfig, readPropertiesFile } from '../src/utils' const files = [ 'src/main.ts', @@ -48,4 +48,22 @@ describe('vite-plugin-java - utils', () => { [path.normalize('nested/main')]: getAbsolutePath('src/nested/main.ts'), }) }) + + it('should read properties and return mapped entries', () => { + const expectedProperties = new Map( + [ + ['app.url', 'http://localhost:8080'], + ['app.name', 'Vite Java App'], + ], + ) + let content = '' + + for (const [key, value] of expectedProperties.entries()) { + content += `${key}=${value}\n` + } + fs.writeFileSync('fixtures/application.properties', content) + + const propertiesMap = readPropertiesFile() + expect(propertiesMap).toEqual(expectedProperties) + }) })