【发布时间】:2019-08-15 02:46:47
【问题描述】:
我有用 react-router、redux、webpack4、babel 构建的 react-hooks 应用程序。 而且我无法在 IE 浏览器上获得 react 应用程序渲染。
我猜这是将 es6 转换为 es5 的问题。
这是我尝试过的事情。 但它们都不起作用。
index.html
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-sham.min.js"></script>
index.js 在索引字段时导入'core-js'或'react-app-polyfill'。
import 'core-js';
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/ie11';
babel.js 向 env 添加目标选项。
presets: [
[
'env',
{
useBuiltIns: 'entry',
targets: {
browsers: [
'last 1 version',
'> 1%',
'not dead',
'ie >= 9'
],
uglify: true,
},
loose: true,
modules: isTest ? 'commonjs' : false,
debug: isTest ? false : true,
},
],
'react',
],
plugins: [
'transform-runtime',
'transform-class-properties',
'transform-object-rest-spread',
'syntax-dynamic-import',
]
package.json
"browserslist": [
"last 1 version",
"> 1%",
"not dead",
"ie >= 9"
],
webpack.base.js 将浏览器选项添加到 'postcss-loader, autoprefixer'。
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
options: babelConfig,
},
],
},
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [
{
loader: devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
minimze: true,
sourceMap: devMode,
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
options: {
indent: 'postcss',
plugins: [
autoprefixer({
browsers: ['last 1 version',
'> 1%',
'not dead',
'ie >= 9'
],
}),
],
sourceMap: devMode,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: devMode,
includePaths: ['client/styles/main.scss'],
},
},
],
},
],
},
...
},
版本
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^4.3.1",
"connected-react-router": "^6.4.0",
"react-redux": "^6.0.0",
"webpack": "^4.16.5",
"babel-core": "^6.26.3",
"core-js": "^2.5.7",
"react-app-polyfill": "^1.0.2",
------------------- 版本 --------------- ------------------
更新:在 IE 浏览器中添加错误消息并创建代码沙箱。
------------------- 版本 --------------- ------------------
更新到最新的 IE 版本后。 我在vendors.lazy-chunk.js 中得到Back navigation caching error 和JS syntax error。
------------------- 版本 --------------- ------------------
登录功能代码示例更新。 code sample
【问题讨论】:
-
您能否创建一个代码沙箱 (codesandbox.io/s/new) 向我们准确展示您在说什么。将代码从 ES6+ 编译到 ES5 时的错误将取决于所使用的特定函数。
-
你遇到了什么错误?
-
IE的控制台一定有错误,请检查一下,更新你的问题。
-
你有没有使用F12开发工具检查IE控制台是否有错误?我们无法仅使用上述代码 sn-p 重现该问题。您能否提供一个使用 CodeSandbox 或 StackBlitz(stackblitz.com) 的最小的、可重现的示例?以便我们更好地理解这个问题。另外,react 应用支持 IE11 可以参考this thread。
-
CodeSandbox 无法在 IE 11 中运行,所以我在 StackBlitz 中使用您的代码做了一个演示:stackblitz.com/edit/react-vhafos。该应用程序似乎在 IE 11 中运行良好:react-vhafos.stackblitz.io。您可以与演示中的依赖项进行比较,看看是否遗漏了一些依赖项。你用的是什么版本的IE?控制台中出现的错误表明 F12 开发工具有问题。请移至最新版本的 IE 并重试。也可以参考this thread。
标签: reactjs internet-explorer webpack babeljs