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

2023-11-26

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

npx create-react-app my-app --typescript

当我构建应用程序时,捆绑包仍然包含 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"
  ]
}

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

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

// This must be the first line in src/index.js
import 'react-app-polyfill/ie11';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

create-react-app 保留了 ES5 之后的 JavaScript 的相关文章

随机推荐