简体   繁体   English

ReactJS-SCRIPT1010:预期标识符-生产版本未在IE11上运行

[英]ReactJS - SCRIPT1010: Expected identifier - Production build not running on IE11

I created a new project with create-react-app today. 我今天用create-react-app创建了一个新项目。 The production build is not running fine on IE11, the console shows following error: 生产版本在IE11上运行不正常,控制台显示以下错误:

SCRIPT1010: Expected identifier

The line it points to inside my main.js: 它指向我的main.js内部的行:

{var n=e&&e.__esModule?function(){return e.default}:function(){return e};

The error is after the e.(default) above. 错误在上面的e。(默认)之后。 My package json is plain: 我的包json很简单:

{
  "name": "sample-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

Strange enough, my dev server works perfectly on IE11 so issue is only with production build. 奇怪的是,我的开发服务器可以在IE11上完美运行,因此问题仅在于生产版本。 It works well on Chrome as well. 它也可以在Chrome上正常运行。 Is that I need to have polyfills? 我需要保鲜纸吗?

Here is how I fixed it. 这是我固定的方法。 Not to forget Mr React "Dan Abramov" helped me in this. 不要忘了React先生“ Dan Abramov”在这方面帮助了我。

So the issue is by default the application was being rendered on IE7 that is not on only IE IE11/EDGE that support the transpiled build. 因此,问题是默认情况下,应用程序是在IE7上呈现的,而不仅仅是在支持已编译版本的IE IE11 / EDGE上呈现。 So I had to mention the meta information to let browser know that the intended browser is IE11/edge. 因此,我不得不提及元信息,以使浏览器知道预期的浏览器是IE11 / edge。 Add this in the head section of your index.html: 将其添加到index.html的head

<meta http-equiv="X-UA-Compatible" content="IE=edge">

Now as I got, you ay also see some error in console that reads: 现在,正如我所知,您还会在控制台中看到以下错误:

SCRIPT5009: 'Set' is undefined

Not to worry, there is a way around that as well: https://reactjs.org/docs/javascript-environment-requirements.html 不用担心,还有一种解决方法: https : //reactjs.org/docs/javascript-environment-requirements.html

Here is the issue I discussed with Dan on git: https://github.com/facebook/create-react-app/issues/4255 这是我在git上与Dan讨论的问题: https : //github.com/facebook/create-react-app/issues/4255

It seems like the order of polyfills is also a factor. 似乎polyfills的顺序也是一个因素。 There are several solutions in this issue that could help resolve this. 此问题中有几种解决方案可以帮助解决此问题。 https://github.com/facebook/react/issues/8379 https://github.com/facebook/react/issues/8379

You need to include babel-polyfills and transpile down to ES5 for IE11 to work. 您需要包括babel-polyfills并向下移植到ES5才能使IE11正常工作。

React 16 depends on the collection types Map and Set. React 16取决于集合类型Map和Set。 If you support older browsers and devices which may not yet provide these natively (eg IE < 11) or which have non-compliant implementations (eg IE 11), consider including a global polyfill in your bundled application, such as core-js or babel-polyfill. 如果您支持的旧版浏览器和设备可能尚未提供本机版本(例如IE <11)或具有不兼容的实现(例如IE 11),请考虑在捆绑的应用程序中包括全局polyfill,例如core-js或babel -polyfill。

https://reactjs.org/docs/javascript-environment-requirements.html https://reactjs.org/docs/javascript-environment-requirements.html

You can include a polyfill via webpack like so 您可以像这样通过webpack包含polyfill

entry: {
      app: [
        'babel-polyfill',
        'react-hot-loader/patch',
        'react',
        'react-dom',
        './src/index.web.tsx',
      ]
},

This is IE problem, not react framework. 这是IE问题,不是反应框架。

the problem that "default" is a reserved word on IE IE中“默认”是保留字的问题

you can rename "default" function to a new name or use javascript function as : 您可以将“默认”函数重命名为新名称,或将javascript函数用作:

return e["default"]

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

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