Skip to content

Instantly share code, notes, and snippets.

@denisx
Created April 27, 2020 12:35
Show Gist options
  • Save denisx/9dc0e8c542b810e67f41691c89e7b948 to your computer and use it in GitHub Desktop.
Save denisx/9dc0e8c542b810e67f41691c89e7b948 to your computer and use it in GitHub Desktop.
suggest-css-hash-len.js
/*
at webpack settings:
plugins: [
...plugins,
new SuggestCssHashLen({
instance: MyShortCssClasses,
selectedHashLen: cssHashLen
})
]
*/
class SuggestCssHashLen {
constructor({ instance, selectedHashLen }) {
this.instance = instance;
this.selectedHashLen = selectedHashLen;
}
apply(compiler) {
compiler.plugin('done', this.run);
}
run = () => {
const { instance, selectedHashLen } = this;
const data = instance.getStat();
const matchLen = {};
const base = {};
Object.entries(data).forEach(([_, { name, test }]) => {
for(let len = 1; len <= name.length; len += 1) {
base[len] = base[len] || {};
const hash = name.substr(0, len);
if (base[len][hash]) {
matchLen[len] = matchLen[len] || 0;
matchLen[len] += 1;
} else {
base[len][hash] = 1;
}
}
});
console.log();
console.log('Suggest Minify Plugin');
console.log('Matched length (len: number):', matchLen);
if (matchLen[selectedHashLen]) {
console.log(
`🚫 You can't use selected hash length (${selectedHashLen}). Increase the hash length.`
);
console.log();
process.exit(1);
} else {
console.log(`Selected hash length (${selectedHashLen}) is OK.`);
if (!matchLen[selectedHashLen - 1]) {
console.log(`🎉 You can decrease the hash length (${selectedHashLen} -> ${selectedHashLen - 1}).`);
}
console.log();
}
};
}
module.exports = SuggestCssHashLen;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment