我在 React 中实际提供我的 bundle.js 文件的 gzip 压缩时遇到问题。我试图显着减小它的大小,并且我已经完成了丑化和重复数据删除等......它从 2.9mb 下降到 2.6mb,这对我来说真的很奇怪。我现在正在使用压缩插件,并得到一个输出的 gzip 文件,但现在我仍在使用 bundle.js 而不是 bundle.js.gz。
我不想使用 express 的压缩中间件,因为我正在构建过程中执行 gzip。无论如何,这是我的 distServer 文件:
import express from 'express';
import path from 'path';
import open from 'open';
/* eslint-disable no-console */
const port = process.env.PORT || 3000;
const app = express();
app.use(express.static('dist'));
app.get('*.js', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
next();
});
app.get('*.css', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/css');
next();
});
app.get('*', function(req, res) {
res.sendFile(path.join( __dirname, '../dist/index.html'));
});
app.listen(port, function(err) {
if (err) {
console.log(err);
} else {
open(`http://localhost:${port}`);
}
});
我的 webpack 配置是:
import webpack from 'webpack';
import path from 'path';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
var CopyWebpackPlugin = require('copy-webpack-plugin');
var CompressionPlugin = require("compression-webpack-plugin");
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
export default {
devtool: 'source-map',
noInfo: false,
entry: [
'./src/index'
],
target: 'web',
output: {
path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
// new BundleAnalyzerPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"production"'}
}),
new ExtractTextPlugin('styles.css'),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
mangle: true,
compress: {
warnings: false, // Suppress uglification warnings
pure_getters: true,
unsafe: true,
unsafe_comps: true,
screw_ie8: true
},
output: {
comments: false,
},
exclude: [/\.min\.js$/gi] // skip pre-minified libs
}),
new CopyWebpackPlugin([
{ from: 'src/robots.txt', to: 'robots.txt' },
{ from: 'src/sitemap.xml', to: 'sitemap.xml' }
], {
copyUnmodified: true
}),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0
})
],
module: {
loaders: [
{test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
{test: /(\.css)$/, loader: ExtractTextPlugin.extract("css?sourceMap")},
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
{test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
{test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
{test: /\.(jpe?g|png|gif|svg)$/i, loader: "url?limit=10000"},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'},
{
test: /favicon\.ico$/,
loader: 'url',
query: {
limit: 1,
name: '[name].[ext]',
},
}
]
}
};
我认为 app.get 函数会根据需要引入这些 gzip 文件,但我可能遗漏了一步?另外,在我的 index.html 文件中,我是否需要省略将捆绑文件一起引入的脚本标记?
任何指导和想法将不胜感激!