Reputation: 1603
I'm using Webpack and Babel for a project. I installed babel-loader
in order to include Babel in my build process. Most of the examples I see online of babel-loader
use the following configuration in the webpack config file:
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
}
My question is, why would we want to exclude node_modules
from being transpiled by Babel? I know that the documentation says that excluding it will speed up the bundling process. However, if node_modules contains dependencies that are needed for my app to work, shouldn't those dependencies also be converted by Babel? Let's say that I want to transpile my app to use ES5 code, and node_modules contains libraries that are using ES6+. It seems like node_modules should also be included in the files that Babel processes.
Upvotes: 0
Views: 1976
Reputation: 73
Packages inside node_modules are likely already built, minified and targeted to ES3, there is no need for Babel to further transpile them.
In a case whereby you import a package and it's in ES6, Webpack automatically builds a dependency graph internally and will detect such package and transpile it to your desired target (ES5) using Babel.
You don't want to let Babel transpile all of the files that reside at node_modules folder (possibly, thousands of files), that's resource-intensive and it'll take a longer time.
Upvotes: 2