Skip to content

Commit 19bb74b

Browse files
committed
feat(utils): enhance glob pattern handling in createRollupInputConfig
1 parent 813b1ac commit 19bb74b

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { AddressInfo } from 'node:net'
66
import debug from 'debug'
77
import { globSync } from 'glob'
88
import 'dotenv/config'
9+
import { merge } from 'smob'
910

1011
export const PLUGIN_NAME = 'vite-plugin-java'
1112
const filter = process.env.VITE_DEBUG_FILTER
@@ -32,45 +33,71 @@ export function createDebugger(
3233
}
3334

3435
/**
35-
* Creates a Rollup input configuration object based on the specified pattern and base directory.
36+
* Creates the Rollup input configuration object.
3637
*
37-
* @param pattern - The file pattern to match for input files. Defaults to src/~/main.ts
38-
* @param baseDir - The base directory for the input files. Defaults to 'src'.
38+
* @param pattern - The glob pattern to match the entry files.
39+
* @param baseDir - The base directory for the entry files.
40+
* @param options - Additional options for the glob pattern matching.
3941
* @returns The Rollup input configuration object.
4042
*/
4143
export function createRollupInputConfig(
4244
pattern: string | string[] = 'src/**/main.ts',
4345
baseDir = 'src',
46+
options: Parameters<typeof globSync>[1] = {},
4447
): { [entryAlias: string]: string } {
48+
const cwd = process.cwd()
49+
const globOptions = merge(options, { cwd })
50+
4551
return Object.fromEntries(
46-
globSync(pattern).map((file) => {
52+
globSync(pattern, globOptions).map((file) => {
53+
const filepath: string = typeof file === 'string' ? file : file.path
54+
const absolutePath = path.resolve(cwd, filepath)
55+
4756
return [
48-
path.relative(baseDir, path.basename(file, path.extname(file))),
49-
fileURLToPath(new URL(file, import.meta.url)),
57+
path.relative(baseDir, filepath.slice(0, filepath.length - path.extname(filepath).length)),
58+
absolutePath,
5059
]
5160
}),
5261
)
5362
}
5463

64+
/**
65+
* Checks if the given address is an IPv6 address.
66+
*
67+
* @param address - The address to check.
68+
* @returns True if the address is an IPv6 address, false otherwise.
69+
*/
5570
export function isIpv6(address: AddressInfo): boolean {
5671
return address.family === 'IPv6'
57-
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version.
58-
// See: https://github.com/laravel/vite-plugin/issues/103
59-
// @ts-expect-error-next-line
72+
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version.
73+
// See: https://github.com/laravel/vite-plugin/issues/103
74+
// @ts-expect-error-next-line
6075
|| address.family === 6
6176
}
6277

6378
/**
64-
* The directory of the current file.
79+
* Returns the directory of the current file.
80+
*
81+
* @returns The directory of the current file.
6582
*/
6683
export function dirname(): string {
6784
return fileURLToPath(new URL('.', import.meta.url))
6885
}
6986

87+
/**
88+
* Checks if the current project is a Maven project.
89+
*
90+
* @returns True if the current project is a Maven project, false otherwise.
91+
*/
7092
export function isMavenProject(): boolean {
7193
return fs.existsSync(path.join(process.cwd(), 'pom.xml'))
7294
}
7395

96+
/**
97+
* Checks if the current project is a Gradle project.
98+
*
99+
* @returns True if the current project is a Gradle project, false otherwise.
100+
*/
74101
export function isGradleProject(): boolean {
75102
return fs.existsSync(path.join(process.cwd(), 'build.gradle'))
76103
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'node:fs'
33
import type { Buffer } from 'node:buffer'
44
import type { AddressInfo } from 'node:net'
55
import path from 'node:path'
6-
import type { Plugin, ResolvedConfig, UserConfig } from 'vite'
6+
import type { ConfigEnv, Plugin, ResolvedConfig, UserConfig } from 'vite'
77
import { loadEnv } from 'vite'
88
import { merge } from 'smob'
99
import colors from 'picocolors'
@@ -15,12 +15,16 @@ const debug = createDebugger(`${PLUGIN_NAME}`)
1515

1616
let exitHandlersBound = false
1717

18+
interface JavaPlugin extends Plugin {
19+
config: (config: UserConfig, env: ConfigEnv) => UserConfig
20+
}
21+
1822
/**
1923
* Java plugin for Vite.
2024
*
2125
* @param config - A config object or relative path(s) of the scripts to be compiled.
2226
*/
23-
export function java(config: string | string[] | VitePluginJavaConfig): Plugin[] {
27+
export function java(config: string | string[] | VitePluginJavaConfig): [JavaPlugin, ...Plugin[]] {
2428
debug?.(`${PLUGIN_NAME} plugin initialized.`)
2529
debug?.('config, %O', config)
2630
const pluginConfig = resolvePluginConfig(config)
@@ -32,7 +36,7 @@ export function java(config: string | string[] | VitePluginJavaConfig): Plugin[]
3236
/**
3337
* Resolve the Java plugin configuration.
3438
*/
35-
function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): Plugin[] {
39+
function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): [JavaPlugin, ...Plugin[]] {
3640
debug?.('start resolving plugins.')
3741
let viteDevServerUrl: DevServerUrl
3842
let resolvedConfig: ResolvedConfig
@@ -41,7 +45,7 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): Plugin
4145
const defaultAliases: Record<string, string> = {
4246
'@': '/src',
4347
}
44-
const plugins: Plugin[] = [
48+
const plugins: [JavaPlugin, ...Plugin[]] = [
4549
{
4650
name: PLUGIN_NAME,
4751
enforce: 'post',
@@ -55,7 +59,7 @@ function resolveJavaPlugin(pluginConfig: Required<VitePluginJavaConfig>): Plugin
5559
: undefined
5660

5761
return {
58-
base: userConfig.base ?? (mode === 'build' ? resolveBase(pluginConfig, assetUrl) : ''),
62+
base: userConfig.base ?? (command === 'build' ? resolveBase(pluginConfig, assetUrl) : ''),
5963
publicDir: userConfig.publicDir ?? pluginConfig.publicDirectory ?? false,
6064
build: {
6165
manifest: userConfig.build?.manifest ?? 'manifest.json',

0 commit comments

Comments
 (0)