Isaac
Isaac

Reputation: 12874

Why would we exclude node_modules when using babel-loader?

Most of the question asking on the site is to how to exclude node_modules but instead, I'm wondering why would we want to exclude node_modules?

module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: ['@babel/preset-env']
        }
      }
    ]
  }
};

Can anyone explain to me to the reason behind excluding node_modules?

Upvotes: 35

Views: 22812

Answers (1)

Austin Mehmet
Austin Mehmet

Reputation: 498

In short, transpiling is an expensive process and many projects have thousands (if not hundreds of thousands) of lines of code imported in that babel would need to run over. Your node_modules should already be runnable without transpiling as said already and there are simple ways to exclude your node_modules but transpile any code that needs it. See https://github.com/babel/babel-loader/issues/171.

I have seen a lot of arguments back and forth about whether or not it should be the developer consuming the applications job of transpiling the library or the library developer's responsibility. Most of the time transpiling is done for browser support and the library creator has no knowledge of what browsers you need to support so they end up either transpiling or not transpiling leaving it in your hands. If they transpile to ES5 you are golden, if not it is usually a simple enough task to figure out which libraries are causing issues and transpile them yourself

Upvotes: 22

Related Questions