Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using node-sass watch option with npm run-script

So I'm running tasks in npm package scripts but I want to pass the watch option in npm start.

This works:

"scripts": {   "scss": "node-sass src/style.scss dist/style.css -w" } 

This doesn't compile, watch, or throw any error:

"scripts": {   "scss": "node-sass src/style.scss dist/style.css",   "start": "parallelshell \"npm run scss -- -w\"" } 

Doesn't work without parallelshell either or without shorthand.

I assume the problem is the run-script is passing the extra argument in quotes, so the command comes out like:

node-sass src/style.scss dist/style.css "-w" 

I'd like this to work without adding any dependencies. What am I missing?

Btw, I'm in Windows 10 with command prompt/git bash.

like image 893
Ryan Metin Avatar asked Jan 14 '16 18:01

Ryan Metin


People also ask

What does -- watch do in npm?

npm run watch does the same, but then it stays active and "watches" for updates to your . vue and . js files. If it detects a change, it'll re-build the browser-friendly file so you can just refresh the page.

What is node Sass npm?

Node-sass is a library that provides binding for Node. js to libsass, the C version of the popular stylesheet preprocessor, Sass. It allows you to natively compile . scss files to css at incredible speed and automatically via a connect middleware. Find it on npm: https://npmjs.org/package/node-sass.

How do I view a Sass file?

Go to you terminal and get to you folder then wrote: sass --watch . I hope this can help you. Show activity on this post.


2 Answers

This is my setup for css building

"scripts": {   "css": "node-sass src/style.scss -o dist",   "css:watch": "npm run css && node-sass src/style.scss -wo dist" }, "devDependencies": {   "node-sass": "^3.4.2" } 

The -o flag sets the directory to output the css. I have a non-watching version "css" because the watching version "css:watch" ~doesn't build as soon as it's run~, it only runs on change, so I call

npm run css  

before calling

node-sass src/style.scss -wo dist 

If you only want it to run on change, and not when first run, just use

"css:watch": "node-sass src/style.scss -wo dist" 
like image 109
Ryan White Avatar answered Oct 01 '22 01:10

Ryan White


Building on the previous answers, another option is to leverage NPM's custom script arguments to remain DRY by not repeating the build script arguments in the watch script:

"scripts": {   "build:sass": "node-sass -r --output-style compressed src/style.scss -o dist",   "watch:sass": "npm run build:sass && npm run build:sass -- -w" } 

In the above example, the watch:sass script works as follows:

  1. Run the build:sass script. This will compile your CSS.
  2. Run the build:sass script again, but this time include the -w flag. This will compile your CSS when one of your SASS file changes.

Notice the -- option used at the end of the watch:sass script. This is used to pass custom arguments when executing a script. From the docs:

As of [email protected], you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script.

like image 27
jhildenbiddle Avatar answered Oct 01 '22 03:10

jhildenbiddle