@docusaurus/cssnano-preset 2.0.0-beta.ff31de0ff → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +9 -0
- package/lib/index.js +23 -0
- package/lib/remove-overridden-custom-properties/index.d.ts +25 -0
- package/lib/remove-overridden-custom-properties/index.js +40 -0
- package/package.json +15 -7
- package/src/deps.d.ts +11 -0
- package/src/index.ts +29 -0
- package/src/remove-overridden-custom-properties/index.ts +51 -0
- package/index.js +0 -25
- package/src/remove-overridden-custom-properties/__tests__/__snapshots__/index.test.js.snap +0 -19
- package/src/remove-overridden-custom-properties/__tests__/fixtures/important_rule.css +0 -8
- package/src/remove-overridden-custom-properties/__tests__/fixtures/normal.css +0 -7
- package/src/remove-overridden-custom-properties/__tests__/index.test.js +0 -33
- package/src/remove-overridden-custom-properties/index.js +0 -42
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import advancedBasePreset from 'cssnano-preset-advanced';
|
|
8
|
+
declare const preset: typeof advancedBasePreset;
|
|
9
|
+
export = preset;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
const tslib_1 = require("tslib");
|
|
9
|
+
const cssnano_preset_advanced_1 = tslib_1.__importDefault(require("cssnano-preset-advanced"));
|
|
10
|
+
const postcss_sort_media_queries_1 = tslib_1.__importDefault(require("postcss-sort-media-queries"));
|
|
11
|
+
const remove_overridden_custom_properties_1 = tslib_1.__importDefault(require("./remove-overridden-custom-properties"));
|
|
12
|
+
const preset = function preset(opts) {
|
|
13
|
+
const advancedPreset = (0, cssnano_preset_advanced_1.default)({
|
|
14
|
+
autoprefixer: { add: false },
|
|
15
|
+
discardComments: { removeAll: true },
|
|
16
|
+
/* cSpell:ignore zindex */
|
|
17
|
+
zindex: false,
|
|
18
|
+
...opts,
|
|
19
|
+
});
|
|
20
|
+
advancedPreset.plugins.unshift([postcss_sort_media_queries_1.default, undefined], [remove_overridden_custom_properties_1.default, undefined]);
|
|
21
|
+
return advancedPreset;
|
|
22
|
+
};
|
|
23
|
+
module.exports = preset;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { Plugin } from 'postcss';
|
|
8
|
+
/**
|
|
9
|
+
* This PostCSS plugin will remove duplicate/same custom properties (which are
|
|
10
|
+
* actually overridden ones) **only** from `:root` selector.
|
|
11
|
+
*
|
|
12
|
+
* Depending on the presence of an `!important` rule in value of custom
|
|
13
|
+
* property, the following actions will happen:
|
|
14
|
+
*
|
|
15
|
+
* - If the same custom properties do **not** have an `!important` rule, then
|
|
16
|
+
* all of them will be removed except for the last one (which will actually be
|
|
17
|
+
* applied).
|
|
18
|
+
* - If the same custom properties have at least one `!important` rule, then
|
|
19
|
+
* only those properties that do not have this rule will be removed.
|
|
20
|
+
*/
|
|
21
|
+
declare function creator(): Plugin;
|
|
22
|
+
declare namespace creator {
|
|
23
|
+
var postcss: true;
|
|
24
|
+
}
|
|
25
|
+
export default creator;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const isRule = (node) => node?.type === 'rule';
|
|
10
|
+
/**
|
|
11
|
+
* This PostCSS plugin will remove duplicate/same custom properties (which are
|
|
12
|
+
* actually overridden ones) **only** from `:root` selector.
|
|
13
|
+
*
|
|
14
|
+
* Depending on the presence of an `!important` rule in value of custom
|
|
15
|
+
* property, the following actions will happen:
|
|
16
|
+
*
|
|
17
|
+
* - If the same custom properties do **not** have an `!important` rule, then
|
|
18
|
+
* all of them will be removed except for the last one (which will actually be
|
|
19
|
+
* applied).
|
|
20
|
+
* - If the same custom properties have at least one `!important` rule, then
|
|
21
|
+
* only those properties that do not have this rule will be removed.
|
|
22
|
+
*/
|
|
23
|
+
function creator() {
|
|
24
|
+
return {
|
|
25
|
+
postcssPlugin: 'postcss-remove-overridden-custom-properties',
|
|
26
|
+
Declaration(decl) {
|
|
27
|
+
if (!isRule(decl.parent) || decl.parent.selector !== ':root') {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const sameProperties = decl.parent.nodes.filter((n) => 'prop' in n && n.prop === decl.prop);
|
|
31
|
+
const hasImportantProperties = sameProperties.some((p) => 'important' in p);
|
|
32
|
+
const overriddenProperties = hasImportantProperties
|
|
33
|
+
? sameProperties.filter((p) => !('important' in p))
|
|
34
|
+
: sameProperties.slice(0, -1);
|
|
35
|
+
overriddenProperties.map((p) => p.remove());
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
creator.postcss = true;
|
|
40
|
+
exports.default = creator;
|
package/package.json
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/cssnano-preset",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Advanced cssnano preset for maximum optimization.",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"watch": "tsc --watch"
|
|
13
|
+
},
|
|
10
14
|
"repository": {
|
|
11
15
|
"type": "git",
|
|
12
16
|
"url": "https://github.com/facebook/docusaurus.git",
|
|
13
17
|
"directory": "packages/docusaurus-cssnano-preset"
|
|
14
18
|
},
|
|
15
19
|
"dependencies": {
|
|
16
|
-
"cssnano-preset-advanced": "^5.
|
|
17
|
-
"postcss": "^8.
|
|
18
|
-
"postcss-sort-media-queries": "^
|
|
20
|
+
"cssnano-preset-advanced": "^5.3.8",
|
|
21
|
+
"postcss": "^8.4.14",
|
|
22
|
+
"postcss-sort-media-queries": "^4.2.1",
|
|
23
|
+
"tslib": "^2.4.0"
|
|
19
24
|
},
|
|
20
25
|
"devDependencies": {
|
|
21
|
-
"to-vfile": "^6.
|
|
26
|
+
"to-vfile": "^6.1.0"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16.14"
|
|
22
30
|
},
|
|
23
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "1ddee1c29cabf9bb52e4d78af6ebfaaabb1bc1f9"
|
|
24
32
|
}
|
package/src/deps.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare module 'postcss-sort-media-queries' {
|
|
9
|
+
const plugin: import('postcss').PluginCreator<object>;
|
|
10
|
+
export default plugin;
|
|
11
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import advancedBasePreset from 'cssnano-preset-advanced';
|
|
9
|
+
import postCssSortMediaQueries from 'postcss-sort-media-queries';
|
|
10
|
+
import postCssRemoveOverriddenCustomProperties from './remove-overridden-custom-properties';
|
|
11
|
+
|
|
12
|
+
const preset: typeof advancedBasePreset = function preset(opts) {
|
|
13
|
+
const advancedPreset = advancedBasePreset({
|
|
14
|
+
autoprefixer: {add: false},
|
|
15
|
+
discardComments: {removeAll: true},
|
|
16
|
+
/* cSpell:ignore zindex */
|
|
17
|
+
zindex: false,
|
|
18
|
+
...opts,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
advancedPreset.plugins.unshift(
|
|
22
|
+
[postCssSortMediaQueries, undefined],
|
|
23
|
+
[postCssRemoveOverriddenCustomProperties, undefined],
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
return advancedPreset;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export = preset;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {Plugin, Rule, Node} from 'postcss';
|
|
9
|
+
|
|
10
|
+
const isRule = (node: Node | undefined): node is Rule => node?.type === 'rule';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This PostCSS plugin will remove duplicate/same custom properties (which are
|
|
14
|
+
* actually overridden ones) **only** from `:root` selector.
|
|
15
|
+
*
|
|
16
|
+
* Depending on the presence of an `!important` rule in value of custom
|
|
17
|
+
* property, the following actions will happen:
|
|
18
|
+
*
|
|
19
|
+
* - If the same custom properties do **not** have an `!important` rule, then
|
|
20
|
+
* all of them will be removed except for the last one (which will actually be
|
|
21
|
+
* applied).
|
|
22
|
+
* - If the same custom properties have at least one `!important` rule, then
|
|
23
|
+
* only those properties that do not have this rule will be removed.
|
|
24
|
+
*/
|
|
25
|
+
function creator(): Plugin {
|
|
26
|
+
return {
|
|
27
|
+
postcssPlugin: 'postcss-remove-overridden-custom-properties',
|
|
28
|
+
Declaration(decl) {
|
|
29
|
+
if (!isRule(decl.parent) || decl.parent.selector !== ':root') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const sameProperties = decl.parent.nodes.filter(
|
|
34
|
+
(n) => 'prop' in n && n.prop === decl.prop,
|
|
35
|
+
);
|
|
36
|
+
const hasImportantProperties = sameProperties.some(
|
|
37
|
+
(p) => 'important' in p,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const overriddenProperties = hasImportantProperties
|
|
41
|
+
? sameProperties.filter((p) => !('important' in p))
|
|
42
|
+
: sameProperties.slice(0, -1);
|
|
43
|
+
|
|
44
|
+
overriddenProperties.map((p) => p.remove());
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
creator.postcss = true as const;
|
|
50
|
+
|
|
51
|
+
export default creator;
|
package/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const advancedBasePreset = require('cssnano-preset-advanced');
|
|
9
|
-
const postCssSortMediaQueries = require('postcss-sort-media-queries');
|
|
10
|
-
const postCssRemoveOverriddenCustomProperties = require('./src/remove-overridden-custom-properties');
|
|
11
|
-
|
|
12
|
-
module.exports = function docusaurusCssnanoPreset(opts) {
|
|
13
|
-
const advancedPreset = advancedBasePreset({
|
|
14
|
-
autoprefixer: {add: false},
|
|
15
|
-
discardComments: {removeAll: true},
|
|
16
|
-
...opts,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
advancedPreset.plugins.unshift(
|
|
20
|
-
[postCssSortMediaQueries],
|
|
21
|
-
[postCssRemoveOverriddenCustomProperties],
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
return advancedPreset;
|
|
25
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`remove-overridden-custom-properties overridden custom properties should be removed 1`] = `
|
|
4
|
-
":root {
|
|
5
|
-
--color-secondary: green;
|
|
6
|
-
--color-primary: blue;
|
|
7
|
-
--color-header: gray;
|
|
8
|
-
}
|
|
9
|
-
"
|
|
10
|
-
`;
|
|
11
|
-
|
|
12
|
-
exports[`remove-overridden-custom-properties overridden custom properties with \`!important\` rule should not be removed 1`] = `
|
|
13
|
-
":root {
|
|
14
|
-
--color-primary: blue;
|
|
15
|
-
--color-header: gray !important;
|
|
16
|
-
--color-secondary: yellow !important;
|
|
17
|
-
}
|
|
18
|
-
"
|
|
19
|
-
`;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const vfile = require('to-vfile');
|
|
10
|
-
const postcss = require('postcss');
|
|
11
|
-
const postCssRemoveOverriddenCustomProperties = require('../index');
|
|
12
|
-
|
|
13
|
-
const processFixture = (name) => {
|
|
14
|
-
const input = vfile.readSync(
|
|
15
|
-
path.join(__dirname, 'fixtures', `${name}.css`),
|
|
16
|
-
'utf8',
|
|
17
|
-
);
|
|
18
|
-
const output = postcss([postCssRemoveOverriddenCustomProperties]).process(
|
|
19
|
-
input,
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
return output.css;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
describe('remove-overridden-custom-properties', () => {
|
|
26
|
-
test('overridden custom properties should be removed', () => {
|
|
27
|
-
expect(processFixture('normal')).toMatchSnapshot();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('overridden custom properties with `!important` rule should not be removed', () => {
|
|
31
|
-
expect(processFixture('important_rule')).toMatchSnapshot();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* This PostCSS plugin will remove duplicate/same custom properties (which are actually overridden ones) **only** from `:root` selector.
|
|
10
|
-
*
|
|
11
|
-
* Depending on the presence of an `!important` rule in value of custom property, the following actions will happens:
|
|
12
|
-
*
|
|
13
|
-
* - 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).
|
|
14
|
-
* - If the same custom properties have at least one `!important` rule, then only those properties that do not have this rule will be removed.
|
|
15
|
-
* @returns {import('postcss').Plugin}
|
|
16
|
-
*/
|
|
17
|
-
module.exports = function creator() {
|
|
18
|
-
return {
|
|
19
|
-
postcssPlugin: 'postcss-remove-overridden-custom-properties',
|
|
20
|
-
Declaration(decl) {
|
|
21
|
-
if (decl.parent.selector !== ':root') {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const sameProperties =
|
|
26
|
-
decl.parent.nodes.filter((n) => n.prop === decl.prop) || [];
|
|
27
|
-
const hasImportantProperties = sameProperties.some((p) =>
|
|
28
|
-
Object.prototype.hasOwnProperty.call(p, 'important'),
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const overriddenProperties = hasImportantProperties
|
|
32
|
-
? sameProperties.filter(
|
|
33
|
-
(p) => !Object.prototype.hasOwnProperty.call(p, 'important'),
|
|
34
|
-
)
|
|
35
|
-
: sameProperties.slice(0, -1);
|
|
36
|
-
|
|
37
|
-
overriddenProperties.map((p) => p.remove());
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
module.exports.postcss = true;
|