Skip to content

Commit

Permalink
fix(watcher): ensure only add normalized file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 25, 2021
1 parent 3d55e83 commit a19c456
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
6 changes: 1 addition & 5 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ export async function createServer(

const { ignored = [], ...watchOptions } = serverConfig.watch || {}
const watcher = chokidar.watch(root, {
ignored: [
'**/node_modules/**',
'**/.git/**',
...ignored
],
ignored: ['**/node_modules/**', '**/.git/**', ...ignored],
ignoreInitial: true,
ignorePermissionErrors: true,
...watchOptions
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import MagicString from 'magic-string'
import { FSWatcher } from 'chokidar'
import {
createDebugger,
ensureWatchedFile,
generateCodeFrame,
isExternalUrl,
normalizePath,
Expand Down Expand Up @@ -205,10 +206,7 @@ export async function createPluginContainer(

addWatchFile(id: string) {
watchFiles.add(id)
// only need to add it if file is out of root.
if (watcher && !id.startsWith(root)) {
watcher.add(id)
}
if (watcher) ensureWatchedFile(watcher, id, root)
}

getWatchFiles() {
Expand Down
13 changes: 3 additions & 10 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
cleanUrl,
prettifyUrl,
removeTimestampQuery,
timeFrom
timeFrom,
ensureWatchedFile
} from '../utils'
import { checkPublicFile } from '../plugins/asset'
import { ssrTransform } from '../ssr/ssrTransform'
Expand Down Expand Up @@ -112,15 +113,7 @@ export async function transformRequest(

// ensure module in graph after successful load
const mod = await moduleGraph.ensureEntryFromUrl(url)
// file is out of root, add it to the watch list
if (
mod.file &&
!mod.file.startsWith(root + '/') &&
// some rollup plugins use null bytes for private resolved Ids
!mod.file.includes('\0')
) {
watcher.add(mod.file)
}
ensureWatchedFile(watcher, mod.file, root)

// transform
const transformStart = Date.now()
Expand Down
19 changes: 19 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import slash from 'slash'
import { FS_PREFIX, SUPPORTED_EXTS } from './constants'
import resolve from 'resolve'
import builtins from 'builtin-modules'
import { FSWatcher } from 'chokidar'

export function isBuiltin(id: string): boolean {
return builtins.includes(id)
Expand Down Expand Up @@ -320,3 +321,21 @@ export function copyDir(srcDir: string, destDir: string) {
}
}
}

export function ensureWatchedFile(
watcher: FSWatcher,
file: string | null,
root: string
) {
if (
file &&
// only need to watch if out of root
!file.startsWith(root + '/') &&
// some rollup plugins use null bytes for private resolved Ids
!file.includes('\0') &&
fs.existsSync(file)
) {
// resolve file to normalized system path
watcher.add(path.resolve(file))
}
}

0 comments on commit a19c456

Please sign in to comment.