|
1 | 1 | import type { BuildConfig, BuildContext, WatchOptions } from './types' |
2 | | -import { dirname, isAbsolute, join, relative } from 'node:path' |
| 2 | +import { join, relative } from 'node:path' |
3 | 3 | import { watch as chokidarWatch } from 'chokidar' |
4 | 4 | import { consola } from 'consola' |
5 | 5 | import { colors as c } from 'consola/utils' |
@@ -201,67 +201,56 @@ async function getWatchPatterns( |
201 | 201 | ): Promise<string[]> { |
202 | 202 | // If explicit patterns are provided, use them |
203 | 203 | if (watchOptions.include && watchOptions.include.length > 0) { |
204 | | - return watchOptions.include.map(normalizeGlobPattern) |
| 204 | + return watchOptions.include |
205 | 205 | } |
206 | 206 |
|
207 | 207 | // Otherwise, derive patterns from build entries |
208 | | - const patterns = new Set<string>() |
209 | | - |
210 | | - const addPattern = (pattern: string | null | undefined) => { |
211 | | - if (!pattern) { |
212 | | - return |
213 | | - } |
214 | | - patterns.add(normalizeGlobPattern(pattern)) |
215 | | - } |
216 | | - |
217 | | - const addFilePatterns = (inputFile: string) => { |
218 | | - const absoluteInput = resolveToAbsolute(inputFile, ctx) |
219 | | - const filePattern = normalizeRelativeToPkg(absoluteInput, ctx) |
220 | | - if (filePattern && filePattern !== '.') { |
221 | | - addPattern(filePattern) |
222 | | - } |
223 | | - addPattern(getDirGlobPattern(dirname(absoluteInput), ctx)) |
224 | | - } |
225 | | - |
226 | | - const addDirectoryPatterns = (inputDir: string) => { |
227 | | - const absoluteDir = resolveToAbsolute(inputDir, ctx) |
228 | | - addPattern(getDirGlobPattern(absoluteDir, ctx)) |
229 | | - } |
| 208 | + const patterns: string[] = [] |
230 | 209 |
|
231 | 210 | for (const entry of config.entries || []) { |
232 | 211 | if (typeof entry === 'string') { |
233 | 212 | const [input] = entry.split(':') |
234 | 213 | if (input.endsWith('/')) { |
235 | 214 | // Transform entry - watch the entire directory |
236 | | - addDirectoryPatterns(input) |
| 215 | + patterns.push(`${input}**/*`) |
237 | 216 | } |
238 | 217 | else { |
239 | 218 | // Bundle entry - watch the specific files and their dependencies |
240 | 219 | const inputs = input.split(',') |
241 | 220 | for (const inputFile of inputs) { |
242 | | - addFilePatterns(inputFile) |
| 221 | + patterns.push(inputFile) |
| 222 | + // Also watch the directory containing the input file |
| 223 | + const dir = inputFile.substring(0, inputFile.lastIndexOf('/')) |
| 224 | + if (dir) { |
| 225 | + patterns.push(`${dir}/**/*`) |
| 226 | + } |
243 | 227 | } |
244 | 228 | } |
245 | 229 | } |
246 | 230 | else { |
247 | 231 | if (entry.type === 'transform') { |
248 | | - addDirectoryPatterns(entry.input) |
| 232 | + patterns.push(`${entry.input}/**/*`) |
249 | 233 | } |
250 | 234 | else { |
251 | 235 | const inputs = Array.isArray(entry.input) ? entry.input : [entry.input] |
252 | 236 | for (const inputFile of inputs) { |
253 | | - addFilePatterns(inputFile) |
| 237 | + patterns.push(inputFile) |
| 238 | + // Also watch the directory containing the input file |
| 239 | + const dir = inputFile.substring(0, inputFile.lastIndexOf('/')) |
| 240 | + if (dir) { |
| 241 | + patterns.push(`${dir}/**/*`) |
| 242 | + } |
254 | 243 | } |
255 | 244 | } |
256 | 245 | } |
257 | 246 | } |
258 | 247 |
|
259 | 248 | // Add common source patterns if no specific patterns found |
260 | | - if (patterns.size === 0) { |
261 | | - ;['src/**/*', '*.ts', '*.js', '*.mjs', '*.json'].forEach(pattern => addPattern(pattern)) |
| 249 | + if (patterns.length === 0) { |
| 250 | + patterns.push('src/**/*', '*.ts', '*.js', '*.mjs', '*.json') |
262 | 251 | } |
263 | 252 |
|
264 | | - return Array.from(patterns) |
| 253 | + return [...new Set(patterns)] // Remove duplicates |
265 | 254 | } |
266 | 255 |
|
267 | 256 | /** |
@@ -295,34 +284,3 @@ function getIgnorePatterns(config: BuildConfig, watchOptions: WatchOptions): str |
295 | 284 |
|
296 | 285 | return allIgnores |
297 | 286 | } |
298 | | - |
299 | | -function resolveToAbsolute(path: string, ctx: BuildContext): string { |
300 | | - return isAbsolute(path) ? path : join(ctx.pkgDir, path) |
301 | | -} |
302 | | - |
303 | | -function normalizeGlobPattern(pattern: string): string { |
304 | | - return pattern.replace(/\\/g, '/') |
305 | | -} |
306 | | - |
307 | | -function normalizeRelativeToPkg(target: string, ctx: BuildContext): string { |
308 | | - const relativePath = relative(ctx.pkgDir, target) |
309 | | - if (!relativePath || relativePath === '') { |
310 | | - return '.' |
311 | | - } |
312 | | - if (relativePath.includes(':')) { |
313 | | - return normalizeGlobPattern(target) |
314 | | - } |
315 | | - return normalizeGlobPattern(relativePath) |
316 | | -} |
317 | | - |
318 | | -function getDirGlobPattern(directory: string, ctx: BuildContext): string { |
319 | | - const normalizedDir = normalizeRelativeToPkg(directory, ctx) |
320 | | - if (!normalizedDir || normalizedDir === '.' || normalizedDir === './') { |
321 | | - return '**/*' |
322 | | - } |
323 | | - return `${stripTrailingSlash(normalizedDir)}/**/*` |
324 | | -} |
325 | | - |
326 | | -function stripTrailingSlash(path: string): string { |
327 | | - return path.replace(/\/+$/, '') |
328 | | -} |
0 commit comments