Skip to content

Commit e4e4684

Browse files
committed
revert: watch mode not work
1 parent 1ab8ff7 commit e4e4684

File tree

1 file changed

+20
-62
lines changed

1 file changed

+20
-62
lines changed

src/watch.ts

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { BuildConfig, BuildContext, WatchOptions } from './types'
2-
import { dirname, isAbsolute, join, relative } from 'node:path'
2+
import { join, relative } from 'node:path'
33
import { watch as chokidarWatch } from 'chokidar'
44
import { consola } from 'consola'
55
import { colors as c } from 'consola/utils'
@@ -201,67 +201,56 @@ async function getWatchPatterns(
201201
): Promise<string[]> {
202202
// If explicit patterns are provided, use them
203203
if (watchOptions.include && watchOptions.include.length > 0) {
204-
return watchOptions.include.map(normalizeGlobPattern)
204+
return watchOptions.include
205205
}
206206

207207
// 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[] = []
230209

231210
for (const entry of config.entries || []) {
232211
if (typeof entry === 'string') {
233212
const [input] = entry.split(':')
234213
if (input.endsWith('/')) {
235214
// Transform entry - watch the entire directory
236-
addDirectoryPatterns(input)
215+
patterns.push(`${input}**/*`)
237216
}
238217
else {
239218
// Bundle entry - watch the specific files and their dependencies
240219
const inputs = input.split(',')
241220
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+
}
243227
}
244228
}
245229
}
246230
else {
247231
if (entry.type === 'transform') {
248-
addDirectoryPatterns(entry.input)
232+
patterns.push(`${entry.input}/**/*`)
249233
}
250234
else {
251235
const inputs = Array.isArray(entry.input) ? entry.input : [entry.input]
252236
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+
}
254243
}
255244
}
256245
}
257246
}
258247

259248
// 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')
262251
}
263252

264-
return Array.from(patterns)
253+
return [...new Set(patterns)] // Remove duplicates
265254
}
266255

267256
/**
@@ -295,34 +284,3 @@ function getIgnorePatterns(config: BuildConfig, watchOptions: WatchOptions): str
295284

296285
return allIgnores
297286
}
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

Comments
 (0)