如果您確切知道您使用的是哪些功能,而舊版瀏覽器不支持,則可以選擇加載「polyfills」文件。
爲了達到這樣的效果,首先需要爲polyfills創建一個單獨的包文件,如果瀏覽器不支持所需的功能,則必須加載它們。這最後一步需要你在你的html文件中有一些模板。
在你的WebPack條目:
polyfills: [
'babel-polyfill',
'whatwg-fetch'
]
在你的插件:
new HtmlWebpackPlugin({
template: './index.ejs',
filename: '../index.html',
inject: false
})
在你index.ejs文件:
<script>
var newBrowser = (
'fetch' in window &&
'Promise' in window &&
'assign' in Object &&
'keys' in Object
);
var scripts = [];
if (!newBrowser) {
scripts.push('<%=htmlWebpackPlugin.files.chunks.polyfills.entry%>');
}
scripts.push('<%=htmlWebpackPlugin.files.chunks.vendors.entry%>');
scripts.push('<%=htmlWebpackPlugin.files.chunks.app.entry%>');
scripts.forEach(function(src) {
var scriptEl = document.createElement('script');
scriptEl.src = src;
scriptEl.async = false;
document.head.appendChild(scriptEl);
});
</script>
我想是不支持瀏覽器,只是爲了顯示一條消息,我不支持它,我的問題是,我不知道我不支持它,因爲我不能使用庫,如modiernizr之前我的webpack加載 –