Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): watch publicDir explicitly to include it outside the root #16502

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 62 additions & 1 deletion packages/vite/src/node/server/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
import { describe, expect, it } from 'vitest'
import { resolve } from 'node:path'
import {
type MockInstance,
afterEach,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import chokidar from 'chokidar'
import { createServer } from '../index'

const stubGetWatchedCode = /getWatched\(\) \{.+?return \{\};.+?\}/s

let watchSpy: MockInstance<
Parameters<typeof chokidar.watch>,
ReturnType<typeof chokidar.watch>
>

vi.mock('../../config', async () => {
const config: typeof import('../../config') =
await vi.importActual('../../config')
const resolveConfig = config.resolveConfig
vi.spyOn(config, 'resolveConfig').mockImplementation(async (...args) => {
const resolved: Awaited<ReturnType<typeof resolveConfig>> =
await resolveConfig.call(config, ...args)
resolved.configFileDependencies.push(
resolve('fake/config/dependency.js').replace(/\\/g, '/'),
)
return resolved
})
return config
})

describe('watcher configuration', () => {
beforeEach(() => {
watchSpy = vi.spyOn(chokidar, 'watch')
})

afterEach(() => {
watchSpy.mockRestore()
})

it('when watcher is disabled, return noop watcher', async () => {
const server = await createServer({
server: {
Expand All @@ -21,4 +59,27 @@ describe('watcher configuration', () => {
})
expect(server.watcher.getWatched.toString()).not.toMatch(stubGetWatchedCode)
})

it('should watch the root directory, config file dependencies, dotenv files, and the public directory', async () => {
await createServer({
server: {
watch: {},
},
publicDir: '__test_public__',
})
expect(watchSpy).toHaveBeenLastCalledWith(
expect.arrayContaining(
[
process.cwd(),
resolve('fake/config/dependency.js'),
resolve('.env'),
resolve('.env.local'),
resolve('.env.development'),
resolve('.env.development.local'),
resolve('__test_public__'),
].map((file) => file.replace(/\\/g, '/')),
),
expect.anything(),
)
})
})
10 changes: 6 additions & 4 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ export async function _createServer(
config.server.hmr.channels.forEach((channel) => hot.addChannel(channel))
}

const publicFiles = await initPublicFilesPromise
const { publicDir } = config

if (httpServer) {
setClientErrorHandler(httpServer, config.logger)
}
Expand All @@ -479,6 +482,9 @@ export async function _createServer(
root,
...config.configFileDependencies,
...getEnvFilesForMode(config.mode, config.envDir),
// Watch the public directory explicitly because it might be outside
// of the root directory.
...(publicDir && publicFiles ? [publicDir] : []),
],
resolvedWatchOptions,
) as FSWatcher)
Expand Down Expand Up @@ -745,8 +751,6 @@ export async function _createServer(
}
}

const publicFiles = await initPublicFilesPromise

const onHMRUpdate = async (
type: 'create' | 'delete' | 'update',
file: string,
Expand All @@ -763,8 +767,6 @@ export async function _createServer(
}
}

const { publicDir } = config

const onFileAddUnlink = async (file: string, isUnlink: boolean) => {
file = normalizePath(file)
await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' })
Expand Down