eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Unable to use addWatchTarget

Open nhoizey opened this issue 5 years ago • 12 comments

I'm trying to use @MadeByMike's addWatchTarget() to react to Sass files changes, but it does nothing.

Here's what I've put in my .eleventy.js file:

eleventyConfig.addWatchTarget("src/_assets/**/*.scss");

And here are my Sass files: https://github.com/nhoizey/nicolas-hoizey.com/tree/master/src/_assets/sass

Any idea why this doesn't work?

Thanks.

nhoizey avatar Jan 30 '20 12:01 nhoizey

I'm wondering the same - it does not seem to be working. Diving into Eleventy internals, I noticed the watch targets are set in the Eleventy constructor, but this happens before the .eleventy.js code even runs. Therefore, the watch targets array is always empty.

If there's anything @nhoizey and I are missing, let us know.

vseventer avatar Feb 19 '20 17:02 vseventer

Ran into this issue as well. I'm attempting to develop Nunjucks macros I'm storing in a module, which I'm accessing via npm link. It'd be nifty to watch the Nunjucks files for changes while I'm developing them. Attempting to addWatchTarget('node_modules/path/to/module/nunjucks-macros/') doesn't trigger a reload when saving a file in that directory.

I can certainly edit macros in my Eleventy project and copy them to my node module, but I'd love saving a step and reducing redundancy.

jordanthornquest avatar Mar 07 '20 00:03 jordanthornquest

I still have this issue. Tried several syntaxes, with glob or not, and nothing is triggered when I change my Sass files… 🤷‍♂️

nhoizey avatar Mar 22 '20 14:03 nhoizey

@nhoizey , not sure if this will help or not, but this is working in my project: eleventyConfig.addWatchTarget("./src/**/*.{js,scss}");

Did you try starting the path with ./?

Paul-Hebert avatar Mar 26 '20 22:03 Paul-Hebert

@Paul-Hebert I did write it like you, without success

nhoizey avatar Mar 29 '20 16:03 nhoizey

Ahh dang, I'm not sure what's going on then 😕

Paul-Hebert avatar Mar 30 '20 16:03 Paul-Hebert

This isn't working for me either, but it was working at some point.

I was targeting the build files (output from webpack), like this:

eleventyConfig.addWatchTarget('./site/build/')

I have also tried targeting the ./src folder but no luck.

acstll avatar Mar 30 '20 18:03 acstll

I noticed 11ty adds .gitignore files and directories in a ignoreFiles list, which probably defeats any addWatchTarget that might conflict with it. In my case, built CSS files are in a _fractal folder that is purposefully excluded from git. No matter how I tried to add this path with addWatchTarget no watching happened. After digging into the code and debugging output, this solved for me:

In .gitignore I had the folder excluded from versioning

/_fractal

This prevented Eleventy from picking it for watching.
Adding this setUseGitIgnore(false) in .eleventy.js solved it:

  eleventyConfig.setUseGitIgnore(false);
  eleventyConfig.addWatchTarget("./_fractal/css/style.css}")

YMMV (if you have template/content heavy sites or markdown/other template files in folders that were previously ignored by .gitignore this might add to build time and/or produce extra pages).

therealpecus avatar Mar 30 '20 21:03 therealpecus

That did it for me, and it explains why it was working (before I added the folder to .gitignore). Thank you @therealpecus

Maybe there should be a note about this (.gitignore) in the documentation regarding addWatchTarget? If so, I could try and contribute a PR.

acstll avatar Mar 31 '20 06:03 acstll

Or perhaps a warning of some level? Something along the lines of:

${file/directory} was flagged as target to watch, but is also on .gitignore (resp. .eleventyignore) so won't be watched.

Ryuno-Ki avatar Mar 31 '20 09:03 Ryuno-Ki

I made a PR for a minor doc update to start. Totally support @Ryuno-Ki proposal but I am short on time at the moment

therealpecus avatar Mar 31 '20 13:03 therealpecus

@therealpecus OMG, thanks for the explanation!

Maybe we could ask Eleventy not to ignore files that are in addWatchTarget even if they are in .gitignore or .eleventyignore?

nhoizey avatar Apr 04 '20 15:04 nhoizey

A long time in the making, but we are now decoupling configuration ignores for watching from ignores for processing.

Docs are building now on: https://www.11ty.dev/docs/watch-serve/#ignore-watching-files

Practically, this means additions to eleventyConfig.ignores will not be ignored by the watcher by default. You will need to add them separately to eleventyConfig.watchIgnores.

zachleat avatar Nov 18 '22 23:11 zachleat

This will ship with v2.0.0-canary.18

zachleat avatar Nov 18 '22 23:11 zachleat