ノードモンを使用する必要はありません。webpack's watch featureを使用できます。
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()));
});
あなたは、あなたのサーバコードに変更を加える場合は、WebPACKのは、再コンパイルします
node backend-dev.js
を使用してコマンドライン上でこのスクリプトを起動することができます。ここでは
はのはbackend-dev.js
それを呼びましょう、スクリプトの例ですサーバーを再始動してください。
通常、サーバー部分にはWebpackは使用されませんが、ブラウザJS、CSS、イメージなどのクライアント側のアセットをバンドルすることを目標にしています。サーバーにWebpackを使用する特別な理由はありますか? – damd
サーバーのwebpackを使用する理由は完全にあります。たとえば、Reactコンポーネントのサーバー側コンパイルを行う場合などです。 –
@PatrickHund:あなたは正しいですが、OPはツールを作成するために新しくなったものなので、彼らはそれを使用している理由を知っていることを確認してください:) – damd