nickjj / manifest-revision-webpack-plugin

Write out a manifest file containing your versioned webpack chunks and assets.
ISC License
124 stars 40 forks source link

Plugin does not include compressed gzip files #53

Closed psdon closed 5 years ago

psdon commented 5 years ago

I'm using compression-webpack-plugin to compress files into gzip. I'm confused how to include .gz files to the manifest.json. Is there a way to do this?

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

new CompressionPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
          threshold: 0,
          minRatio: 0.8,
          deleteOriginalAssets: true,
    }),
    new ManifestRevisionPlugin(path.join(__dirname, 'app', 'webpack', 'manifest.json'), {
      rootAssetPath,
      ignorePaths: ['/js', '/css'],
      extensionsRegex: /\.(ttf|eot|svg|png|jpe?g|gif|ico)$/i,
    }),
nickjj commented 5 years ago

What happens if you add gz to extentionsRegex? Does it pick it up then?

psdon commented 5 years ago

It does not, it just mapped the original files.

Problem

_I know main_css.map and main_js.map is not associated with manifest-revision-webpack-plugin, just trying out :) I'm new with this compression plugin_

Here's my webpack.config.js:

const path = require('path');
const webpack = require('webpack');

/*
 * Webpack Plugins
 */
const ManifestRevisionPlugin = require('manifest-revision-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HashOutput = require('webpack-plugin-hash-output');

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

// take debug mode from the environment
const debug = (process.env.NODE_ENV !== 'production');

// Development asset host (webpack dev server)
const publicHost = debug ? 'http://0.0.0.0:2992' : '';

const rootAssetPath = path.join(__dirname, 'assets');

const hashType = debug ? '[hash]': '[contentHash]'
console.log(hashType)

module.exports = {
  // configuration
  context: __dirname,
  entry: {
    main_js: path.join(__dirname, 'assets', 'js', 'main.js'),
    main_css: [
      path.join(__dirname, 'assets', 'css', 'main.css'),
    ],
  },
  output: {
    path: path.join(__dirname, 'app', 'static'),
    publicPath: `${publicHost}/static/`,
    filename: '[name].' + hashType + '.js',
    chunkFilename: '[id].' + hashType + '.js',
    library: 'main',
  },
  optimization: {
  minimizer: [
   new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true
    }),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.css$/g,
      cssProcessor: require('cssnano'),
      cssProcessorPluginOptions: {
        preset: ['default', { discardComments: { removeAll: true } }],
      },
      canPrint: true
    }),
  ],
},
  resolve: {
    extensions: ['.js', '.css'],
  },
  devtool: 'source-map',
  devServer: {
    headers: { 'Access-Control-Allow-Origin': '*' },
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              hmr: debug,
            },
          },
          'css-loader',
          {
            loader: 'postcss-loader'
          }
        ],
      },
      { test: /\.html$/, loader: 'raw-loader' },
      { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader', options: { limit: 10000, mimetype: 'application/font-woff' } },
      {
        test: /\.(ttf|eot|svg|png|jpe?g|gif|ico)(\?.*)?$/i,
        loader: `file-loader?context=${rootAssetPath}&name=[path][name].${hashType}.[ext]`
      },
      { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ['@babel/preset-env'], cacheDirectory: true } },
    ],
  },
  plugins: [
    new HashOutput(),
    new MiniCssExtractPlugin({ filename: '[name].' + hashType + '.css', }),
//    new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }),

  ].concat(debug ? [
    new ManifestRevisionPlugin(path.join(__dirname, 'app', 'webpack', 'manifest.json'), {
      rootAssetPath,
      ignorePaths: ['/js', '/css'],
      extensionsRegex: /\.(ttf|eot|svg|png|jpe?g|gif|ico)$/i,
    }),
  ] : [
    // production webpack plugins go here
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production'),
      } }),
    new ManifestRevisionPlugin(path.join(__dirname, 'app', 'webpack', 'manifest.json'), {
      rootAssetPath,
      ignorePaths: ['/js', '/css'],
      extensionsRegex: /\.(ttf|eot|svg|png|jpe?g|gif|ico|gz)$/i,
    }),
    new CompressionPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
          threshold: 0,
          minRatio: 0.8,
          deleteOriginalAssets: true,
    }),
  ]),
};

Generated manifest.json:

{"assets":
  {
    "img/favicon.ico":"img/favicon.0ee3a2f1a989a0fd00d2c83beaa289e8.ico",
    "img/home/hack-video-bg.jpg":"img/home/hack-video-bg.af06b002c5ab863f9ad6c669e5431142.jpg",
    "svg/home/circle-x.svg":"svg/home/circle-x.0cf375ec93e456cff1dc9efa64d00b41.svg",
    "svg/home/header-content.svg":"svg/home/header-content.64bd2ba92ee75f05f7f905ef017a9a02.svg",
    "svg/home/play-button.svg":"svg/home/play-button.a873bf7be2f259b637852584c651b7bc.svg",
    "svg/home/sign-in.svg":"svg/home/sign-in.8d986766fa3457f85915b9912f1a9fcf.svg",
    "svg/logo.svg":"svg/logo.b1965a5843a04fe9c4fc4c380fc44a65.svg",
    "main_css.map":"main_css.ac630a5fe947300ebd49.js.map",
    "main_js.map":"main_js.83140fd204362d432f3a.js.map"}, 

  "publicPath":"/static/"
}
nickjj commented 5 years ago

I never used that compression plugin nor have I ever really checked what order Webpack runs plugins in, but what happens if you swap the order of the compression plugin to go before this plugin?

psdon commented 5 years ago

Closing this issue now. I decided to used Nginx on-the-fly Gzip compression instead.