Skip to content

Commit

Permalink
feat: run optimize against assets added later by plugins for webpack@5
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Dec 10, 2020
1 parent 2afc01b commit 7ca675e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,25 @@ class CssMinimizerPlugin {
async optimize(compiler, compilation, assets, CacheEngine, weakCache) {
const assetNames = Object.keys(
typeof assets === 'undefined' ? compilation.assets : assets
).filter((assetName) =>
ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(assetName)
);
).filter((assetName) => {
if (
!ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
undefined,
this.options
)(assetName)
) {
return false;
}

const { info } = CssMinimizerPlugin.getAsset(compilation, assetName);

if (info.minimized) {
return false;
}

return true;
});

if (assetNames.length === 0) {
return Promise.resolve();
Expand Down Expand Up @@ -467,6 +479,7 @@ class CssMinimizerPlugin {
{
name: pluginName,
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true,
},
(assets) => this.optimize(compiler, compilation, assets, CacheEngine)
);
Expand Down
38 changes: 38 additions & 0 deletions test/CssMinimizerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
readAsset,
removeCache,
ModifyExistingAsset,
EmitNewAsset,
} from './helpers';

describe('CssMinimizerPlugin', () => {
Expand Down Expand Up @@ -1104,4 +1105,41 @@ describe('CssMinimizerPlugin', () => {
resolve();
});
});

if (!getCompiler.isWebpack4()) {
it('should run plugin against assets added later by plugins', async () => {
const compiler = getCompiler({
output: {
pathinfo: false,
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
entry: {
entry: `${__dirname}/fixtures/test/foo.css`,
},
module: {
rules: [
{
test: /.s?css$/i,
use: ['css-loader'],
},
],
},
});
new CssMinimizerPlugin({
minimizerOptions: {
preset: ['default', { discardEmpty: false }],
},
}).apply(compiler);

new EmitNewAsset({ name: 'newFile.css' }).apply(compiler);

const stats = await compile(compiler);

expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
}
});
14 changes: 13 additions & 1 deletion test/__snapshots__/CssMinimizerPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ exports[`CssMinimizerPlugin should respect the hash options #1: errors 1`] = `Ar

exports[`CssMinimizerPlugin should respect the hash options #1: warnings 1`] = `Array []`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: assets 1`] = `
Object {
"newFile.css": ".a{display:block;color:coral}",
}
`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: errors 1`] = `Array []`;

exports[`CssMinimizerPlugin should run plugin against assets added later by plugins: warnings 1`] = `Array []`;

exports[`CssMinimizerPlugin should throw error from postcss: error 1`] = `
Array [
"Error: foo.css from Css Minimizer
error-plugin: /foo.css:2:3: Postcss error [foo.css:2,3]",
"Error: foo.css from Css Minimizer
error-plugin: /foo.css:2:3: Postcss error [foo.css:2,3]",
]
`;
Expand Down Expand Up @@ -94,7 +106,7 @@ exports[`CssMinimizerPlugin should work and do not use memory cache when the "ca

exports[`CssMinimizerPlugin should work and generate real content hash: assets 1`] = `
Object {
"entry.19e4764f9c1d9fe130e2.2b805513ea22ef0b0e87.569cdce223d5e9f199ef.css": "body{color:red}a{color:#00f}",
"entry.19e4764f9c1d9fe130e2.4c83f86dfff978a8623b.b0f9ad463b6a0480ea34.css": "body{color:red}a{color:#00f}",
}
`;

Expand Down
32 changes: 32 additions & 0 deletions test/helpers/EmitNewAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default class EmitNewAsset {
constructor(options = {}) {
this.options = options;
}

apply(compiler) {
const pluginName = this.constructor.name;

const { RawSource } = compiler.webpack.sources;

compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: pluginName,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
() => {
// eslint-disable-next-line no-param-reassign
compilation.emitAsset(
this.options.name,
new RawSource(`
.a {
display: block;
color: coral;
}
`)
);
}
);
});
}
}
2 changes: 2 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getCompiler from './getCompiler';
import readAsset from './readAsset';
import readAssets from './readAssets';
import ModifyExistingAsset from './ModifyExistingAsset';
import EmitNewAsset from './EmitNewAsset';
import removeCache from './removeCache';
import getErrors from './getErrors';
import getWarnings from './getWarnings';
Expand All @@ -14,6 +15,7 @@ export {
readAsset,
readAssets,
ModifyExistingAsset,
EmitNewAsset,
removeCache,
getErrors,
getWarnings,
Expand Down

0 comments on commit 7ca675e

Please sign in to comment.