简体   繁体   English

如何避免在 react-app 的生产版本中使用 polyfill

[英]How to avoid polyfills on production build of react-app

My aim would be to keep ES6 syntax (or latest one) in my react-app built .我的目标是在我的 react-app built 中保留ES6语法(或最新的语法)

I've already managed to avoid polyfills on my dev-environment by removing some babel dependencies (such as @babel/preset-env and more).我已经设法通过删除一些babel依赖项(例如@babel/preset-env等)来避免在我的开发环境中使用 polyfill。 I've noticed that production built uses var keyword and other ES5 syntax.我注意到构建的产品使用var关键字和其他 ES5 语法。 I'm assuming that babel (or webpack ?) is polyfilling my react-app.我假设babel (或webpack ?)正在填充我的反应应用程序。 I've tried removing babel completely, but it is necessary for React : it is apparently used to convert jsx to vanilla js .我已经尝试完全删除babel ,但对于React是必需的:它显然用于将jsx转换为vanilla js Could I keep only required polyfill and ommit all others?我可以只保留所需的 polyfill 并省略所有其他的吗?

Here is my package.json :这是我的package.json

  "main": "index.js",
  "scripts": {
    "start": "webpack serve --mode=development --open",
    "build": "webpack --mode=production"
  },
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "devDependencies": {
    "@babel/preset-react": "^7.16.5",
    "babel-loader": "^8.2.3",
    "css-loader": "^6.5.1",
    "html-webpack-plugin": "^5.5.0",
    "style-loader": "^3.3.1",
    "webpack": "^5.65.0",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.7.2"
  },
  "babel": {
    "presets": [ "@babel/preset-react" ]
  }

and here is my webpack.config.js :这是我的webpack.config.js

const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "[name].js"
  },
  resolve: {
    modules: [ path.join(__dirname, "src"), "node_modules" ],
    alias: {
      react: path.join(__dirname, "node_modules", "react")
    }
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      },
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({ template: "./src/index.html" })
  ],
};

I am not using create-react-app but my own boilerplate and configuration.我没有使用create-react-app而是我自己的样板和配置。

My index.js , app.js , index.html , styles.css are into the ./src folder.我的index.jsapp.jsindex.htmlstyles.css都在./src文件夹中。

Please note that I have looked other questions but none satisfy my needs.请注意,我查看了其他问题,但没有一个能满足我的需求。

Thanks谢谢

You don't need to disable babel.你不需要禁用 babel。 If your only concerns are polyfills there is useBuiltIns option that you can configure for babel so it doesn't use polyfills at all.如果您唯一关心的是 polyfill,那么您可以为 babel 配置useBuiltIns选项,这样它就根本不使用 polyfill。

If you want to use es6 syntax then you can use targets option.如果你想使用 es6 语法,那么你可以使用targets选项。 For example setting targets: 'defaults, not ie 11, not ie_mob 11' doesn't use var instead of const anymore.例如设置targets: 'defaults, not ie 11, not ie_mob 11'不再使用var而不是const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM