Hi please help me understand the differences between setting babel config inside .babelrc vs webpack loader options, vs inserting it in package.json.
For example, Would it make any difference if I put the presets in the webpack babel-loader options vs package.json or a separate .babelrc config file?
In webpack config:
{
test: /\.(js|jsx|mjs)$/,
loader: require.resolve('babel-loader'),
options: {
"presets": [
"react-app"
]
},
},
In package json:
"babel": {
"presets": [
"react-app"
]
},
Babel can be classified as a tool in the "JavaScript Compilers" category, while Webpack is grouped under "JS Build Tools / JS Task Runners".
babel-loader exposes a loader-builder utility that allows users to add custom handling of Babel's configuration for each file that it processes.
Babel has two parallel config file formats which can be used together, or independently. . babelrc would be useful if you want to run certain transformations / plugins on a subset of files /directories. Maybe you have 3rd party libraries that you don't want to be transformed/changed by babel.
Webpack is a modular build tool that has two sets of functionality — Loaders and Plugins. Loaders transform the source code of a module. For example, style-loader adds CSS to DOM using style tags. sass-loader compiles SASS files to CSS. babel-loader transpiles JS code given the presets.
Webpack config :
config the babel-loader completely in webpack.conf.js (no .babelrc).
Webpack config + .babelrc :
Enable the babel-loader in webpack.conf.js, let the options object be empty. Configure the options in a .babelrc. Webpack will use the babel-loader with the options given in .babelrc.
you can remove the webpack presets options if you have a .babelrc, because babel-loader uses babel, which obviously respects the .babelrc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With