Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference when configuring webpack babel-loader vs configuring it within package.json?

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"
    ]
  },
like image 986
Davis Davis Avatar asked Jan 27 '18 13:01

Davis Davis


People also ask

What is the difference between Babel and Webpack?

Babel can be classified as a tool in the "JavaScript Compilers" category, while Webpack is grouped under "JS Build Tools / JS Task Runners".

Why do we need babel-loader?

babel-loader exposes a loader-builder utility that allows users to add custom handling of Babel's configuration for each file that it processes.

What is the difference between Babelrc and Babel config JS?

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.

What is Webpack babel-loader?

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.


1 Answers

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.

like image 75
Jayavel Avatar answered Sep 22 '22 16:09

Jayavel