sndyuk/mangle-css-class-webpack-plugin

Webpack 5 Support

jsbrain opened this issue ยท 4 comments

I just upgraded my Gatsby Project to v3 which uses Webpack 5 now and now my build fails with:

failed Building production JavaScript and CSS bundles - 5.424s

ERROR #98123  WEBPACK

Generating JavaScript bundles failed

this._source.node is not a function

error Command failed with exit code 1.

I tried to set up a whole new fresh project with just nothing extra installed and it still happens so I am pretty sure it has to do with Webpack. Any idea what might be the problem?

Right, I just realize right at the start of the build I get the error:

 ERROR 

(node:37933) [DEP_WEBPACK_COMPILATION_OPTIMIZE_CHUNK_ASSETS]
DeprecationWarning: optimizeChunkAssets is deprecated (use
Compilation.hooks.processAssets instead and use one of
Compilation.PROCESS_ASSETS_STAGE_* as stage option)
(Use `node --trace-deprecation ...` to show where the warning was
 created)

which I now also realize is an error and not just a warning. Apparently all deprecated features have been removed so I guess it's pretty safe to say that the error is right here ๐Ÿ˜€

Xaz16 commented

which I now also realize is an error

No it is just a warning and it is not a big problem
I created a fork, which remove warning by using new webpack 5 compilation hook (still tapAsync) and fix the issue. When, in my case angular application with custom webpack builder where I use this plugin, throws me "this._source.node is not a function"

As I understand that was an issue to use it like this:
compilation.assets[file] = source;
https://github.com/sndyuk/mangle-css-class-webpack-plugin/blob/master/lib/optimizer.js#L81

And it should be used like that:
compilation.updateAsset(file, source, { minimized: true });
https://github.com/Xaz16/mangle-css-class-webpack-plugin/blob/master/lib/optimizer.js#L82

My fix looks like resolve strange issue like that I mention above, but it drops support of webpack less than 5 version.
Because new (not deprecated) hook have another signature -- now it is just asset but not chunk with files...
@sndyuk I can make PR, but it will be good if someone can rewrite it in way to support all versions

@Xaz16 thank you for the clarification, I'll try out your fork!

But I don't think there's an easy way to support all versions in the future, Webpack v5 is a breaking change on all libraries I know so far so I guess the best thing to do is to update the major version on this lib and then simply document:

... for Webpack v4 & v3 support install lib before 5.x.x ...

I think that's the way other libs are handling it as well.

@Xaz16 Thank you for the contribution! It looks good to me. The new version 5.0.5 has been published.

@jsbrain Agreed. I've updated the README. Thanks!