Webpack gzip包 - 未捕获的SyntaxError:意外的令牌

问题描述 投票:0回答:1

我正在使用webpack进行捆绑的反应项目。我想减少我的包大小,所以决定使用压缩插件来提供一个gzip文件以获得一个不错的小包大小。项目建设很好,我得到一个很好的小包大小,但这是我的问题..当我去服务我当前的项目这里是我得到的错误:

enter image description here

调查我意识到,无论出于何种原因而不是提供main.js或vendor.js的内容,它都会返回我的index.html文件

enter image description here

我相当肯定我的apache服务器配置为处理gzip编码,因为我可以在响应头中看到它:

enter image description here

这是我正在使用的webpack配置:

const appConstants = function() {
    switch (process.env.NODE_ENV) {
        case 'local':
            const localConfig = require('./config/local');
            return localConfig.config();
        case 'development':
            const devConfig = require('./config/development');
            return devConfig.config();
        case 'production':
        default:
            const prodConfig = require('./config/production');
            return prodConfig.config();
    }
};

const HtmlWebPackPlugin = require("html-webpack-plugin");
const webpack = require('webpack');
const CompressionPlugin = require('compression-webpack-plugin');


const htmlWebpackPlugin = new HtmlWebPackPlugin({
    template: "./src/index.html",
    filename: "./index.html",
    hash: true
});

const compressionPlugin = new CompressionPlugin({
    filename: "[path].gz[query]",
    test: /\.(js|css)$/,
    algorithm: 'gzip',
    deleteOriginalAssets: true
});

let webpackConfig = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.css$/,
                exclude: [ /assets/, /node_modules/ ],
                use: [
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                            importLoaders: 1,
                            localIdentName: "[name]_[local]_[hash:base64]",
                            sourceMap: true,
                            minimize: true
                        }
                    }
                ]
            },
            {
                test: /\.(pdf|jpg|png|gif|svg|ico)$/,
                exclude: [/node_modules/],
                use: [
                    {
                        loader: 'file-loader'
                    },
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|svg)$/,
                exclude: [/node_modules/],
                use: {
                    loader: 'url-loader?limit100000'
                }
            }
        ]
    },
    entry: [ "@babel/polyfill", "./src/index.js"],
    output: {
        publicPath: appConstants().DS_BASENAME ? JSON.parse(appConstants().DS_BASENAME) : '/',
        chunkFilename: '[name].[chunkhash].js'
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
    },
    plugins: [
        htmlWebpackPlugin,
        compressionPlugin,
        new webpack.DefinePlugin({
            'process.env': appConstants()
        }),
        new webpack.EnvironmentPlugin(['NODE_ENV']),
    ],
    devServer: {
        historyApiFallback: true
    }
};

// configure source map per-env
if (process.env.NODE_ENV === 'local') {
    webpackConfig.devtool = 'source-map';
} else {
    webpackConfig.devtool = 'hidden-source-map';
}

module.exports = webpackConfig;

我无法弄清楚为什么浏览器没有读取/识别gzip。我曾尝试过几个类似问题但没有解决方案的帖子。

reactjs webpack gzip webpack-4
1个回答
0
投票

你需要添加中间件函数来处理带有后缀的.js和.css文件.gz就像这样

const app = express()

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.use('/dist', serve('./dist', true))
app.use(express.static('./dist'));

您必须在express.static之前添加中间件功能

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.