Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.babelrc configuration placed in package.JSON

Currently exploring webpack different tools associated with it. Now I am using Babel for transpiling ES6 code into ES5 code. I came accross the need for a .babelrc file which holds the configurations for Babel. However, on the website of Babel I also saw that you could also place these configurations into the package.json file. Like this:

Package.json File:

{
  "name": "webpack-tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.16.2",
    "webpack-cli": "^3.1.0"
  },
  "babel": {
    "presets": [
        "env"
    ]
  }

}

Now when I run npm run dev Babel also works and the code gets transpiled succesfully.

How does Babel know to access the package.json file? Does it first look for an .babelrc file and then if this is not present does it automatically look for its configurations in the package.json? How does Webpack interact with both Babel and the package.json file to produce this result?

like image 251
Willem van der Veen Avatar asked Jul 24 '18 10:07

Willem van der Veen


People also ask

Where do I put .babelrc files?

babelrc file is your local configuration for your code in your project. Generally you would put it in the root of your application repo. It will affect all files that Babel processes that are in the same directory or in sibling directories of the . babelrc .

Is Babelrc the same as Babel config?

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.

Is Babelrc required?

config. json . .babelrc. json files are not required for subfolder-specific configuration like they were in Babel 6, so often they are not needed in Babel 7, in favor of babel.


1 Answers

For anyone who is interested it was on the official website:

Babel will look for a .babelrc in the current directory of the file being transpiled. If one does not exist, it will travel up the directory tree until it finds either a .babelrc, or a package.json with a "babel": {} hash within.

like image 195
Willem van der Veen Avatar answered Nov 05 '22 12:11

Willem van der Veen