4

我正在使用 npm 脚本(请参阅下面的 npm 文件)来监视文件的更改,watchify然后编译并在编译后通知。然而,发生的情况是 watchify 任务运行(第一次运行时需要 40 秒),然后notify:js立即调用该任务。我希望notify:js在 watchify 完成编译后调用。

{
  "dependencies": {
    "react": "^0.14.3",
    "react-dom": "^0.14.3"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "babelify": "^7.2.0",
    "browserify": "^12.0.1",
    "exorcist": "^0.4.0",
    "osx-notifier": "^0.2.2",
    "watchify": "^3.6.1"
  },
  "scripts": {
    "watch": "watchify src/app.js -o build/bundle.js -t [ babelify --presets [ es2015 react ] ] -dv | npm run notify:js",
    "compile:js": "browserify -e src/app.js -d -o build/bundle.js -t [ babelify --presets [ es2015 react ] ] -v",
    "notify:js": "osx-notifier --title 'JavaScript Compiled' --message 'JavaScript compiled to build/bundle.js' --type pass"
  }
}

我尝试了以下命令变体。

watchify src/app.js -o build/bundle.js -t [ babelify --presets [ es2015 react ] ] \
-dv & npm run notify:js

上述命令与操作符的作用相同|

watchify src/app.js -o build/bundle.js -t [ babelify --presets [ es2015 react ] ] \
-dv && npm run notify:js

上述命令与操作符的作用相同&

对于我想要实现的目标,我在watchify 文档中看不到任何选项。我在这里遗漏了什么,或者我试图这样做的方式是不可能的?

4

1 回答 1

3

我使用nodemon和browserify -incremental解决了这个问题。

这会将 JavaScript 编译成一个bundle.js文件。

browserifyinc -e resources/assets/js/app.js -o public/assets/js/bundle.js --cachefile .browserify-cache.json && npm run notify:js

这是我的package.json文件。

{
  "dependencies": {
    "react": "^0.14.3",
    "react-dom": "^0.14.3"
  },
  "devDependencies": {
    "babelify": "^6.1.3",
    "browserify": "^12.0.1",
    "browserify-incremental": "^3.0.1",
    "nodemon": "^1.3.8"
  },
  "browserify": {
    "transform": [
      "babelify"
    ]
  },
  "scripts": {
    "build": "npm run compile:js && npm run compile:sass",
    "watch": "npm run watch:js & npm run watch:sass",
    "notify:js": "osascript -e 'display notification \"JavaScript compiled to public/assets/js/bundle.js\" with title \"JavaScript Compiled\"'",
    "notify:sass": "osascript -e 'display notification \"CSS compiled to public/assets/js/app.js\" with title \"CSS Compiled\"'",
    "compile:js": "browserifyinc -e resources/assets/js/app.js -o public/assets/js/bundle.js --cachefile .browserify-cache.json && npm run notify:js",
    "compile:sass": "sass resources/assets/sass/app.scss:public/assets/css/app.css && npm run notify:sass",
    "watch:js": "nodemon --watch 'resources/assets/js' --exec 'npm run compile:js'",
    "watch:sass": "nodemon --watch 'resources/assets/sass' -e scss --exec 'npm run compile:sass'"
  }
}
于 2015-12-04T09:21:34.720 回答