Skip to content

Commit 01462f6

Browse files
refactor: migrate on processAssets hook (#20)
1 parent cb038b9 commit 01462f6

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/Webpack5Cache.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import serialize from 'serialize-javascript';
2-
31
export default class Cache {
42
// eslint-disable-next-line no-unused-vars
53
constructor(compilation, ignored) {
@@ -13,8 +11,8 @@ export default class Cache {
1311

1412
async get(task) {
1513
// eslint-disable-next-line no-param-reassign
16-
task.cacheIdent =
17-
task.cacheIdent || `${task.name}|${serialize(task.cacheKeys)}`;
14+
task.cacheIdent = task.cacheIdent || `${task.name}`;
15+
1816
// eslint-disable-next-line no-param-reassign
1917
task.cacheETag =
2018
task.cacheETag || this.cache.getLazyHashedEtag(task.assetSource);

src/index.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,6 @@ class CssMinimizerPlugin {
339339
} else {
340340
// For webpack@5 cache
341341
task.assetSource = assetSource;
342-
343-
task.cacheKeys = {
344-
cssMinimizer: CssMinimizerPackageJson.version,
345-
// eslint-disable-next-line global-require
346-
'css-minimizer-webpack-plugin': require('../package.json').version,
347-
'css-minimizer-webpack-plugin-options': this.options,
348-
};
349342
}
350343

351344
yield task;
@@ -505,11 +498,11 @@ class CssMinimizerPlugin {
505498
return Promise.resolve();
506499
};
507500

508-
const plugin = { name: this.constructor.name };
501+
const pluginName = this.constructor.name;
509502

510-
compiler.hooks.compilation.tap(plugin, (compilation) => {
503+
compiler.hooks.compilation.tap(pluginName, (compilation) => {
511504
if (this.options.sourceMap) {
512-
compilation.hooks.buildModule.tap(plugin, (moduleArg) => {
505+
compilation.hooks.buildModule.tap(pluginName, (moduleArg) => {
513506
// to get detailed location info about errors
514507
// eslint-disable-next-line no-param-reassign
515508
moduleArg.useSourceMap = true;
@@ -518,16 +511,22 @@ class CssMinimizerPlugin {
518511

519512
if (CssMinimizerPlugin.isWebpack4()) {
520513
compilation.hooks.optimizeChunkAssets.tapPromise(
521-
plugin,
514+
pluginName,
522515
optimizeFn.bind(this, compilation)
523516
);
524517
} else {
525-
compilation.hooks.optimizeAssets.tapPromise(
526-
plugin,
518+
// eslint-disable-next-line global-require
519+
const Compilation = require('webpack/lib/Compilation');
520+
521+
compilation.hooks.processAssets.tapPromise(
522+
{
523+
name: pluginName,
524+
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
525+
},
527526
optimizeFn.bind(this, compilation)
528527
);
529528

530-
compilation.hooks.statsPrinter.tap(plugin, (stats) => {
529+
compilation.hooks.statsPrinter.tap(pluginName, (stats) => {
531530
stats.hooks.print
532531
.for('asset.info.minimized')
533532
.tap(

0 commit comments

Comments
 (0)