获取错误承诺在 IE11 中未定义
Getting Error Promise is undefined in IE11
我正在将 React 代码转换为打字稿,
tsconfig 中的目标是 es5.
在 IE 11 中的 运行 我得到一个错误 "Promise is undefined"
我知道我需要 polyfill,但是怎么做呢?
我没有使用 Babel。
以下是我的Webpack.config
var webpack = require("webpack");
var Promise = require('es6-promise').Promise;
var paths = require('./config/paths');
var HtmlWebpackPlugin = require('html-webpack-plugin');
//var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var AureliaWebpackPlugin = require('aurelia-webpack-plugin');
var publicPath = '/';
var publicUrl = '';
module.exports = {
entry: {
app: [
'core-js/fn/promise',
'./Generated Files/app.js'
],
vendor: paths.vendorPath,
},
output: {
path:__dirname + "/dist",
filename: 'bundle.js',
publicPath: publicPath
},
devtool: '#source-map',
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{
test: /.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
//exclude: /node_modules/,
},
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
loader: 'file',
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('development')
}
}),
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
}),
// For using jQuery
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
'window.jQuery': 'jquery',
'window.$': 'jquery',
}),
new webpack.ProvidePlugin({
"Promise": "promise-polyfill"
}),
// new AureliaWebpackPlugin(),
new webpack.optimize.CommonsChunkPlugin({/* chunkName= */name:"vendor", /* filename= */filename:'static/js/vendor.js'})
]
};
您需要添加 Promise polyfill。
在您的包中加入 polyfill。
https://github.com/stefanpenner/es6-promise
仅在浏览器/设备需要时加载 polyfill:
https://www.npmjs.com/package/polyfill-io-feature-detection
var ES6Promise = require("es6-promise");
ES6Promise.polyfill();
var axios = require("axios");
在 axios 上面写这个对我有用
也许其他选项也有效
我遇到的主要是 IE 缓存问题
安装 es6-promise-promise
webpack 插件也有效
npm install es6-promise-promise
并包括
new webpack.ProvidePlugin({
Promise: 'es6-promise-promise', // works as expected
});
在 webpack 插件中
我会用更多可能的选项编辑这个答案
您需要包含 Polyfill 才能在 Internet Explorer 中运行。
import { polyfill } from 'es6-promise'; polyfill();
为 browsers/devices 需要它的人添加复方药丸。
安装这些包:
npm install es6-promise
npm install whatwg-fetch
然后更新webpack配置:
module.exports = {
context: path.resolve(__dirname, 'src'),
entry: ['whatwg-fetch', './index.js'], <========== add whatwg-fetch !!!!
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
resolve: {extensions: ['.js', '.jsx']},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({ template: 'index.html' }),
new webpack.ProvidePlugin({
React: 'react',
Promise: 'es6-promise' <============ add Promises for IE !!!
}),
],
module: ...
您可以使用 babel-polyfill library which can be found in cdnjs 并提供大量我发现对 IE 兼容性有用的 polyfill(包括 Promises)。
请注意,您不必使用 babel 编译器来使用它;只需加载脚本就可以了:)
添加此脚本:
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
之后您可以在控制台中测试 Promise 在 IE 中是否正常工作
var promise = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('bar');
}, 1000);
});
promise.then(function(result) {
console.log(result);
});
从 Babel 7.4.0 开始
ES6 Promises 只是一个问题,您将遇到更多问题,例如新的赋值运算符等等。要使您的 React 解决方案在 IE11 上很好地工作,您需要做一些事情
- core-js 将提供您需要的大部分 pollyfill(尽管需要额外的 pollyfill 来获取)
- isomporphic-fetch - 因为这不是 core-js
提供的
- are-you-es5 - 通常的做法是不转译你的节点模块,但是有些模块是用 ES6 或更高版本编写的,那么它们将无法工作,所以你可以使用
are-you-es5
与 webpack 一起生成 include
和 exclude
什么和什么不转译的模式
填充
根据 @babel/pollyfill 像这样使用 core-js
重要的是要注意@babel/pollyfill建议使用core-js代替
用于 SO 上类似问题的答案
// install packages
npm install core-js
npm install regenerator-runtime
npm install isomorphic-fetch
// then in your entry point (index.ts or index.js)
import "isomorphic-fetch"
import "core-js/stable";
import "regenerator-runtime/runtime";
配置 Webpack 以转译相关 node_moduels
npm install are-you-es5
在你的 webpack 配置文件中
import {
checkModules,
buildIncludeRegexp,
buildExcludeRegexp
} from 'are-you-es5'
const result = checkModules({
path: '', // Automatically find up package.json from cwd
checkAllNodeModules: true,
ignoreBabelAndWebpackPackages: true
})
/** Returns the regexp including all es6 modules */
const es6IncludeRegExp = buildIncludeRegexp(result.es6Modules)
/** Returns the regexp excluding all es6 modules */
const es6ExcludeRegexp = buildExcludeRegexp(result.es6Modules)
{
...
module: {
rules: [
{
test: /\.(t)sx?$/,
use: {
loader: "babel-loader",
},
exclude: /node_modules/,
},
{
test: /\.(j)sx?$/,
use: {
loader: "babel-loader",
},
exclude: es6ExcludeRegexp, // don't transpile these!!!
include: es6IncludeRegExp // tranpile these
}
]
}
}
我花了很长时间才让 IE11 的一切正常工作,希望这会减轻人们我所经历的痛苦。
我正在将 React 代码转换为打字稿, tsconfig 中的目标是 es5.
在 IE 11 中的 运行 我得到一个错误 "Promise is undefined"
我知道我需要 polyfill,但是怎么做呢?
我没有使用 Babel。
以下是我的Webpack.config
var webpack = require("webpack");
var Promise = require('es6-promise').Promise;
var paths = require('./config/paths');
var HtmlWebpackPlugin = require('html-webpack-plugin');
//var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var AureliaWebpackPlugin = require('aurelia-webpack-plugin');
var publicPath = '/';
var publicUrl = '';
module.exports = {
entry: {
app: [
'core-js/fn/promise',
'./Generated Files/app.js'
],
vendor: paths.vendorPath,
},
output: {
path:__dirname + "/dist",
filename: 'bundle.js',
publicPath: publicPath
},
devtool: '#source-map',
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{
test: /.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style-loader!css-loader',
//exclude: /node_modules/,
},
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
loader: 'file',
query: {
name: 'static/media/[name].[hash:8].[ext]'
}
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('development')
}
}),
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
}),
// For using jQuery
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
'window.jQuery': 'jquery',
'window.$': 'jquery',
}),
new webpack.ProvidePlugin({
"Promise": "promise-polyfill"
}),
// new AureliaWebpackPlugin(),
new webpack.optimize.CommonsChunkPlugin({/* chunkName= */name:"vendor", /* filename= */filename:'static/js/vendor.js'})
]
};
您需要添加 Promise polyfill。
在您的包中加入 polyfill。 https://github.com/stefanpenner/es6-promise
仅在浏览器/设备需要时加载 polyfill: https://www.npmjs.com/package/polyfill-io-feature-detection
var ES6Promise = require("es6-promise");
ES6Promise.polyfill();
var axios = require("axios");
在 axios 上面写这个对我有用 也许其他选项也有效
我遇到的主要是 IE 缓存问题
安装 es6-promise-promise
webpack 插件也有效
npm install es6-promise-promise
并包括
new webpack.ProvidePlugin({
Promise: 'es6-promise-promise', // works as expected
});
在 webpack 插件中
我会用更多可能的选项编辑这个答案
您需要包含 Polyfill 才能在 Internet Explorer 中运行。
import { polyfill } from 'es6-promise'; polyfill();
为 browsers/devices 需要它的人添加复方药丸。
安装这些包:
npm install es6-promise
npm install whatwg-fetch
然后更新webpack配置:
module.exports = {
context: path.resolve(__dirname, 'src'),
entry: ['whatwg-fetch', './index.js'], <========== add whatwg-fetch !!!!
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
resolve: {extensions: ['.js', '.jsx']},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({ template: 'index.html' }),
new webpack.ProvidePlugin({
React: 'react',
Promise: 'es6-promise' <============ add Promises for IE !!!
}),
],
module: ...
您可以使用 babel-polyfill library which can be found in cdnjs 并提供大量我发现对 IE 兼容性有用的 polyfill(包括 Promises)。
请注意,您不必使用 babel 编译器来使用它;只需加载脚本就可以了:)
添加此脚本:
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
之后您可以在控制台中测试 Promise 在 IE 中是否正常工作
var promise = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('bar');
}, 1000);
});
promise.then(function(result) {
console.log(result);
});
从 Babel 7.4.0 开始
ES6 Promises 只是一个问题,您将遇到更多问题,例如新的赋值运算符等等。要使您的 React 解决方案在 IE11 上很好地工作,您需要做一些事情
- core-js 将提供您需要的大部分 pollyfill(尽管需要额外的 pollyfill 来获取)
- isomporphic-fetch - 因为这不是 core-js 提供的
- are-you-es5 - 通常的做法是不转译你的节点模块,但是有些模块是用 ES6 或更高版本编写的,那么它们将无法工作,所以你可以使用
are-you-es5
与 webpack 一起生成include
和exclude
什么和什么不转译的模式
填充
根据 @babel/pollyfill 像这样使用 core-js
重要的是要注意@babel/pollyfill建议使用core-js代替
用于 SO 上类似问题的答案
// install packages
npm install core-js
npm install regenerator-runtime
npm install isomorphic-fetch
// then in your entry point (index.ts or index.js)
import "isomorphic-fetch"
import "core-js/stable";
import "regenerator-runtime/runtime";
配置 Webpack 以转译相关 node_moduels
npm install are-you-es5
在你的 webpack 配置文件中
import {
checkModules,
buildIncludeRegexp,
buildExcludeRegexp
} from 'are-you-es5'
const result = checkModules({
path: '', // Automatically find up package.json from cwd
checkAllNodeModules: true,
ignoreBabelAndWebpackPackages: true
})
/** Returns the regexp including all es6 modules */
const es6IncludeRegExp = buildIncludeRegexp(result.es6Modules)
/** Returns the regexp excluding all es6 modules */
const es6ExcludeRegexp = buildExcludeRegexp(result.es6Modules)
{
...
module: {
rules: [
{
test: /\.(t)sx?$/,
use: {
loader: "babel-loader",
},
exclude: /node_modules/,
},
{
test: /\.(j)sx?$/,
use: {
loader: "babel-loader",
},
exclude: es6ExcludeRegexp, // don't transpile these!!!
include: es6IncludeRegExp // tranpile these
}
]
}
}
我花了很长时间才让 IE11 的一切正常工作,希望这会减轻人们我所经历的痛苦。