create-react-app 保留了 ES5 之后的 JavaScript

Chr*_*phe 5 ecmascript-5 typescript create-react-app

我正在使用 create-react-app 的 TypeScript 风格:

npx create-react-app my-app --typescript
Run Code Online (Sandbox Code Playgroud)

当我构建应用程序时,捆绑包仍然包含 ES5 后的代码,例如 Symbol。我缺少什么?我的 tsconfig 文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*phe 2

我发现create-react-app有一个名为react-app-polyfill的关联polyfill包,默认情况下不包含在解决方案中

将目标设置为 ES5 还不够,您还需要获取 polyfill 包并在代码中引用它。例如对于 IE11 支持:

// This must be the first line in src/index.js
import 'react-app-polyfill/ie11';
Run Code Online (Sandbox Code Playgroud)