This repository was archived by the owner on Feb 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpostcss-purgecss.js
104 lines (81 loc) · 3.41 KB
/
postcss-purgecss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var path = _interopDefault(require('path'));
var postcss = _interopDefault(require('postcss'));
var Purgecss = _interopDefault(require('purgecss'));
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
var CONFIG_FILENAME = 'purgecss.config.js';
var ERROR_CONFIG_FILE_LOADING = 'Error loading the config file';
var loadConfigFile = function loadConfigFile(configFile) {
var pathConfig = typeof configFile === 'undefined' ? CONFIG_FILENAME : configFile;
var options;
try {
var t = path.resolve(process.cwd(), pathConfig);
options = require(t);
} catch (e) {
throw new Error(ERROR_CONFIG_FILE_LOADING + e.message);
}
return options;
};
var index = postcss.plugin('postcss-plugin-purgecss', function (opts) {
return function (root, result) {
if (typeof opts === 'string' || typeof opts === 'undefined') opts = loadConfigFile(opts);
if (!opts.css || !opts.css.length) {
opts.css = ['__postcss_purgecss_placeholder'];
}
var purgecss = new Purgecss(opts);
purgecss.root = root; // Get selectors from content files
var _purgecss$options = purgecss.options,
content = _purgecss$options.content,
extractors = _purgecss$options.extractors;
var fileFormatContents = content.filter(function (o) {
return typeof o === 'string';
});
var rawFormatContents = content.filter(function (o) {
return _typeof(o) === 'object';
});
var cssFileSelectors = purgecss.extractFileSelector(fileFormatContents, extractors);
var cssRawSelectors = purgecss.extractRawSelector(rawFormatContents, extractors); // Get css selectors and remove unused ones
var cssSelectors = new Set([].concat(_toConsumableArray(cssFileSelectors), _toConsumableArray(cssRawSelectors))); // purge selectors
purgecss.getSelectorsCss(cssSelectors); // purge keyframes
if (purgecss.options.keyframes) purgecss.removeUnusedKeyframes(); // purge font face
if (purgecss.options.fontFace) purgecss.removeUnusedFontFaces();
if (purgecss.options.rejected && purgecss.selectorsRemoved.size > 0) {
result.messages.push({
type: 'purgecss',
plugin: 'postcss-purgecss',
text: "purging ".concat(purgecss.selectorsRemoved.size, " selectors:\n ").concat(Array.from(purgecss.selectorsRemoved).map(function (selector) {
return selector.trim();
}).join('\n '))
});
purgecss.selectorsRemoved.clear();
}
};
});
module.exports = index;