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?
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 .
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.
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.
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.
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