Webpack --watch 并启动 nodemon? [英] Webpack --watch and launching nodemon?

查看:78
本文介绍了Webpack --watch 并启动 nodemon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢@@出色的回答McMath 我现在有 webpack 编译我的客户端和我的服务器.我现在正在尝试使 webpack --watch 变得有用.理想情况下,我希望它在包更改时为我的服务器进程生成类似 nodemon 的东西,并在我的客户端更改时生成一些浏览器同步.

Thanks to an excellent answer by @McMath I now have webpack compiling both my client and my server. I'm now on to trying to make webpack --watch be useful. Ideally I'd like to have it spawn something like nodemon for my server process when that bundle changes, and some flavor of browsersync for when my client changes.

我意识到它是一个打包器/加载器,而不是真正的任务运行器,但是有什么方法可以做到这一点吗?缺少谷歌搜索结果似乎表明我正在尝试新的东西,但这一定已经完成了..

I realize it's a bundler/loader and not really a task runner, but is there some way to accomplish this? A lack of google results seems to indicate I'm trying something new, but this must have been done already..

我总是可以将 webpack 包放到另一个目录并使用 gulp 来观看它/复制它/浏览器同步它,但这似乎是一个黑客......有没有更好的方法?

I can always have webpack package to another directory and use gulp to watch it/copy it/browsersync-ify it, but that seems like a hack.. Is there a better way?

推荐答案

遇到同样的问题,找到了下一个解决方案 - webpack-shell-plugin.它

Faced the same problem and found the next solution - webpack-shell-plugin. It

允许你在 webpack 构建之前或之后运行任何 shell 命令

所以,这就是我在 package.json 中的脚本:

So, thats my scripts in package.json:

"scripts": {
      "clean": "rimraf build",
      "prestart": "npm run clean",
      "start": "webpack --config webpack.client.config.js",
      "poststart": "webpack --watch --config webpack.server.config.js",
}

如果我运行开始"脚本,它会启动下一个脚本序列:clean ->开始 ->启动后.还有'webpack.server.config.js'的一部分:

If I run 'start' script it launches next script sequence: clean -> start -> poststart. And there is part of 'webpack.server.config.js':

var WebpackShellPlugin = require('webpack-shell-plugin');

...
if (process.env.NODE_ENV !== 'production') {
    config.plugins.push(new WebpackShellPlugin({onBuildEnd: ['nodemon build/server.js --watch build']}));
}
...

onBuildEnd"事件仅在第一次构建后触发一次,重建不会触发onBuildEnd",因此 nodemon 按预期工作

"onBuildEnd" event fires only once after first build, rebuilds are not trigger "onBuildEnd", so nodemon works as intended

这篇关于Webpack --watch 并启动 nodemon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