node-sass icon indicating copy to clipboard operation
node-sass copied to clipboard

Exposing watch in API

Open baileyparker opened this issue 9 years ago • 12 comments

I've read through the issues about the --watch flag and appreciate the amount of effort that has been put into maintaining it! :smile: The implementation is impressively terse and as I understand it (the PR mentioning this was merged), has recently been overhauled.

Would it be possible to expose this in the Node API so that it could be invoked programmatically (from a task runner like Gulp or Grunt, or just in plain Node) through something like sass.watch('./file.scss')?

In the case of task runners, this could allow allow for an evented API that allows for additional build steps on recompile. Perhaps something like this:

var sass = require('node-sass');

sass.watch('./file.scss')
  .on('update', function(file) {  // where file is a Vinyl instance
    // Version image url() assets, for example
    file.contents = cssVersioner(file.contents);
  });

If this sounds reasonable, I could get started on a PR for it. Otherwise, I plan on creating a npm module for this.

baileyparker avatar Aug 07 '15 05:08 baileyparker

I would also like a way to use the watch functionality through an API. This has the big advantage that external build tools are not necessary if you want to have a callback when a file is compiled.

SpaceK33z avatar Aug 07 '15 19:08 SpaceK33z

I think we should expose this. Maybe one day we will be able to tell libsass which files need to be reloaded individually.

saper avatar Aug 07 '15 22:08 saper

Agree, and I'd be willing to add hooks into grunt-sass once this is done.

schmod avatar Aug 11 '15 21:08 schmod

+1 would like this functionality.

monarchwadia avatar Nov 01 '15 20:11 monarchwadia

Any progress on this? It would be really nice to have this as part of Gulp/Grunt process.

niksy avatar Jan 20 '16 09:01 niksy

+1

a good example of implementation is webpack:

var webpack = require("webpack");

// returns a Compiler instance
var compiler = webpack({
    // configuration
});

// regular build
compiler.run(function(err, stats) {
    // ...
});

// build + watch
compiler.watch({ // watch options:
    aggregateTimeout: 300, // wait so long for more changes
    poll: true // use polling instead of native watchers
    // pass a number to set the polling interval
}, function(err, stats) {
    // ...
});

DarkPark avatar Jan 22 '16 10:01 DarkPark

@saper I would like to try to tackle this issue. Do you have any advice on how to do it? I tried to look at the way render works, but it seems that the way it works in the cli and the way it is done in lib/index.js is totally different. I would like to try to avoid duplicating the grapher/gaze configuration.

Thanks.

genintho avatar Jan 29 '16 21:01 genintho

There is a proposal in #1156 to split CLI as the separate module and I think this is a good way to fix our hopelessly broken command line interface (in a incompatibly way I hope).

This way CLI could depend on the watcher (whichever one is about to be chosen) and the dependency calculator (currently https://github.com/xzyfer/sass-graph).

For now we have disabled CLI watcher tests, because they were not working. Maybe trying to expose the API and use that in tests would be a good place to start. See https://github.com/sass/node-sass/pull/1288 https://github.com/sass/node-sass/pull/1261 https://github.com/sass/node-sass/pull/1260 https://github.com/sass/node-sass/pull/1216.

Calling a watcher is actually a line or two in our CLI so it might be not that difficult.

saper avatar Feb 14 '16 20:02 saper

+1 Looking for a way to watch a file and its @imports (which are from multiple directories). The CLI allows watch, but only the API allows multiple @import dirs.

cguinnup avatar Jan 19 '17 19:01 cguinnup

lib/watcher.js is already a progress, but the way it is called from node-sass binary needs to be refactored

saper avatar Oct 18 '19 05:10 saper

FYI https://github.com/sass/sass/issues/2823 now exists to discuss this in a cross-implementation context.

nex3 avatar Feb 11 '20 01:02 nex3

+1

ymatuhin avatar Jul 24 '20 07:07 ymatuhin