babel,带有nodemon脚本的webpack?

我想知道是否有一种方法来configurationwebpack babel和nodemon。 我几乎在网上search,但没有发现任何有用的东西,或者可能是我,因为我是非常新的构build工具。 我在我的package.json有这个脚本:

 "start": "nodemon app.js --exec babel-node" 

它会传输我的代码,并在发生更改时重新启动服务器。 我想知道是否有这样的手表function的webpack的configuration。 我可以做到这一点与webpack(运行服务器,并观察变化,并重新启动巴贝尔transpile)?

你不必使用nodemon,你可以使用webpack的手表function 。

这是一个脚本示例,我们称之为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将重新编译并重新启动服务器。

至于babel部分,我相信babel loader有你覆盖。 我在我的webpack.config.js (webpack 2)中使用这个:

 module: { ... rules: [ { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: {presets: ['es2015']} } ] } 

但是我不使用nodemon,所以只有一半的答案感到抱歉。 我在开发中使用webpack-dev-server。 和pm2在舞台/制作,我正在使用它的分期手表,所以我不必手动重新启动的东西,它比webpack更容易configurationdito:

 // pm2's ecosystem.json (just to be thorough): "watch" : "./", "ignore_watch" : "node_modules", 

没有生产的手表,不,不,不,不,我,没有敏感 – 可以出错的事情越less越好。