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

Can't affirmatively specify files being watched #7850

Closed
7 tasks done
jcushman opened this issue Apr 21, 2022 · 2 comments
Closed
7 tasks done

Can't affirmatively specify files being watched #7850

jcushman opened this issue Apr 21, 2022 · 2 comments

Comments

@jcushman
Copy link

Describe the bug

I have a Django project with a large number of files in the working directory, so if I run vite dev the file watcher takes a lot of CPU and soon runs out of file handles. To avoid this I would like to specify particular paths for Vite to watch.

Two previous requests for this feature were closed because a workaround exists (here and here). But I can't figure out what the workaround is (and in one case I don't think they found one, just used a manual blocklist).

My initial thought from the watch.ignored documentation was that something like this in npm create vite@latest vite-base -- --template vue should work to restrict the watcher to the src directory:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    watch: {
      ignored: [
        '**',
        '!**/src/**',
        '!**/vite.config.js'
      ],
    }
  },
})

But when I run this in the vanilla vue template, changes to all files are ignored. I tried some variations like including absolute paths (see reproduction link) and couldn't get those to work either.

Can others reproduce this problem, or am I messing something up? If this workaround doesn't work, maybe it's worth revisiting the option to override paths passed to chokidar.watch.

(In the meantime I think my workaround is to list the directory in js and provide everything to ignored except the desired paths. Messy, but better than having to manually maintain a list of Django files or rearrange Django's directory structure.)

((Eagerly awaiting the obvious thing I've missed. 😆))

Reproduction

https://stackblitz.com/edit/vitejs-vite-voaybn?file=vite.config.js

System Info

System:
    OS: macOS 12.2.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 1001.38 MB / 32.00 GB
    Shell: 3.3.1 - /usr/local/bin/fish
  Binaries:
    Node: 17.9.0 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 8.5.5 - /usr/local/bin/npm
  Browsers:
    Chrome: 100.0.4896.127
    Edge: 100.0.1185.39
    Firefox: 99.0.1
    Firefox Developer Edition: 100.0
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-vue: ^2.3.1 => 2.3.1
    vite: ^2.9.5 => 2.9.5

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

Vite does nothing special with this.

const { ignored = [], ...watchOptions } = serverConfig.watch || {}
const watcher = chokidar.watch(path.resolve(root), {
ignored: [
'**/node_modules/**',
'**/.git/**',
...(Array.isArray(ignored) ? ignored : [ignored])
],
ignoreInitial: true,
ignorePermissionErrors: true,
disableGlobbing: true,
...watchOptions
}) as FSWatcher

I dont know if this behavior is intended by chokidar.
But you can use a function to achieve what you are looking for.

import { defineConfig, normalizePath } from 'vite'
import * as path from 'path'
import * as url from 'url'

export default defineConfig({
  server: {
    watch: {
      ignored: (p) => {
        const relativePath = path.relative(
          path.resolve(url.fileURLToPath(import.meta.url), '..'),
          p
        )
        return (
          relativePath !== '' &&
          relativePath !== 'src' &&
          !normalizePath(relativePath).startsWith('src/')
        )
      }
    }
  }
})

@bluwy
Copy link
Member

bluwy commented May 3, 2022

Agree with @sapphi-red too. It seems that chokidar doesn't support the negation pattern for ignored, using a function like suggested is the way to go.

@bluwy bluwy closed this as completed May 3, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants