对于@babel/preset-env
与useBuiltIns
结合使用的不同配置,我得到不同的输出@babel/transform-runtime
。我已阅读文档,但无法弄清楚最佳实践应该是什么。
例如,当我的目标浏览器列表包含 Edge 18 时,@babel/preset-env
withuseBuiltIns
将添加一个 polyfill string.replace
。
但是当我@babel/transform-runtime
改为使用时,不会添加该 polyfill。
所以,从这个问题开始:
Does `string.replace` need to be polyfilled for Edge 18?
Run Code Online (Sandbox Code Playgroud)
我检查了caniuse.com,它显示它完全受支持 - 这意味着不需要 polyfill。
但是,根据 Manuel Beaudru 的博客文章core-js@3,babel 和展望未来
caniuse
,mdn
并且compat-table
是很好的教育资源,但并不真正用作开发人员工具的数据源:仅compat-table
包含一组良好的 ES 相关数据并且由 @babel/preset-env 使用,但它有一些限制
并进一步:
出于这个原因,我创建了这个
core-js-compat
包:它提供了关于不同目标引擎的 core-js 模块必要性的数据。使用时core-js@3
,@babel/preset-env
将使用该新包而不是compat-table.
所以我将我的目标浏览器传递给core-js-compat
它并输出所有需要的 polfills。如下图所示,很多字符串方法需要 polyfill,主要是为了支持 Edge 18。
到现在为止还挺好。看起来Edge 18string.replace
确实需要填充。
我正在使用airbnb配置进行eslint,它给了我这个警告:
[eslint] 'isLoading' is missing in props validation (react/prop-types)
有没有办法为isLoading设置PropTypes?
const withLoading = Component => ({ isLoading, ...rest }) =>
(isLoading ? <LoadingSpinner /> : <Component {...rest} />);
Run Code Online (Sandbox Code Playgroud)
这是我如何使用它的一个例子:
const Button = ({ onClick, className, children }) => (
<button onClick={onClick} className={className} type="button">
{children}
</button>
);
Button.propTypes = {
onClick: PropTypes.func.isRequired,
className: PropTypes.string,
children: PropTypes.node.isRequired,
};
Button.defaultProps = {
onClick: () => {},
className: '',
children: 'Click me',
};
const Loading = () => (
<div>
<p>Loading...</p>
</div>
);
const …
Run Code Online (Sandbox Code Playgroud) reactjs eslint react-proptypes higher-order-components eslint-config-airbnb