@fullhuman/postcss-purgecss 6.0.0 → 7.0.2
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/LICENSE +21 -0
- package/lib/postcss-purgecss.d.ts +1 -0
- package/lib/postcss-purgecss.esm.js +89 -1
- package/lib/postcss-purgecss.js +113 -1
- package/package.json +5 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Full Human
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1 +1,89 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { PurgeCSS, defaultOptions, standardizeSafelist, mergeExtractorSelectors } from 'purgecss';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* PostCSS Plugin for PurgeCSS
|
|
6
|
+
*
|
|
7
|
+
* Most bundlers and frameworks to build websites are using PostCSS.
|
|
8
|
+
* The easiest way to configure PurgeCSS is with its PostCSS plugin.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
const PLUGIN_NAME = "postcss-purgecss";
|
|
13
|
+
/**
|
|
14
|
+
* Execute PurgeCSS process on the postCSS root node
|
|
15
|
+
*
|
|
16
|
+
* @param opts - PurgeCSS options
|
|
17
|
+
* @param root - root node of postCSS
|
|
18
|
+
* @param helpers - postCSS helpers
|
|
19
|
+
*/
|
|
20
|
+
async function purgeCSS(opts, root, { result }) {
|
|
21
|
+
const purgeCSS = new PurgeCSS();
|
|
22
|
+
let configFileOptions;
|
|
23
|
+
try {
|
|
24
|
+
const t = path.resolve(process.cwd(), "purgecss.config.js");
|
|
25
|
+
configFileOptions = await import(t);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// no config file present
|
|
29
|
+
}
|
|
30
|
+
const options = {
|
|
31
|
+
...defaultOptions,
|
|
32
|
+
...configFileOptions,
|
|
33
|
+
...opts,
|
|
34
|
+
safelist: standardizeSafelist((opts === null || opts === void 0 ? void 0 : opts.safelist) || (configFileOptions === null || configFileOptions === void 0 ? void 0 : configFileOptions.safelist)),
|
|
35
|
+
};
|
|
36
|
+
if (opts && typeof opts.contentFunction === "function") {
|
|
37
|
+
options.content = opts.contentFunction((root.source && root.source.input.file) || "");
|
|
38
|
+
}
|
|
39
|
+
purgeCSS.options = options;
|
|
40
|
+
if (options.variables) {
|
|
41
|
+
purgeCSS.variablesStructure.safelist = options.safelist.variables || [];
|
|
42
|
+
}
|
|
43
|
+
const { content, extractors } = options;
|
|
44
|
+
const fileFormatContents = content.filter((o) => typeof o === "string");
|
|
45
|
+
const rawFormatContents = content.filter((o) => typeof o === "object");
|
|
46
|
+
const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles(fileFormatContents, extractors);
|
|
47
|
+
const cssRawSelectors = await purgeCSS.extractSelectorsFromString(rawFormatContents, extractors);
|
|
48
|
+
const selectors = mergeExtractorSelectors(cssFileSelectors, cssRawSelectors);
|
|
49
|
+
//purge unused selectors
|
|
50
|
+
purgeCSS.walkThroughCSS(root, selectors);
|
|
51
|
+
if (purgeCSS.options.fontFace)
|
|
52
|
+
purgeCSS.removeUnusedFontFaces();
|
|
53
|
+
if (purgeCSS.options.keyframes)
|
|
54
|
+
purgeCSS.removeUnusedKeyframes();
|
|
55
|
+
if (purgeCSS.options.variables)
|
|
56
|
+
purgeCSS.removeUnusedCSSVariables();
|
|
57
|
+
if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) {
|
|
58
|
+
result.messages.push({
|
|
59
|
+
type: "purgecss",
|
|
60
|
+
plugin: "postcss-purgecss",
|
|
61
|
+
text: `purging ${purgeCSS.selectorsRemoved.size} selectors:
|
|
62
|
+
${Array.from(purgeCSS.selectorsRemoved)
|
|
63
|
+
.map((selector) => selector.trim())
|
|
64
|
+
.join("\n ")}`,
|
|
65
|
+
});
|
|
66
|
+
purgeCSS.selectorsRemoved.clear();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* PostCSS Plugin for PurgeCSS
|
|
71
|
+
*
|
|
72
|
+
* @param opts - PurgeCSS Options
|
|
73
|
+
* @returns the postCSS plugin
|
|
74
|
+
*
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
const purgeCSSPlugin = function (opts) {
|
|
78
|
+
if (typeof opts === "undefined")
|
|
79
|
+
throw new Error("PurgeCSS plugin does not have the correct options");
|
|
80
|
+
return {
|
|
81
|
+
postcssPlugin: PLUGIN_NAME,
|
|
82
|
+
OnceExit(root, helpers) {
|
|
83
|
+
return purgeCSS(opts, root, helpers);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
purgeCSSPlugin.postcss = true;
|
|
88
|
+
|
|
89
|
+
export { purgeCSSPlugin as default, purgeCSSPlugin };
|
package/lib/postcss-purgecss.js
CHANGED
|
@@ -1 +1,113 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var purgecss = require('purgecss');
|
|
7
|
+
|
|
8
|
+
function _interopNamespaceDefault(e) {
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* PostCSS Plugin for PurgeCSS
|
|
29
|
+
*
|
|
30
|
+
* Most bundlers and frameworks to build websites are using PostCSS.
|
|
31
|
+
* The easiest way to configure PurgeCSS is with its PostCSS plugin.
|
|
32
|
+
*
|
|
33
|
+
* @packageDocumentation
|
|
34
|
+
*/
|
|
35
|
+
const PLUGIN_NAME = "postcss-purgecss";
|
|
36
|
+
/**
|
|
37
|
+
* Execute PurgeCSS process on the postCSS root node
|
|
38
|
+
*
|
|
39
|
+
* @param opts - PurgeCSS options
|
|
40
|
+
* @param root - root node of postCSS
|
|
41
|
+
* @param helpers - postCSS helpers
|
|
42
|
+
*/
|
|
43
|
+
async function purgeCSS(opts, root, { result }) {
|
|
44
|
+
const purgeCSS = new purgecss.PurgeCSS();
|
|
45
|
+
let configFileOptions;
|
|
46
|
+
try {
|
|
47
|
+
const t = path__namespace.resolve(process.cwd(), "purgecss.config.js");
|
|
48
|
+
configFileOptions = await import(t);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// no config file present
|
|
52
|
+
}
|
|
53
|
+
const options = {
|
|
54
|
+
...purgecss.defaultOptions,
|
|
55
|
+
...configFileOptions,
|
|
56
|
+
...opts,
|
|
57
|
+
safelist: purgecss.standardizeSafelist((opts === null || opts === void 0 ? void 0 : opts.safelist) || (configFileOptions === null || configFileOptions === void 0 ? void 0 : configFileOptions.safelist)),
|
|
58
|
+
};
|
|
59
|
+
if (opts && typeof opts.contentFunction === "function") {
|
|
60
|
+
options.content = opts.contentFunction((root.source && root.source.input.file) || "");
|
|
61
|
+
}
|
|
62
|
+
purgeCSS.options = options;
|
|
63
|
+
if (options.variables) {
|
|
64
|
+
purgeCSS.variablesStructure.safelist = options.safelist.variables || [];
|
|
65
|
+
}
|
|
66
|
+
const { content, extractors } = options;
|
|
67
|
+
const fileFormatContents = content.filter((o) => typeof o === "string");
|
|
68
|
+
const rawFormatContents = content.filter((o) => typeof o === "object");
|
|
69
|
+
const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles(fileFormatContents, extractors);
|
|
70
|
+
const cssRawSelectors = await purgeCSS.extractSelectorsFromString(rawFormatContents, extractors);
|
|
71
|
+
const selectors = purgecss.mergeExtractorSelectors(cssFileSelectors, cssRawSelectors);
|
|
72
|
+
//purge unused selectors
|
|
73
|
+
purgeCSS.walkThroughCSS(root, selectors);
|
|
74
|
+
if (purgeCSS.options.fontFace)
|
|
75
|
+
purgeCSS.removeUnusedFontFaces();
|
|
76
|
+
if (purgeCSS.options.keyframes)
|
|
77
|
+
purgeCSS.removeUnusedKeyframes();
|
|
78
|
+
if (purgeCSS.options.variables)
|
|
79
|
+
purgeCSS.removeUnusedCSSVariables();
|
|
80
|
+
if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) {
|
|
81
|
+
result.messages.push({
|
|
82
|
+
type: "purgecss",
|
|
83
|
+
plugin: "postcss-purgecss",
|
|
84
|
+
text: `purging ${purgeCSS.selectorsRemoved.size} selectors:
|
|
85
|
+
${Array.from(purgeCSS.selectorsRemoved)
|
|
86
|
+
.map((selector) => selector.trim())
|
|
87
|
+
.join("\n ")}`,
|
|
88
|
+
});
|
|
89
|
+
purgeCSS.selectorsRemoved.clear();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* PostCSS Plugin for PurgeCSS
|
|
94
|
+
*
|
|
95
|
+
* @param opts - PurgeCSS Options
|
|
96
|
+
* @returns the postCSS plugin
|
|
97
|
+
*
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
const purgeCSSPlugin = function (opts) {
|
|
101
|
+
if (typeof opts === "undefined")
|
|
102
|
+
throw new Error("PurgeCSS plugin does not have the correct options");
|
|
103
|
+
return {
|
|
104
|
+
postcssPlugin: PLUGIN_NAME,
|
|
105
|
+
OnceExit(root, helpers) {
|
|
106
|
+
return purgeCSS(opts, root, helpers);
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
purgeCSSPlugin.postcss = true;
|
|
111
|
+
|
|
112
|
+
exports.default = purgeCSSPlugin;
|
|
113
|
+
exports.purgeCSSPlugin = purgeCSSPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fullhuman/postcss-purgecss",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"description": "PostCSS plugin for PurgeCSS",
|
|
5
5
|
"author": "FoundrySH <no-reply@foundry.sh>",
|
|
6
6
|
"homepage": "https://purgecss.com",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"url": "https://github.com/FullHuman/purgecss/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"purgecss": "^
|
|
30
|
+
"purgecss": "^7.0.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"postcss": "^8.4.
|
|
33
|
+
"postcss": "^8.4.47"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"postcss": "^8.0.0"
|
|
@@ -38,5 +38,6 @@
|
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public",
|
|
40
40
|
"registry": "https://registry.npmjs.org/"
|
|
41
|
-
}
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "e95398d961e03a809b5e3da75e3dae1813b46b68"
|
|
42
43
|
}
|