* @param {string | string[]} watchPath watch path * @param {WatchOptions=} watchOptions watch options * @returns {Promise }
(watchPath, watchOptions = {})
| 3411 | * @returns {Promise<void>} |
| 3412 | */ |
| 3413 | async watchFiles(watchPath, watchOptions = {}) { |
| 3414 | const { default: chokidar } = await import("chokidar"); |
| 3415 | const { globSync, isDynamicPattern } = await import("tinyglobby"); |
| 3416 | |
| 3417 | const isWin = path.sep === "\\"; |
| 3418 | const toPosix = (/** @type {string} */ filePath) => |
| 3419 | isWin ? filePath.split(path.sep).join("/") : filePath; |
| 3420 | const toNative = (/** @type {string} */ filePath) => |
| 3421 | isWin ? filePath.split("/").join(path.sep) : filePath; |
| 3422 | |
| 3423 | const cwd = watchOptions.cwd ? toPosix(watchOptions.cwd) : undefined; |
| 3424 | |
| 3425 | const expand = (/** @type {string} */ item) => { |
| 3426 | const posix = toPosix(item); |
| 3427 | return isDynamicPattern(posix) |
| 3428 | ? globSync(posix, { cwd, absolute: true }).map(toNative) |
| 3429 | : item; |
| 3430 | }; |
| 3431 | |
| 3432 | const resolveGlobs = (/** @type {string | string[]} */ input) => |
| 3433 | (Array.isArray(input) ? input : [input]).flatMap((item) => |
| 3434 | typeof item === "string" ? expand(item) : item, |
| 3435 | ); |
| 3436 | |
| 3437 | const resolvedPaths = resolveGlobs(watchPath); |
| 3438 | |
| 3439 | if (typeof watchOptions.ignored === "string") { |
| 3440 | watchOptions.ignored = resolveGlobs(watchOptions.ignored); |
| 3441 | } else if (Array.isArray(watchOptions.ignored)) { |
| 3442 | watchOptions.ignored = watchOptions.ignored.flatMap((item) => |
| 3443 | typeof item === "string" ? expand(item) : item, |
| 3444 | ); |
| 3445 | } |
| 3446 | |
| 3447 | const watcher = chokidar.watch(resolvedPaths, watchOptions); |
| 3448 | |
| 3449 | // disabling refreshing on changing the content |
| 3450 | if (this.options.liveReload) { |
| 3451 | watcher.on("change", (item) => { |
| 3452 | if (this.webSocketServer) { |
| 3453 | this.sendMessage( |
| 3454 | this.webSocketServer.clients, |
| 3455 | "static-changed", |
| 3456 | item, |
| 3457 | ); |
| 3458 | } |
| 3459 | }); |
| 3460 | } |
| 3461 | |
| 3462 | this.staticWatchers.push(watcher); |
| 3463 | } |
| 3464 | |
| 3465 | /** |
| 3466 | * @param {import("webpack-dev-middleware").Callback=} callback callback |
no test coverage detected