mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 14:06:59 +02:00
refactor(cssnano-preset): migrate to TS (#7440)
* refactor(cssnano-preset): migrate to TS * fix
This commit is contained in:
parent
c8b5f230ab
commit
71b5901bcd
9 changed files with 76 additions and 43 deletions
11
packages/docusaurus-cssnano-preset/src/deps.d.ts
vendored
Normal file
11
packages/docusaurus-cssnano-preset/src/deps.d.ts
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare module 'postcss-sort-media-queries' {
|
||||
const plugin: import('postcss').PluginCreator<object>;
|
||||
export default plugin;
|
||||
}
|
27
packages/docusaurus-cssnano-preset/src/index.ts
Normal file
27
packages/docusaurus-cssnano-preset/src/index.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import advancedBasePreset from 'cssnano-preset-advanced';
|
||||
import postCssSortMediaQueries from 'postcss-sort-media-queries';
|
||||
import postCssRemoveOverriddenCustomProperties from './remove-overridden-custom-properties';
|
||||
|
||||
const preset: typeof advancedBasePreset = function preset(opts) {
|
||||
const advancedPreset = advancedBasePreset({
|
||||
autoprefixer: {add: false},
|
||||
discardComments: {removeAll: true},
|
||||
...opts,
|
||||
});
|
||||
|
||||
advancedPreset.plugins.unshift(
|
||||
[postCssSortMediaQueries, undefined],
|
||||
[postCssRemoveOverriddenCustomProperties, undefined],
|
||||
);
|
||||
|
||||
return advancedPreset;
|
||||
};
|
||||
|
||||
export = preset;
|
|
@ -5,12 +5,12 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const vfile = require('to-vfile');
|
||||
const postcss = require('postcss');
|
||||
const postCssRemoveOverriddenCustomProperties = require('../index');
|
||||
import path from 'path';
|
||||
import vfile from 'to-vfile';
|
||||
import postcss from 'postcss';
|
||||
import postCssRemoveOverriddenCustomProperties from '../index';
|
||||
|
||||
const processFixture = (name) => {
|
||||
const processFixture = (name: string) => {
|
||||
const input = vfile.readSync(
|
||||
path.join(__dirname, '__fixtures__', `${name}.css`),
|
||||
'utf8',
|
|
@ -5,6 +5,10 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import type {Plugin, Rule, Node} from 'postcss';
|
||||
|
||||
const isRule = (node: Node | undefined): node is Rule => node?.type === 'rule';
|
||||
|
||||
/**
|
||||
* This PostCSS plugin will remove duplicate/same custom properties (which are
|
||||
* actually overridden ones) **only** from `:root` selector.
|
||||
|
@ -17,32 +21,31 @@
|
|||
* 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() {
|
||||
function creator(): Plugin {
|
||||
return {
|
||||
postcssPlugin: 'postcss-remove-overridden-custom-properties',
|
||||
Declaration(decl) {
|
||||
if (decl.parent.selector !== ':root') {
|
||||
if (!isRule(decl.parent) || decl.parent.selector !== ':root') {
|
||||
return;
|
||||
}
|
||||
|
||||
const sameProperties = decl.parent.nodes.filter(
|
||||
(n) => n.prop === decl.prop,
|
||||
(n) => 'prop' in n && n.prop === decl.prop,
|
||||
);
|
||||
const hasImportantProperties = sameProperties.some((p) =>
|
||||
Object.prototype.hasOwnProperty.call(p, 'important'),
|
||||
const hasImportantProperties = sameProperties.some(
|
||||
(p) => 'important' in p,
|
||||
);
|
||||
|
||||
const overriddenProperties = hasImportantProperties
|
||||
? sameProperties.filter(
|
||||
(p) => !Object.prototype.hasOwnProperty.call(p, 'important'),
|
||||
)
|
||||
? sameProperties.filter((p) => !('important' in p))
|
||||
: sameProperties.slice(0, -1);
|
||||
|
||||
overriddenProperties.map((p) => p.remove());
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.postcss = true;
|
||||
creator.postcss = true as const;
|
||||
|
||||
export default creator;
|
Loading…
Add table
Add a link
Reference in a new issue