【问题标题】:How to tell Babel to target ES2021?如何告诉 Babel 以 ES2021 为目标?
【发布时间】:2022-11-04 00:34:10
【问题描述】:

我有以下 babel.config。js(不是 .rc)文件:

module.exports = function (api) {
    api.cache(true);
       return {
        "presets": [
            "@babel/preset-react",
            [
                "@babel/preset-env",
                {
                    "useBuiltIns": "entry",
                    "corejs": "3.8",
                    targets: {
                        chrome: 97      // January 2022
                    },
                    "modules": false
                }
            ]
        ],
        "plugins": [
            ["@babel/plugin-transform-react-jsx", {
                "throwIfNamespace": false, // defaults to true
                "runtime": "automatic" // defaults to classic
            }],
            "@babel/plugin-proposal-class-properties",
            "@babel/plugin-transform-object-assign",
            "@babel/plugin-proposal-object-rest-spread",
            ["module:fast-async", { "spec": true }]
        ]
    }

我发现即使我的目标是现代浏览器(Chrome v97,2022 年 1 月发布),Babel 仍然将 async/await 转换为基于 Promise 的代码。

我想以 ES2021 为目标,因此 Babel 不需要转译 async/await。

另外我想使用 ??和 ?。运营商。事实上,任何 ES2021 都支持。

我知道有插件可以模拟大多数东西,但我的目标浏览器已经支持 ES2021。我只是不知道如何告诉 Babel“如果浏览器已经支持它就不要编译。”。

我该怎么做?

这是我的 package.json:

{
  "name": "scts-expenses",
  "version": "0.1.0",
  "description": "",
  "scripts": {
    "build": "webpack --config tools/webpack/config/build",
    "ci": "webpack --config tools/webpack/config/integration",
    "dev": "webpack --config tools/webpack/config/dev",
    "lint": "node_modules/.bin/eslint src -c .eslintrc --ext js",
    "start": "npm run dev",
    "test": "node_modules/.bin/jest",
    "watch-dev": "webpack --config tools/webpack/config/dev --watch"
  },
  "author": "scott",
  "license": "MIT",
  "devDependencies": {
    "@babel/cli": "^7.18.10",
    "@babel/core": "^7.12.10",
    "@babel/node": "^7.18.10",
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-object-assign": "^7.18.6",
    "@babel/plugin-transform-react-jsx": "^7.18.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-react": "^7.18.6",
     "autoprefixer": "^9.8.6",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.2.5",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "bluebird": "^3.7.2",
    "browser-sync": "^2.26.13",
    "browser-sync-webpack-plugin": "^2.3.0",
    "clean-webpack-plugin": "^3.0.0",
    "copy-webpack-plugin": "^5.1.2",
    "cors": "^2.8.5",
    "cross-env": "^5.2.0",
    "css-loader": "^3.6.0",
    "eslint": "^6.8.0",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-jest": "^23.20.0",
    "eval": "^0.1.4",
    "fast-async": "^6.3.8",
    "filemanager-webpack-plugin": "^2.0.5",
    "imagemin-webpack-plugin": "^2.4.2",
    "jest": "^24.9.0",
    "jest-axe": "^3.5.0",
    "mini-css-extract-plugin": "^0.8.2",
    "node-sass": "^4.14.1",
    "postcss": "^7.0.35",
    "postcss-css-variables": "^0.13.0",
    "postcss-custom-properties": "^9.2.0",
    "postcss-loader": "^3.0.0",
    "preact": "^10.10.6",
    "preact-render-to-json": "^3.6.6",
    "preact-render-to-string": "^5.2.2",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.3.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-hot-middleware": "^2.25.0",
    "webpack-merge": "^4.2.2"
  },
  "dependencies": {
    "@microsoft/applicationinsights-web": "^2.5.10",
    "abortcontroller-polyfill": "^1.7.3",
    "core-js": "^3.25.0",
    "custom-event-polyfill": "^1.0.7",
    "element-closest-polyfill": "^1.0.2",
    "es6-object-assign": "^1.1.0",
    "form-request-submit-polyfill": "^2.0.0",
    "picturefill": "^3.0.3",
    "promise-polyfill": "^8.2.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "storm-modal": "^1.2.1",
    "storm-tabs": "^1.3.3",
    "unfetch": "^4.2.0",
    "url-search-params-polyfill": "^8.1.0"
  }
}

【问题讨论】:

  • 你有没有找到解决这个问题的方法?我正在努力解决类似的问题。

标签: javascript reactjs babeljs


【解决方案1】:

好的,我发现了如何使用最新的(或者,我需要的现代的)功能。

关键是使用 babel/preset-env拒绝 Internet Explorer 作为浏览器目标.

我通过在https://babeljs.io/repl/ 上修补发现了这一点。当我找到生成现代代码的设置时(例如,没有将 await/async 转换为 Promise),我将它们复制到我的 babel 配置中。

这是我的 babel 配置:

module.exports = function (api) {
api.cache(true);

return {
    "presets": [
        "@babel/preset-react",
    [
      "@babel/preset-env",
      Object.assign({}, process.env.NODE_ENV === "test"
        ? {
              "loose": true,
                "targets": {
                    "node": 8,
                    "browsers": [">0.25%","not IE"]
                }
          }
          : {
              "useBuiltIns": "entry", 
              "corejs": "3.8", 
                "targets": {
                    "browsers": "defaults, not ie 11, not ie_mob 11"
                },
              "modules": false
          }
        )
    ]
  ],
  "plugins": [
      ["@babel/plugin-transform-react-jsx", {
          "throwIfNamespace": false, // defaults to true
          "runtime": "automatic" // defaults to classic
      }]
    ]
}

};

这是我的 package.json。注意:我相信不再需要引用的部分/全部 polyfill(例如 es6-object-assign 和 promise-polyfill)。我将在我的项目中删除它们 - 除非必要,否则请不要将它们包含在您的项目中。

{
  "name": "NeverYouMind ;)",
  "version": "0.1.0",
  "description": "",
  "scripts": {
    "ci": "webpack --config tools/webpack/config/integration",
    "dev": "webpack --config tools/webpack/config/dev",
    "lint": "node_modules/.bin/eslint src -c .eslintrc --ext js",
    "start": "npm run dev",
    "test": "node_modules/.bin/jest",
    "watch-dev": "webpack --config tools/webpack/config/dev --watch"
  },
  "author": "Scotty T",
  "license": "MIT",
  "devDependencies": {
    "@babel/cli": "^7.18.10",
    "@babel/core": "^7.12.10",
    "@babel/node": "^7.18.10",
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-transform-object-assign": "^7.18.6",
    "@babel/plugin-transform-react-jsx": "^7.18.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-react": "^7.18.6",
    "autoprefixer": "^9.8.6",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.2.5",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "bluebird": "^3.7.2",
    "browser-sync": "^2.26.13",
    "browser-sync-webpack-plugin": "^2.3.0",
    "clean-webpack-plugin": "^3.0.0",
    "copy-webpack-plugin": "^5.1.2",
    "cors": "^2.8.5",
    "cross-env": "^5.2.0",
    "css-loader": "^3.6.0",
    "eslint": "^6.8.0",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-jest": "^23.20.0",
    "eval": "^0.1.4",
    "fast-async": "^6.3.8",
    "filemanager-webpack-plugin": "^2.0.5",
    "imagemin-webpack-plugin": "^2.4.2",
    "jest": "^24.9.0",
    "jest-axe": "^3.5.0",
    "mini-css-extract-plugin": "^0.8.2",
    "node-sass": "^4.14.1",
    "postcss": "^7.0.35",
    "postcss-css-variables": "^0.13.0",
    "postcss-custom-properties": "^9.2.0",
    "postcss-loader": "^3.0.0",
    "preact": "^10.10.6",
    "preact-render-to-json": "^3.6.6",
    "preact-render-to-string": "^5.2.2",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.3.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-hot-middleware": "^2.25.0",
    "webpack-merge": "^4.2.2"
  },
  "dependencies": {
    "@microsoft/applicationinsights-web": "^2.5.10",
    "abortcontroller-polyfill": "^1.7.3",
    "core-js": "^3.25.0",
    "custom-event-polyfill": "^1.0.7",
    "element-closest-polyfill": "^1.0.2",
    "es6-object-assign": "^1.1.0",
    "form-request-submit-polyfill": "^2.0.0",
    "picturefill": "^3.0.3",
    "promise-polyfill": "^8.2.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "unfetch": "^4.2.0",
    "url-search-params-polyfill": "^8.1.0"
  }
}

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2020-01-14
    • 2011-06-22
    相关资源
    最近更新 更多