最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .babelrc configuration placed in package.JSON - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Jul 24, 2018 at 10:58 Willem van der VeenWillem van der Veen 36.6k18 gold badges205 silver badges176 bronze badges 2
  • 2 babeljs.io/docs/en/babelrc#lookup-behavior – Chris Satchell Commented Jul 24, 2018 at 11:04
  • Okay thanks that is clear!! – Willem van der Veen Commented Jul 24, 2018 at 11:07
Add a comment  | 

1 Answer 1

Reset to default 20

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.

发布评论

评论列表(0)

  1. 暂无评论