2017-02-26 51 views
0

您好我的項目正在與reactjs和webpack,加載網站時,如果運行在不支持reactjs或webpack的舊瀏覽器,我不能顯示我的瀏覽器不支持消息(我檢查支持與modernizr圖書館)。有沒有辦法讓我顯示一條消息,並在他們加載之前使用modernizr?也許以某種方式使用可以在首先加載的html文件中使用modernizr的腳本?任何答案都會受到歡迎! (這需要在所有瀏覽器上運行)。謝謝您的幫助!不能顯示瀏覽器支持消息

回答

0

如果您確切知道您使用的是哪些功能,而舊版瀏覽器不支持,則可以選擇加載「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> 
+0

我想是不支持瀏覽器,只是爲了顯示一條消息,我不支持它,我的問題是,我不知道我不支持它,因爲我不能使用庫,如modiernizr之前我的webpack加載 –