【发布时间】:2018-01-12 04:58:19
【问题描述】:
只有在某些浏览器上我才能得到
Uncaught ReferenceError: Set is not defined.
它是使用 create-react-app 创建的 reactjs 应用。
为什么我仍然收到错误消息?
如何使用 polyfill?
reactjs默认不使用polyfill吗?
webpack.config.prod.js
entry: [require.resolve('./polyfills'), paths.appIndexJs],
polyfills.js
'use strict';
if (typeof Promise === 'undefined') {
// Rejection tracking prevents a common issue where React gets into an
// inconsistent state due to an error, but it gets swallowed by a Promise,
// and the user has no idea what causes React's erratic future behavior.
require('promise/lib/rejection-tracking').enable();
window.Promise = require('promise/lib/es6-extensions.js');
}
// fetch() polyfill for making API calls.
require('whatwg-fetch');
// Object.assign() is commonly used with React.
// It will use the native implementation if it's present and isn't buggy.
Object.assign = require('object-assign');
// In tests, polyfill requestAnimationFrame since jsdom doesn't provide it yet.
// We don't polyfill it in the browser--this is user's responsibility.
if (process.env.NODE_ENV === 'test') {
require('raf').polyfill(global);
}
【问题讨论】:
-
看来我们需要在需要 polyfill 的 reacttjs 组件上使用
import 'babel-polyfill'。但我们为什么要这样做? babel loader 不能处理吗? -
我在某些 Chrome/Android 浏览器上看到了这个错误,即使我已经用
https://cdn.polyfill.io/v2/polyfill.min.js?features=...,Set,Map"></script>设置了 polyfill
标签: javascript reactjs