postcss-discard-comments
Version:
Discard comments in your CSS files with PostCSS.
45 lines (37 loc) • 1.04 kB
JavaScript
import cssnanoUtils from 'cssnano-utils';
const { isImportantComment } = cssnanoUtils;
class CommentRemover {
/** @type {boolean} */
#hasFirst;
/** @param {import('../index.js').Options} options */
constructor(options) {
if (options.remove !== undefined && typeof options.remove !== 'function') {
throw new TypeError('The remove option must be a function or undefined.');
}
this.options = options;
this.#hasFirst = false;
}
/**
* @param {string} comment
* @return {boolean | undefined}
*/
canRemove(comment) {
const remove = this.options.remove;
if (remove) {
return remove(comment);
} else {
const isImportant = isImportantComment(comment);
if (!isImportant) {
return true;
}
if (this.options.removeAll || this.#hasFirst) {
return true;
} else if (this.options.removeAllButFirst && !this.#hasFirst) {
this.#hasFirst = true;
return false;
}
}
return false;
}
}
export default CommentRemover;