我想知道是否有一种方法可以使用 babel 和 nodemon 配置 webpack。我几乎在网上搜索过,但没有发现任何有用的东西,或者可能是我,因为我对构建工具还很陌生。
我的 package.json
中有这个脚本:
"start": "nodemon app.js --exec babel-node"
它会转译我的代码,并在发生更改时重新启动服务器。我想知道带有 watch 功能的 webpack 是否有这样的配置。我可以使用 webpack 来做到这一点(运行服务器并观察更改并与 babel transpile 一起重新启动)吗?
最佳答案
你不必使用nodemon,你可以使用webpack's watch feature .
这是一个示例脚本,我们将其命名为 backend-dev.js
:
const path = require('path');
const webpack = require('webpack');
const spawn = require('child_process').spawn;
const compiler = webpack({
// add your webpack configuration here
});
const watchConfig = {
// compiler watch configuration
// see https://webpack.js.org/configuration/watch/
aggregateTimeout: 300,
poll: 1000
};
let serverControl;
compiler.watch(watchConfig, (err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
info.errors.forEach(message => console.log(message));
return;
}
if (stats.hasWarnings()) {
info.warnings.forEach(message => console.log(message));
}
if (serverControl) {
serverControl.kill();
}
// change app.js to the relative path to the bundle created by webpack, if necessary
serverControl = spawn('node', [path.resolve(__dirname, 'app.js')]);
serverControl.stdout.on('data', data => console.log(data.toString()));
serverControl.stderr.on('data', data => console.error(data.toString()));
});
您可以使用命令行启动此脚本
node backend-dev.js
当您更改服务器代码时,webpack 将重新编译并重新启动您的服务器。
关于javascript - babel,带有nodemon脚本的webpack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500303/