简体   繁体   English

babel,使用nodemon脚本的webpack?

[英]babel, webpack with nodemon script?

I was wondering if there is a way to configure webpack with babel and nodemon. 我想知道是否可以使用babel和nodemon配置webpack。 II pretty much searched the web but found nothing helpful or it may be me because i am pretty much new to build tools. II几乎在网上搜索过,但没有发现任何帮助,或者可能是我,因为我对构建工具非常陌生。 I have this script in my package.json : 我的package.json有这个脚本:

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

It transpile my code and also restart the server when there is changes. 它转换我的代码,并在发生更改时重新启动服务器。 I was wondering if there is such configuration for webpack with the watch functionality. 我想知道是否有带有监视功能的webpack配置。 Can i do it with webpack (run the server and watch for changes and restart along with the babel transpile) ? 我可以使用webpack(运行服务器并观察更改并与babel传输一起重新启动)吗?

You don't have to use nodemon, you can use webpack's watch feature . 您不必使用nodemon,可以使用webpack的watch功能

Here's an example script, let's call it backend-dev.js : 这是一个示例脚本,我们称其为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()));
});

You can start this script on the command line with 您可以使用以下命令在命令行中启动此脚本:

node backend-dev.js

When you make changes in your server code, webpack will recompile and restart your server. 当您更改服务器代码时,webpack将重新编译并重新启动服务器。

As for the babel part, I believe babel loader have you covered. 至于通天塔部分,我相信通天塔装载机已经覆盖了您。 I use this in my webpack.config.js (webpack 2): 我在webpack.config.js (webpack 2)中使用了它:

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

But I don't use nodemon, so sorry for just half an answer. 但是我不使用nodemon,因此仅对半个回答感到抱歉。 I do use webpack-dev-server in development. 我确实在开发中使用webpack-dev-server。 and pm2 in staging/production and I'm using it's watch while staging so I don't have to restart things manually, and it's much easier to configure than webpacks dito: 和pm2在暂存/生产中,并且我在暂存时正在使用它,因此我不必手动重新启动,并且比webpacks dito容易配置:

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

No watch in production though, no no, not me, no touchy - The less things that can go wrong the better. 但是,没有生产中的手表,没有,不是我,没有敏感-越少出错的问题越好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM