<tbody id="86a2i"></tbody>


<dd id="86a2i"></dd>
<progress id="86a2i"><track id="86a2i"></track></progress>

<dd id="86a2i"></dd>
<em id="86a2i"><ruby id="86a2i"><u id="86a2i"></u></ruby></em>

    <dd id="86a2i"></dd>

    背景

    最近遇到了一個比較棘手的問題,客戶要求我們的react應用ios9上運行,我們的應用在ios9上是白屏顯示,所以需要做一些兼容。

    遇到問題

    遇到問題之后有一個更大的問題就是手上沒有ios9的機器,畢竟這個太他娘的古老了,我就去搜了一下ios9的出現時間,根據百度可知蘋果IOS9將于2015年9月16日正式向用戶推送,但是同時可以知道ie11于2013年10月17日隨Windows 8.1發行,所以(我猜的)只要能夠兼容ie11,那么兼容ios9應該就問題不大了,而且目前已知的解決兼容ie的方案比較多,沒找到啥兼容ios的方案,就先從ie入手。

    解決方案

    已知react兼容ie可以使用babel,實際上項目里面已經用了babel,只不過沒有兼容ie11。

    初始配置

    {
        // react 版本
        "react": "^16.13.1",
        "react-dev-utils": "^10.2.1",
        "react-dom": "^16.13.1",
        "react-redux": "^7.2.0",
        "react-rnd": "^10.2.2",
        "react-router-dom": "^5.2.0",
        "redux": "^4.0.5",
        // babel 版本
        "@babel/core": "7.9.0",
        "babel-eslint": "10.1.0",
        "babel-jest": "^24.9.0",
        "babel-loader": "8.1.0",
        "babel-plugin-import": "^1.13.5",
        "babel-plugin-named-asset-import": "^0.3.6",
        "babel-preset-react-app": "^9.1.2",
        // browserslist
        "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      },
      // babel config
      "babel": {
      "presets": [
          "react-app"
        ],
        "plugins": [
          [
            "import",
            {
              "libraryName": "antd-mobile",
              "style": "css"
            }
          ]
        ]
      }
    }
    

    ie的報錯顯示是

    ReactJS?應用兼容ios9對標ie11解決方案

    安裝@babel/preset-env

    在語法正確的情況下遇到語法錯誤/缺少標識符的報錯很大概率可能是es6+語法沒有被編譯成es5的語法,查看打包后的文件存在大量的const/let/解構等沒有被編譯。

    SCRIPT1002: 語法錯誤

    SCRIPT1010: 缺少標識符

    ReactJS?應用兼容ios9對標ie11解決方案

    • 安裝 @babel/preset-env 和 babel-polyfill
    yarn add @babel/preset-env babel-polyfill --save-dev
    
    • 配置babel(我的babel是寫在packjson里面的)
    // index.tsx 頂部引入
    import 'babel-polyfill';
    
    // presets 添加 @babel/preset-env ,target 配置 "ie > 9"
    "babel": {
      "presets": [
        "react-app",
        [
          "@babel/preset-env",
          {
            "targets": {
              "ie": 9
            }
          }
        ]
      ],
      "plugins": [
        [
          "import",
          {
            "libraryName": "antd-mobile",
            "style": "css"
          }
        ]
      ]
    }
    
    • 配置 browserslist,修改到ie>=9
      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all",
          "ie >= 9"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version",
          "ie >= 9"
        ]
      },
    

    安裝 @babel/plugin-proposal-decorators 和 @babel/plugin-proposal-class-properties

    其實配置完@babel/preset-env應該就可以了,但是項目里面使用了裝飾器和類,所以還是報錯:

    SyntaxError: xxx.ts: Decorators are not enabled.
    If you are using ["@babel/plugin-proposal-decorators", { "version": "legacy" }], make sure it comes *before* "@babel/plugin-proposal-class-properties" and enable loose mode, like so:
            ["@babel/plugin-proposal-decorators", { "version": "legacy" }]
            ["@babel/plugin-proposal-class-properties", { "loose": true }]
      27 | }
      28 | 
    > 29 | @StoreConfig({ name: "auth", resettable: true })
         | ^
      30 | export class AuthStore extends Store<Auth> {
      31 |   constructor() {
      32 |     super(createInitialState());
    

    ReactJS?應用兼容ios9對標ie11解決方案按照提示安裝 @babel/plugin-proposal-decorators@babel/plugin-proposal-class-properties。(ps:使用yarn安裝,因為項目使用的是node sass,所以node版本是14.16,對應的npm是6,安不上這兩個插件。)

    • 安裝 @babel/plugin-proposal-decorators@babel/plugin-proposal-class-properties
    yarn add @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties --save-dev
    
    • 按照報錯提示新增babel配置
    "babel": {
      "presets": [
        "react-app",
        [
          "@babel/preset-env",
          {
            "targets": {
              "ie": 9
            }
          }
        ]
      ],
      "plugins": [
        [
          "@babel/plugin-proposal-decorators",
          {
            "legacy": true
          }
        ],
        [
          "@babel/plugin-proposal-class-properties",
          {
            "loose": true
          }
        ],
        [
          "import",
          {
            "libraryName": "antd-mobile",
            "style": "css"
          }
        ]
      ]
    }
    

    安裝promise

    其實到上面一步已經差不多了,如果還報Promise undefined,可以單獨引入Promise掛載到windeow上。

    結語

    雖然就是幾個配置的問題但是真的搞了好長時間,大概弄了有兩天這樣,配置項真的太痛了,希望下次不要再去配了。

    以上就是ReactJS 應用兼容ios9對標ie11解決方案的詳細內容,更多關于ReactJS兼容ios9對標ie11的資料請關注其它相關文章!

    原文地址:https://juejin.cn/post/7185710915856105527

    相關文章:

    免费一级a片在线播放视频|亚洲娇小性XXXX色|曰本无码毛片道毛片视频清|亚洲一级a片视频免费观看
    <tbody id="86a2i"></tbody>

    
    
    <dd id="86a2i"></dd>
    <progress id="86a2i"><track id="86a2i"></track></progress>

    <dd id="86a2i"></dd>
    <em id="86a2i"><ruby id="86a2i"><u id="86a2i"></u></ruby></em>

      <dd id="86a2i"></dd>