我想将我的react应用压缩为gzip,实际上是2.2 mb,所以我使用了 compression-webpack-plugin ,但是我无法压缩
我的 webpack.config.js
const webpack = require('webpack');
const path = require('path');
const fs = require("fs")
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CompressionPlugin = require('compression-webpack-plugin');
const VENDOR_LIBS =[
'antd','axios','moment','rc-time-picker','react',
'react-dom','react-ga','react-google-maps','react-loadable',
'react-redux','react-router','react-router-dom','recompose','redux','redux-thunk'
]
module.exports = (env) => {
const isProduction = env === 'production';
const CSSExtract = new ExtractTextPlugin('styles.css');
return {
node: {
fs: 'empty',
child_process: 'empty',
},
entry: {
bundle:'./src/app.js',
vendor:VENDOR_LIBS
},
output: {
path: path.join(__dirname, 'public'),
filename: '[name].js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}, {
test: /\.s?css$/,
use: CSSExtract.extract({
use: [
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
})
},{
test: /\.(gif|svg|jpg|png|ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
loader: "file-loader",
}],
},
plugins: [
CSSExtract,
new webpack.optimize.CommonsChunkPlugin({
name:'vendor'
}),
new HtmlWebpackPlugin({
template:'src/index.html'
}),
new CompressionPlugin(),
new BundleAnalyzerPlugin()
],
devtool: isProduction ? 'source-map' : 'inline-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
host:'0.0.0.0',
disableHostCheck: true,
}
};
};
显示我错误
TypeError:无法读取未定义的属性'emit'
之类的 CompressionPlugin 添加其他选项,
在CompressionPlugin.apply(/ media / .........
如果我向
new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
threshold: 10240,
minRatio: 0.8
})
显示我错误
抛出新的ValidationError(ajv.errors,name);
ValidationError:压缩插件无效选项
选项不应具有其他属性
我该如何解决这个问题,或者有其他方法可以gzip我的应用程序。
答案 0 :(得分:29)
将“资产”更改为“文件名”。
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
threshold: 10240,
minRatio: 0.8
})
答案 1 :(得分:2)
compression-webpack-plugin@2.0发生了重大变化。
您要么必须升级到webpack 4,要么使用compression-webpack-plugin@1.1.12。
不建议使用资产选项,请改用filename。
答案 2 :(得分:0)
您的问题是您需要与webpack 4一起运行,并且节点必须大于6.9.9.0,如文档所述:
“此模块至少需要Node v6.9.0和Webpack v4.0.0。”
您的配置也有误:
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
threshold: 10240,
minRatio: 0.8
})