docusaurus/packages/docusaurus-cssnano-preset/src/remove-overridden-custom-properties/index.js
Joshua Chen aa446b7a9c
chore: clean up ESLint config, enable a few rules (#6514)
* chore: clean up ESLint config, enable a few rules

* enable max-len for comments

* fix build
2022-01-31 10:31:24 +08:00

48 lines
1.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* This PostCSS plugin will remove duplicate/same custom properties (which are
* actually overridden ones) **only** from `:root` selector.
*
* Depending on the presence of an `!important` rule in value of custom
* property, the following actions will happen:
*
* - If the same custom properties do **not** have an `!important` rule, then
* all of them will be removed except for the last one (which will actually be
* applied).
* - If the same custom properties have at least one `!important` rule, then
* only those properties that do not have this rule will be removed.
* @returns {import('postcss').Plugin}
*/
module.exports = function creator() {
return {
postcssPlugin: 'postcss-remove-overridden-custom-properties',
Declaration(decl) {
if (decl.parent.selector !== ':root') {
return;
}
const sameProperties = decl.parent.nodes.filter(
(n) => n.prop === decl.prop,
);
const hasImportantProperties = sameProperties.some((p) =>
Object.prototype.hasOwnProperty.call(p, 'important'),
);
const overriddenProperties = hasImportantProperties
? sameProperties.filter(
(p) => !Object.prototype.hasOwnProperty.call(p, 'important'),
)
: sameProperties.slice(0, -1);
overriddenProperties.map((p) => p.remove());
},
};
};
module.exports.postcss = true;