2016-01-16 31 views
8

我正在嘗試第一次使用Webpack。我一直在使用Gulp和Browserify一段時間,並且對它很滿意。在這一點上,我只是測試了幾個Webpack插件。即compression-webpack-plugin。我以前從來沒有使用過壓縮,如果我做出任何noob錯誤,那麼我就沒有使用過壓縮。未提供Webpack gzip壓縮包,未壓縮包是

下面是我的webpack.config.js。結果是我得到了一個main.js,main.js.gz,main.css和index.html。 main.js被注入到index.html中,但是如果我在瀏覽器中打開index.html,它會提供未壓縮的main.js,而不是壓縮的main.js.gz.我已經讀過,我不需要在腳本標記中包含.gz擴展名,並且html-webpack-plugin不包含它,所以我認爲事情工作正常,但未壓縮的main.js被服務,而不是壓縮的。

var path = require('path'); 
var ExtractTextPlugin = require('extract-text-webpack-plugin'); 
var HtmlWebpackPlugin = require('html-webpack-plugin'); 
var CompressionPlugin = require('compression-webpack-plugin'); 

module.exports = { 
    entry: './app/scripts/main.js', 
    output: { 
    path: path.join(__dirname, 'public'), 
    filename: '[name].js', 
    chunkFilename: '[id].js' 
    }, 
    module: { 
    loaders: [ 
     {test: /\.scss$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')}, 
     {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'} 
    ] 
    }, 
    plugins: [ 
    new HtmlWebpackPlugin({ 
     hash: true, 
     template: 'app/index.html', 
     inject: 'body' 
    }), 
    new ExtractTextPlugin('[name].css'), 
    new CompressionPlugin() 
    ] 
}; 

回答

10

script - 標記加載main.js.gz,而不是main.js在包括main.js的情況下,你需要配置你的Web服務器(Apache時,Nginx的,等等)

請記住,配置應加載.js.gz.js取決於瀏覽器是否接受gzip。 Web服務器可以在HTTP請求標頭中檢查它Accept-Encoding: gzip, deflate

在瀏覽器devtools中,您將在任何情況下看到main.js。

+0

爲了加載PRE-ZIP(未生成)文件,我應該怎麼做? –

+0

@RoyiNamir您需要編寫重寫規則 –

+0

我沒有找到合適的規則來爲js文件提供gz文件 –

4

您可以通過nginx gzip static module輕鬆有條件地爲gz靜態服務。服務器檢查app.js.gz文件是否爲例如要求/statics/app.js存在並透明地提供服務。無需更改您的應用程序或檢測Accept-Encoding - 所有由nginx模塊處理的內容。下面是配置片段:

location /statics/ { 
    gzip_static on; 
} 
0

我有點晚了這個線程,但想到我會加我2C。我使用webpack生成了一個gz文件,但是Apache保持服務於未壓縮的文件(或者如果它不存在則發生錯誤),儘管Accept-Encoding被正確設置。原來,我不得不取消註釋AddEncoding x-gzip .gz .tgz並重新加載httpd。爲了記錄,已經設置了AddType application/x-gzip .gz .tgz,並且我在Chrome 62上使用了Apache 2.4.6。感謝上面的Dmitry讓我看看conf/httpd.conf文件。