Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use emit event instead of this-compilation #71

Merged
merged 1 commit into from
Sep 29, 2017

Conversation

ijpiantanida
Copy link
Contributor

@ijpiantanida ijpiantanida commented Sep 2, 2017

Notable Changes

Given that compression is most likely one of the last steps on the pipeline
chain, it makes sense that this plugin uses the emit event which is the last
step to add/modify assets before they're written to disk.

This plays nicely with other plugins that create and delete files
on the fly AFTER they're compiled
(for example https://github.com/jtefera/merge-files-webpack, which is the
recommeded solution for this issue
webpack-contrib/extract-text-webpack-plugin#179)

Issues

edited by @michael-ciniawsky (Formatting)

Given that compression is most likely one of the last steps on the pipeline
chain, it makes sense that this plugin uses the `emit` event which is the last
step to add/modify assets before they're written to disk.

This plays nicely with other plugins that create and delete files
on the fly AFTER they're compiled
(for example https://github.com/jtefera/merge-files-webpack, which is the
recommeded solution for this issue
webpack-contrib/extract-text-webpack-plugin#179)
@michael-ciniawsky michael-ciniawsky added this to the 1.0.0 milestone Sep 3, 2017
@michael-ciniawsky michael-ciniawsky changed the title Use emit event instead of this-compilation refactor: use emit event instead of this-compilation Sep 3, 2017
@michael-ciniawsky
Copy link
Member

Marked as (:label: Minor) for now, but not 💯 if this should be considered a breaking change, personally I don't think so

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@michael-ciniawsky michael-ciniawsky mentioned this pull request Sep 4, 2017
@genu
Copy link

genu commented Sep 18, 2017

Whats holding up this from being pulled?

@ilan-schemoul
Copy link

  entry: path.resolve(__dirname, 'client/src'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'client/dist'),
  },
  module: {
    rules: [
      {
        test: /\.html?$/,
        use: ['html-loader'],
      },
    ],
  },
  plugins: [
    new HtmlPlugin({
      filename: 'index.html',
    }),
    new CompressionPlugin(),
  ],
  target: 'web',

With this config my js gets compressed but not my HTML why ??

@alexander-akait
Copy link
Member

@NitroBAY compression just create *.gz files and don't minify html content

@ilan-schemoul
Copy link

Yes I know but it creates a js file with its gz counterpart but the html file created by HtmlWebpackPlugin (shorten by HtmlPlugin here) is not having its gz version of it. I missing something because I'm new to webpack maybe but then what is it ?

@ilan-schemoul
Copy link

ilan-schemoul commented Mar 24, 2018

Even without htmlplugin but with file-loader/extract-loader :

module.exports = {
  entry: path.resolve(__dirname, 'client/src/index.js'),
  output: {
    path: path.resolve(__dirname, 'client/dist')
  },
  module: {
    rules: [{
      test: /\.html$/,
      loaders: [{
        loader: 'file-loader',
      },
        {
          loader: 'extract-loader',
        }],
    }],
  },
  plugins: [
    new CompressionPlugin(),
  ]
}
};

The output html is not gzipped is it normal ? How do I gzip html ?

@alexander-akait
Copy link
Member

@NitroBAY seems you have default threshold https://github.com/webpack-contrib/compression-webpack-plugin/blob/master/src/index.js#L87 (i.e. gzip is not recommend for this html), just change value

@ilan-schemoul
Copy link

ilan-schemoul commented Mar 24, 2018

@evilebottnawi the default is 0 bytes. My html is not 130 bytes so it's greater than 0. Even when I set to 1 or whatever value it doesn't change a thing. Ratio has been also set to 0.

                                Asset       Size  Chunks             Chunk Names
95e8d881b9d1f966529393f226969b18.html  130 bytes          [emitted]
                              main.js  690 bytes       0  [emitted]  main
                           main.js.gz  399 bytes          [emitted]
Entrypoint main = main.js
   [0] ./node_modules/html-loader!./client/src/index.html 105 bytes {0} [built]
   [1] ./client/src/index.js 88 bytes {0} [built]

@nerdmax
Copy link

nerdmax commented Sep 28, 2018

