diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/index.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/index.ts new file mode 100644 index 0000000000..e565ff1722 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/index.ts @@ -0,0 +1,3 @@ +export function greeting(name: string) { + return `Hello ${name}`; +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/package.json new file mode 100644 index 0000000000..5940efd7c5 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/package.json @@ -0,0 +1,5 @@ +{ + "name": "pack-watch-config", + "private": true, + "type": "module" +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots.toml new file mode 100644 index 0000000000..7e3a125720 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots.toml @@ -0,0 +1,5 @@ +[[case]] +name = "pack_watch_reloads_config" +vp = "local" +comment = "Changing pack.minify while watching applies the new configuration, including on Windows." +steps = [["node", "verify.mjs"]] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots/pack_watch_reloads_config.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots/pack_watch_reloads_config.md new file mode 100644 index 0000000000..ce8f37be56 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/snapshots/pack_watch_reloads_config.md @@ -0,0 +1,9 @@ +# pack_watch_reloads_config + +Changing pack.minify while watching applies the new configuration, including on Windows. + +## `node verify.mjs` + +``` +pack --watch applied the updated minify option +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/verify.mjs b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/verify.mjs new file mode 100644 index 0000000000..8bfa997002 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/verify.mjs @@ -0,0 +1,47 @@ +import assert from 'node:assert/strict'; +import { fork } from 'node:child_process'; +import { once } from 'node:events'; +import { readFile, readdir, writeFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import { dirname, join } from 'node:path'; + +const require = createRequire(import.meta.url); +const cli = join(dirname(require.resolve('vite-plus/package.json')), 'dist/pack-bin.js'); +const child = fork(cli, ['--watch'], { silent: true }); +let logs = ''; +child.stdout.on('data', (data) => { + logs += data; +}); +child.stderr.on('data', (data) => { + logs += data; +}); + +try { + // The onSuccess IPC message synchronizes edits with completed builds on every platform. + await once(child, 'message', { signal: AbortSignal.timeout(20000) }); + const output = join('dist', (await readdir('dist')).find((file) => /\.m?js$/.test(file))); + const before = await readFile(output, 'utf8'); + assert.match(before, /\n/); + + const rebuilt = once(child, 'message', { signal: AbortSignal.timeout(20000) }); + const config = await readFile('vite.config.ts', 'utf8'); + await writeFile('vite.config.ts', config.replace('minify: false', 'minify: true')); + await rebuilt; + + const after = await readFile(output, 'utf8'); + assert.notEqual(after, before, 'the rebuilt output must use the updated pack configuration'); + assert.ok( + after.trim().split('\n').length < before.trim().split('\n').length, + 'the rebuilt output must be minified', + ); + console.log('pack --watch applied the updated minify option'); +} catch (error) { + console.error(logs); + throw error; +} finally { + if (child.exitCode === null && child.signalCode === null) { + const exited = once(child, 'exit'); + child.kill(); + await exited; + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/vite.config.ts b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/vite.config.ts new file mode 100644 index 0000000000..e350ab1beb --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/pack_watch_config/vite.config.ts @@ -0,0 +1,10 @@ +export default { + pack: { + entry: ['index.ts'], + dts: false, + minify: false, + onSuccess() { + process.send?.('built'); + }, + }, +}; diff --git a/packages/cli/src/pack-bin.ts b/packages/cli/src/pack-bin.ts index e7ff631a2e..f788d44bf2 100644 --- a/packages/cli/src/pack-bin.ts +++ b/packages/cli/src/pack-bin.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node import module from 'node:module'; +import path from 'node:path'; import { cac } from 'cac'; import { @@ -155,7 +156,8 @@ cli const configDeps = new Set(); if (viteConfig?.configFile) { - configDeps.add(viteConfig.configFile); + // Vite uses forward slashes, but watcher events use native path separators. + configDeps.add(path.normalize(viteConfig.configFile)); } const configs: ResolvedConfig[] = [];