[关闭]
@chris-ren 2016-07-19T02:13:19.000000Z 字数 3252 阅读 2551

前端IE8兼容问题修改

react redux router antDesign ie8


备注:development_hot环境不支持IE8,请设置环境变量为ie8_production: export NODE_ENV=ie8_production

1. package.json修改:
增加如下配置,增加兼容IE8相关的shim或polyfill:

  1. "console-polyfill": "^0.2.2",
  2. "es5-shim": "^4.4.1",
  3. "es3ify-loader": "^0.2.0",
  4. "es6-promise": "^3.0.2",
  5. "fetch-ie8": "^1.4.0",
  6. "core-js": "^2.0.2",
  7. "html5shiv": "^3.7.3",
  8. "jquery": "^1.7.2"

2..babelrc配置文件修改:
去掉如下代码(开发环境的热加载需要去掉,热加载暂不支持IE8):

  1. "development": {
  2. "presets": ["react-hmre"]
  3. },

3. webpack中_base.js修改:
头部的import修改为require引用,如下:

  1. const webpack = require('webpack');
  2. const config = require('../../config');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');

babel-loader中的热加载选项去掉。配置如下:

  1. {
  2. test : /\.(js|jsx)$/,
  3. exclude : /node_modules/,
  4. loader : 'babel'
  5. }

增加es3ify-loader 解决default在IE8中作为保留字不能识别问题

  1. postLoaders:[
  2. {
  3. test:/\.js$/,
  4. loaders:['es3ify-loader']
  5. }
  6. ]

4.index.html修改:
增加如下代码,支持IE

  1. <meta http-equiv="x-ua-compatible" content="ie=edge"/>

5.index.js中修改:
入口文件的import修改为require

  1. /**
  2. * 以下require要放在文件最开始,且不能使用'import',因为babel会把'
  3. * import'转译为`Object.defineProperty`,IE8不支持该语法
  4. * (但是在 `require('es5-shim')`后就会被转译为支持的语法).
  5. */
  6. require ('es5-shim');
  7. require ('es5-shim/es5-sham');
  8. require ('console-polyfill');
  9. require('es6-promise');
  10. require('fetch-ie8');
  11. require('core-js/fn/object/assign');
  12. require ('babel-polyfill');
  13. require ('html5shiv');
  14. /**
  15. * 全局定义jquery,antDesignve中Velocity在IE8下需要加载jquery
  16. */
  17. window.jQuery = require('jquery');
  18. /**
  19. * 以下不能使用'imoprt',因为import会在`require('es5-shim')`之前执行
  20. */
  21. const React = require('react');
  22. const ReactDOM = require('react-dom');
  23. const createRoutes = require('./framework/routes');
  24. const configureStore = require('./framework/configureStore').configureStore;
  25. const Provider = require('react-redux').Provider;
  26. const loginUserSuccess = require('./framework/actions').authActions.loginUserSuccess;
  27. const {Router,hashHistory,browserHistory} = require('react-router');
  28. const syncHistoryWithStore = require('react-router-redux/lib/sync').default;

6.antDesign相关修改:
(1)报错:Velocity: IE8 and below require jQuery to be loaded before Velocity
原因:antDesign中使用了Velocity,Velocity在IE8以下浏览器需要增加jQuery的依赖
解决方法:增加jquery模块,在首页index.js中引入全局jquery

  1. window.jQuery = require('jquery');

(2)图标组件使用 iconfont.cn,默认公网可访问。但是在没有网络的时候,需要本地部署。并且由于antDesign中less样式文件IE8不兼容,修改为本地加载。

7.其它修改:
(1)由于react-router-redux的index.js中使用了export xx from xx的语法,这种语法babel会转译为bject.defineProperty,这种语法在IE8中不支持,因此我们在引用的时候需要做如下修改:
修改前:

  1. import { push, routerActions } from 'react-router-redux';

修改后:

  1. import {push, routerActions} from 'react-router-redux/lib/actions';

备注:以后代码中,为了兼容IE8引用react-router-redux相关内容时,不能直接引用,需要引用到具体的js。

(2)代码中不能写export * from xxx的语法

(3)由于将src/framework/actions/index.js中内容修改为:

  1. import * as authActions from './auth';
  2. import * as menuActions from './menu';
  3. import * as userActions from './user';
  4. import * as supportActions from './support';
  5. export {authActions};
  6. export {menuActions};
  7. export {userActions};
  8. export {supportActions};

在其他文件引用的时候需要进行修改,如需要调用supportActions中方法,引用时如下:

  1. import { supportActions } from '../actions';
  2. //采用如下方式进行调用
  3. supportActions.initialize();

(4) 在执行checkBrowserSupport方法时失败,返回reject()
解决方法:去掉了'addEventListener' in window的判断

(5)react-charts.js修改
修改源码(暂未提交),参照 https://github.com/jhudson8/react-chartjs/issues/69

(6)登陆页面样式修改
登陆页面之前采用antDesign的Flex布局,但是Flex布局在IE8下不能很好地显示,修改为普通布局+css实现相同的显示效果。

7. 遗留问题:

问题1:webpack配置文件中分开app\vendor打包后一直报错forEach()方法支持,打成一个包正常,原因未查明。
问题2:react-router中使用了Object.defineProperty,IE8中不支持,该问题暂未找到合适的方法,修改了源码,将源码中相关内容修改了。
问题:css3样式IE8兼容问题 ,placegolder IE8不支持

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注