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

feat: run optimize against assets added later by plugins for webpack@5 #47

Merged
merged 2 commits into from
Jan 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: run optimize against assets added later by plugins for webpack@5
cap-Bernardito authored and alexander-akait committed Jan 8, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 992be65a00d57b144263c33ecc7d093a8fdc972c
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
@@ -463,6 +475,7 @@ class CssMinimizerPlugin {
{
name: pluginName,
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true,
},
(assets) => this.optimize(compiler, compilation, assets, CacheEngine)
);
38 changes: 38 additions & 0 deletions test/CssMinimizerPlugin.test.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import {
readAsset,
removeCache,
ModifyExistingAsset,
EmitNewAsset,
} from './helpers';

describe('CssMinimizerPlugin', () => {
@@ -1102,4 +1103,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');
});
}
});
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
@@ -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';
@@ -14,6 +15,7 @@ export {
readAsset,
readAssets,
ModifyExistingAsset,
EmitNewAsset,
removeCache,
getErrors,
getWarnings,