如何在我的npm脚本中使用"watch"?

19 javascript node.js npm

我有以下目录结构:

在此输入图像描述

package.json看起来像这样:

{
  "name": "personal_site",
  "version": "1.0.0",
  "description": "My personal website.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "node-sass": "node-sass --output-style compressed --include-path node_modules/bourbon/app/assets/stylesheets/ --include-path node_modules/bourbon-neat/app/assets/stylesheets/ 'src/scss/styles.scss' 'dist/css/bundle.min.css'",
    "html-minifier": "html-minifier --collapse-whitespace --remove-comments --remove-attribute-quotes -o 'dist/index.html' 'src/index.html'",
    "imagemin": "imagemin src/images dist/images",
    "serve": "http-server ./dist"
  },
  "author": "Dean Gibson",
  "license": "ISC",
  "dependencies": {
    "bourbon": "^4.2.6",
    "bourbon-neat": "^1.7.4",
    "normalize-scss": "^4.0.3"
  },
  "devDependencies": {
    "html-minifier": "^1.3.0",
    "http-server": "^0.9.0",
    "node-sass": "^3.4.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

所以,首先,我要运行这些脚本分别例如npm run node-sass或者npm run html-minifier等我会非常想是运行npm serve,这将做到以下几点:

  1. 运行html-minifier
  2. 运行node-sass
  3. 运行运行image-min
  4. 运行http-server
  5. 最后,观察我的src文件夹中的所有内容,并在文件更改时运行相应的脚本node-sass等.

我怎样才能最好地解决这个问题?

Meh*_*hdi 26

您可以使用以下方式查看目录nodemon.

一个解决方案是创建三个监视脚本,每个任务一个:

  • watch:node-sass,
  • watch:html-minifier,和
  • watch:imagemin.

然后有一个中心脚本watch开始三:

{
  "name": "personal_site",
  "version": "1.0.0",
  "description": "My personal website.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "node-sass": "node-sass --output-style compressed --include-path node_modules/bourbon/app/assets/stylesheets/ --include-path node_modules/bourbon-neat/app/assets/stylesheets/ 'src/scss/styles.scss' 'dist/css/bundle.min.css'",
    "html-minifier": "html-minifier --collapse-whitespace --remove-comments --remove-attribute-quotes -o 'dist/index.html' 'src/index.html'",
    "imagemin": "imagemin src/images dist/images",
    "serve": "http-server ./dist",
    "watch:node-sass": "nodemon -e scss -x \"npm run node-sass\"",
    "watch:html-minifier": "nodemon -e html -x \"npm run html-minifier\"",
    "watch:imagemin": "nodemon --watch src/images -x \"npm run imagemin\"",
    "watch": "npm run watch:node-sass & npm run watch:html-minifier & npm run watch:imagemin"
  },
  "author": "Dean Gibson",
  "license": "ISC",
  "dependencies": {
    "bourbon": "^4.2.6",
    "bourbon-neat": "^1.7.4",
    "normalize-scss": "^4.0.3"
  },
  "devDependencies": {
    "html-minifier": "^1.3.0",
    "http-server": "^0.9.0",
    "node-sass": "^3.4.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

阅读:如何使用npm作为构建工具.


rev*_*elt 18

我建议onchange,看看这个样板.

例如,

"watch:css": "onchange 'src/scss/*.scss' -- npm run build:css",
Run Code Online (Sandbox Code Playgroud)

要么

"watch:js": "onchange 'src/js/*.js' -- npm run build:js",
Run Code Online (Sandbox Code Playgroud)

不需要Grunt或Gulp!

  • 使用 ",而不是 ' 至少在 Windows 中: "watch:html": "onchange \"src/*.html\" \"src/templates/headTemplate.html\" -- npm run htmlTempl"。它不适用于单引号。 (2认同)
  • 请记住安装 onchange:npm install onchange (2认同)
  • 注意:onchance 不支持运行多个命令。因此,您可以设置要运行的 BASH 脚本来执行所有服务器终止、重新启动等操作。 (2认同)