Any solutions to that? I just want the html-webpack-plugin to reference the .gz file

@nerdmax
Copy link

nerdmax commented Oct 2, 2018

I've resolved this issue by implementing this plugin: html-webpack-change-assets-extension-plugin.

Eg:

plugins: [
  new HtmlWebpackPlugin({
    jsExtension: ".gz"
  }),
  new CompressionPlugin(), // compression plugin will generate the xxx.js.gz file
  new HtmlWebpackChangeAssetsExtensionPlugin()
];

Please create an issue here if you encounter any issues related to that plguin.

@hedefalk
Copy link

hedefalk commented Oct 7, 2018

@nerdmax Thx! Using it and it works!

I read quite a few issues that was being closed, for instance jantimon/html-webpack-plugin#61

I just want to clarify my need. Using only

new HtmlWebpackPlugin({
      template: './src/index.html',
      jsExtension: '.gz',
    }),
    new CompressionPlugin({
      test: /\.js(\?.*)?$/i,
    }),

I get:

~/d/p/kb-frontend ❯❯❯ ll dist/marko.*                                                                                                                                                                master ✭ ✱
-rw-r--r--  1 viktor  staff   382K Oct  7 22:39 dist/marko.7299746349ba7d1314da.js
-rw-r--r--  1 viktor  staff   108K Oct  7 22:39 dist/marko.7299746349ba7d1314da.js.gz
-rw-r--r--  1 viktor  staff   1.8M Oct  7 22:39 dist/marko.7299746349ba7d1314da.js.map

But then the output html isn't using the gz:

/d/p/kb-frontend ❯❯❯ cat dist/index.html                                                                                                                                                            master ✭ ✱
<!doctype html>
<html>
       …
    <script type="text/javascript" src="marko.7299746349ba7d1314da.js"></script>
    …
</html>

no matter which order I use HtmlWebpackPlugin and CompressionPlugin.

But with:

    new HtmlWebpackPlugin({
      template: './src/index.html',
      jsExtension: '.gz',
    }),
    new CompressionPlugin({
      test: /\.js(\?.*)?$/i,
    }),
    new HtmlWebpackChangeAssetsExtensionPlugin(),

I have:

~/d/p/kb-frontend ❯❯❯ cat dist/index.html                                                                                                                                                            master ✭ ✱
<!doctype html>
<html>
       …
    <script type="text/javascript" src="marko.7299746349ba7d1314da.js.gz"></script>
    …
</html>

Anyway, HtmlWebpackChangeAssetsExtensionPlugin worked for my use case.

@pimvanderheijden
Copy link

Is it me or is this simply broken with webpack v5?

Running

node --trace-deprecation node_modules/webpack/bin/webpack.js --config ./webpack-configs/webpack.prod-client.js

I get

[webpack-cli] TypeError: Cannot read property 'tapAsync' of undefined                                                                                                                                              
    at ... /node_modules/html-webpack-change-assets-extension-plugin/dist/html-webpack-change-assets-extension-plugin.umd.js:25:36

Node 14
Webpack v5
html-webpack-change-assets-extension-plugin 1.2.0
html-webpack-plugin 5.3.2

In html-webpack-plugin jsExtension seems to be deprecated, it doesn't appear to do anything and it's not in the docs.
Other than I've nothing worth mentioning in my html-webpack-plugin definition.
Also changing the order of creating the plugins does not change a thing.

@pimvanderheijden
Copy link

This is my webpack config by the way:

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const HtmlWebpackChangeAssetsExtensionPlugin = require('html-webpack-change-assets-extension-plugin')

module.exports = {
    mode   : 'production',
    target : 'web',
    output: {
        filename      : '[name]-bundle.[fullhash].js',
        chunkFilename : '[name].js',
        path          : path.resolve(__dirname, '../build/'),
        publicPath    : '/'
    },
    plugins: [
        new HtmlWebpackPlugin({
            template           : 'index.pug',
            jsExtension        : '.gz',
        }),
        new CompressionPlugin({
            test: /\.(js|css|html|svg|png|gif|jpg|woff|woff2|eot|ttf|otf)$/
        }),
        new HtmlWebpackChangeAssetsExtensionPlugin()
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants