Using node-sass watch option with npm run-script

前端 未结 6 1256
难免孤独
难免孤独 2020-12-05 10:46

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

This works:

\"scripts\": {
  \"scss\": \"         


        
相关标签:
6条回答
  • 2020-12-05 11:00

    Btw, here's my change:

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

    Edit: Change was asynchronous script runs, for the initial compile and then with the watch flag.

    0 讨论(0)
  • 2020-12-05 11:09

    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"
    
    0 讨论(0)
  • 2020-12-05 11:10

    if you use vcode , I prefer to use extention watching sass live-server it's very useful way

    0 讨论(0)
  • 2020-12-05 11:10

    Simplest in my opinion, for a smaller quick project, is just open a new bash window and paste:

    node-sass src/ -wo dist
    
    0 讨论(0)
  • 2020-12-05 11:16

    If you want to watch and compile changes automatically when you save that changes in your .scss file , you can use this solution:

    "scripts": {
      "watch:sass": "node-sass -w path/to/your/scss --output-style compressed", 
      // example  :  node-sass -w public/styles/scss/bootstrap.scss public/styles/style.css --output-style compressed
    }
    
    0 讨论(0)
  • 2020-12-05 11:23

    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 npm@2.0.0, 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.

    0 讨论(0)
提交回复
热议问题