Skip to content

Commit

Permalink
fix(dev): watch publicDir explicitly to include it outside the root (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shakeskeyboarde authored Apr 23, 2024
1 parent 7171837 commit 4d83eb5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
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

0 comments on commit 4d83eb5

Please sign in to comment.