【问题标题】:Babel 7 + react + redux => crash on IE / old FFBabel 7 + react + redux => 在 IE / 旧 FF 上崩溃
【发布时间】:2018-10-04 14:15:59
【问题描述】:

我遇到了react-redux 和 ie(目标为 11)的问题。

Project with : 
>     "react": "^16.5.1",
>     "react-dom": "^16.5.1",
>     "react-redux": "^5.0.7",
>     "redux": "^4.0.0",
>     "@babel/XXX" : ^7.0.0

使用 babel 并得到 polyfill:

"babel": {
    "presets": [
      "@babel/preset-react",
      [
        "@babel/preset-env",
        {
          "targets": "> 0.25%, not dead",
          "useBuiltIns": "entry"
        }
      ]
    ],
    "plugins": [
      "react-hot-loader/babel",
      "lodash",
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ],
      "@babel/plugin-proposal-function-sent",
      "@babel/plugin-proposal-export-namespace-from",
      "@babel/plugin-proposal-numeric-separator",
      "@babel/plugin-proposal-throw-expressions",
      "@babel/plugin-proposal-class-properties",
      "@babel/plugin-syntax-dynamic-import",
      "@babel/plugin-syntax-object-rest-spread"
    ]
  }

代码示例:

import React from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';

class Test extends React.PureComponent {
  render() {
    return (
      <div>
        the anwser is : {this.props.test}
      </div>
    )
  }
}

// with or without compose there is the same result
export default compose (
  connect(
    (state) => ({
      test: 42
    }),{
    })
) (Test);

在 chrome 中,我得到:“答案是:42”

在 IE11 中:空白页 ... / Collat​​or 无法初始化并且不会被使用

在 Firefox 45 中: react-router provider error another error

谢谢

【问题讨论】:

    标签: reactjs internet-explorer react-router babeljs babel-polyfill


    【解决方案1】:

    您可能希望将bowserslist 字段添加到package.json 并添加@babel/polyfill 作为依赖项。 这告诉 babel-preset-env 和 @babel/polyfill 编译时兼容浏览器列表中指定的值。

    示例(pacakge.json):

    {
      "browserslist": [
        "last 1 version",
        "> 1%",
        "maintained node versions",
        "not dead"
      ]
    }
    

    请注意,您需要使用 babel 预设环境才能使其工作。

    【讨论】:

    • 我已经在我的配置 webpack 中有 '@babel/polyfill' 并且我使用 babeljs.io/docs/en/babel-preset-env 文档,他们在选项中说目标,但也是 bowserslist
    • 您也可以在 env 选项中包含浏览器列表。我发现将它添加到 package.json 中很有用,因为其他工具可以为您提供此字段。
    • 那么我必须在哪里添加这个浏览器列表字段?在 babel.presets."@babel/preset-env" 或类似 babel/name 级别?
    • 在 package.json 中。确保根据您的需要调整浏览器。如果您针对更多旧浏览器,那么您的捆绑包可能会增大
    • 但是在 ie11 中使用这个浏览器列表字段会崩溃
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2021-02-04
    相关资源
    最近更新 更多