Skip to content

Fix node-sass watcher #71

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

Merged
merged 3 commits into from
Apr 2, 2018
Merged

Fix node-sass watcher #71

merged 3 commits into from
Apr 2, 2018

Conversation

Fatme
Copy link
Contributor

@Fatme Fatme commented Mar 20, 2018

Currently node-sass watcher is used to compile sass files when some changes occurs. The problem is when file A is imported in file B. If some change occurs in file A, file A will be successfully compiled by node-sass but file B will not be updated. So when livesync command is executed, changes are not reflected in the app's UI.

This PR removes --watch option from node-sass compiler and introduces chokidar watcher.

Also fixes this #70

Currently node-sass watcher is used to compile sass files when some changes occurs. The problem is when file A is imported in file B. If some change occurs in file A,  file A will be successfully compiled by `node-sass` but file B will not be updated. So when `livesync command` is executed, changes are not reflected in the app's UI.

This PR removes `--watch` option from `node-sass` compiler and introduces chokidar watcher.

Also fixes this #70
options = options || {};
var sassPath = getSassPath();
var data = {
sassPath: sassPath,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use short syntax for all properties:

var data = {
    sassPath,
    projectDir,
...
}

cwd: appDir,
awaitWriteFinish: {
pollInterval: 100,
stabilityThreshold: 500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it way too long, maybe 300 ?

logger.info('Found peer node-sass');
} catch (err) { }
} else {
throw Error('node-sass installation local to project was not found. Install by executing `npm install node-sass`.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error

var currentSassProcess = spawn(process.execPath, nodeArgs, { env: env });

var isResolved = false;
var watchResolveTimeout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable is not used

if (code === 0) {
resolve();
} else {
reject(Error('SASS compiler failed with exit code ' + code));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Error

});

// SASS does not recompile on watch, so directly resolve.
if (options.watch && !isResolved) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the check for options.watch here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact the whole if and the call to resolve is not needed here.


watcher = choki.watch(['**/*.scss', '**/*.sass'], watcherOptions)
.on('all', (event, filePath) => {
spawnNodeSass(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you apply several changes and save them one-by-one, you'll receive multiple events and several processes will be spawned. This is a problem, so I suggest chaining the calls to spawnNodeSass:

var watchPromisesChain = Promise.resolve();
watcher = choki.watch(['**/*.scss', '**/*.sass'], watcherOptions)
	.on('all', (event, filePath) => {
		watchPromiseChain = watchPromiseChain.then( () => spawnNodeSass(data) );
	});

Copy link
Contributor

@rosen-vladimirov rosen-vladimirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

if (fs.existsSync(sassPath)) {
try {
logger.info('Found peer node-sass');
} catch (err) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will the logger.info fail ?

@Fatme Fatme merged commit 52b2bb2 into master Apr 2, 2018
@Fatme Fatme deleted the fatme/choki branch April 2, 2018 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants