javascript - React App 将 JS 转换为 ES5 以针对 IE 11

标签 javascript reactjs babeljs babel-polyfill

我目前有一个 React 应用程序,它没有在 IE 中运行/渲染,我认为是由于 ES5/ES6 问题。

我得到的错误是这样的:语法错误。

在下面突出显示的行上。

    if(process.env.NODE_ENV == 'production') {
=>       module.exports = require('./cjs/react.production.min.js');
    } else {
      module.exports = require('./cjs/react.development.js');

编译前的index.js顶部:

import 'babel-polyfill'; 
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

package.json:

{
  "name": "reactapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/polyfill": "^7.8.3",
    "@material-ui/core": "^3.9.3",
    "@material-ui/icons": "^3.0.2",
    "@sentry/browser": "^5.10.2",
    "adblockdetect": "0.0.2",
    "animatewithsass": "^3.2.1",
    "axios": "^0.18.1",
    "babel-polyfill": "^6.26.0",
    "core-js": "^3.6.5",
    "date-fns": "^2.10.0",
    "detectrtc": "^1.4.0",
    "easy-peasy": "^3.2.0",
    "formik": "^2.0.3",
    "google-map-react": "^1.1.5",
    "js-cookie": "^2.2.1",
    "lodash": "^4.17.15",
    "material-design-icons": "^3.0.1",
    "material-icons": "^0.3.1",
    "node-sass": "^4.13.0",
    "platform": "^1.3.5",
    "query-string": "^6.10.1",
    "react": "^16.11.0",
    "react-albus": "^2.0.0",
    "react-app-polyfill": "^1.0.6",
    "react-color": "^2.18.0",
    "react-confirm-alert": "^2.4.1",
    "react-dom": "^16.11.0",
    "react-dropzone": "^11.1.0",
    "react-loader-spinner": "^3.1.5",
    "react-media-recorder": "^1.2.0",
    "react-paginate": "^6.3.2",
    "react-router-dom": "^5.1.2",
    "react-rtc-real": "^1.11.22",
    "react-scripts": "2.1.8",
    "react-select": "^3.1.0",
    "react-simple-line-icons": "^1.0.8",
    "react-spring": "^8.0.27",
    "react-stripe-elements": "^5.1.0",
    "react-stripe-script-loader": "^1.0.16",
    "react-switch": "^5.0.1",
    "react-toastify": "^5.4.0",
    "react-toggle": "^4.1.1",
    "react-tooltip": "^3.11.2",
    "regenerator-runtime": "^0.13.5",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^0.88.2",
    "styled-components": "^4.4.1",
    "sweetalert2-react": "^0.8.3",
    "videojs": "^1.0.0",
    "videojs-record": "^4.0.0",
    "webfontloader": "^1.6.28",
    "webrtc-adapter": "^7.5.1",
    "yup": "^0.27.0"
  },
  "scripts": {
    "start": "react-app-rewired start",
    "build": "set NODE_ENV=production && react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all",
      "IE 11"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "IE 11"
    ]
  },
  "devDependencies": {
    "animate-sass": "^0.8.2",
    "es-check": "^5.1.1",
    "react-app-rewired": "^2.1.6"
  }
}

.babelrc 内部

{
    "presets": ["@babel/env","@babel/preset-react",["@babel/preset-env", { "targets": "defaults" }]],
    "plugins" : ["@babel/plugin-syntax-dynamic-import"]
}

任何人都可以指导我正确的方向,如何让 babel 正确编译 ES5 的东西吗?你会注意到这里我使用的是react-app-rewired而不是弹出,我认为这不会引起问题,所以任何帮助表示感谢。

更新:

"Object doesn't support property or method 'repeat'" - when running in development mode. How do I polyfill this? I think this may be the problem.  

最佳答案

这与 this Github issue in core-js 非常相似

按照该线程中的建议,您可以将其添加到您的预设配置中

 { modules: false,
   useBuiltIns: 'entry',
   corejs: 3 }

或者直接导入函数

import 'core-js/features/string/repeat';

关于javascript - React App 将 JS 转换为 ES5 以针对 IE 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64066045/

相关文章:

java - MVC 或 Rest 或两者

javascript - 从 Titanium Mobile 中的 TableView 打开新窗口

javascript - React native 能够使用 access_token 进行 GET 但无法 POST,它返回 "Unauthenticated."

javascript - 如何创建范围 slider ? ( native react )

javascript - 添加新组件时,组件数组不会重新渲染 - ReactJS

javascript - 尝试将类导出为模块 ES6 babel

vue.js - VueJs 3 + Vuetify : Not working in IE and Edge

javascript - Angular:动态呈现时 SVG 路径标记不可见

node.js - React 前端连接到我计算机上的开发服务器,而不是托管在 Azure VM 上的 Node 服务器,为什么使用 localhost

typescript - TS 转译 : babel vs tsc