bable中配置es6、jsx语法

yarn安装

  1. yarn add @babel/preset-env
  2. yarn add @babel/preset-react


package.json文件

  1. "@babel/preset-env": "^7.9.5",
  2. "@babel/preset-react": "^7.9.4",

.babelrc添加如下配置

  1. {
  2. "presets": ["@babel/preset-env","@babel/preset-react"]
  3. }

也可以将.babelrc配置放入webpack.config.js中不使用.babelrc 可以使用如下配置

  1. {
  2. test:/\.js$/,
  3. exclude: /(node_modules)/,
  4. use: {
  5. loader: 'babel-loader',
  6. options: {
  7. presets: [
  8. '@babel/preset-env',
  9. '@babel/preset-react'
  10. ]
  11. }
  12. }
  13. },

babel中配置可选链

"@babel/preset-env": "^7.8.7"以上版本集成了可选链的配置

可以直接使用可选链配置

  1. const obj = {};
  2. obj?.list?.picker?.a?.a?.a => undefined
  3. obj?.test ?? 'hello' => 'hello'

babel中配置decorators

  1. {
  2. plugins: [
  3. "@babel/plugin-proposal-decorators",
  4. {
  5. "legacy": true
  6. }
  7. ],
  8. }

babel中配置箭头函数

  1. class App extends React.Component {
  2. state = {
  3. name:1
  4. }
  5. clickBtn = () => {
  6. console.log('hhh')
  7. }
  8. }

可以在babel添加配置

  1. {
  2. plugins: ["@babel/plugin-proposal-class-properties"]
  3. }

babel配置typescript

yarn add @babel/preset-typescript

  1. "presets": ["@babel/preset-env","@babel/preset-react","@babel/preset-typescript"],

babel配置去除console.log
  1. ["babel-plugin-transform-remove-console",{
  2. "exclude":["error","warn"]
  3. }]