-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (71 loc) · 2.03 KB
/
index.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
/* eslint-env node */
'use strict';
const htmlTags = require('html-tags');
const diff = require('lodash.difference');
const merge = require('lodash.merge');
const compileCSS = require('broccoli-postcss');
const fetch = (key) => {
const { getItemSync } = process.emberAddonStateBucket;
return getItemSync(`ember-dom-inventory:${key}`) || [];
};
const removeableElements = () => {
const whitelistedTags = ['html', 'body'];
const removeableTags = diff(htmlTags, whitelistedTags);
const htmlElementsInApp = fetch('htmlElements');
return diff(
removeableTags,
htmlElementsInApp
);
};
const removeableClasses = () => {
const { getItemSync } = process.emberAddonStateBucket;
const cssClassesInApp = fetch('htmlClasses')
if (cssClassesInApp.length) {
const klasses = cssClassesInApp.join('|');
const regex = new RegExp(`^\\.(?!(${klasses})$).*`)
return regex;
}
const regexMatchingAllClasses = new RegExp(/^\..*$/);
return regexMatchingAllClasses;
};
const removeableIds = () => {
const idsInApp = fetch('htmlIds')
if (idsInApp.length) {
const ids = idsInApp.join('|');
const regex = new RegExp(`^#(?!(${ids})$).*`)
return regex;
}
const regexMatchingAllIds = new RegExp(/^#.*$/);
return regexMatchingAllIds;
};
module.exports = {
name: 'ember-tachyons-sweeper',
postprocessTree(type, tree) {
if (type === 'css') {
const selectorBlacklist = removeableElements();
const regexenBlacklist = [removeableClasses(), removeableIds()];
return compileCSS(tree, {
plugins: [
{
module: require('postcss-discard-comments'),
options: {
removeAll: true
},
},
{
module: require('postcss-strip-selectors'),
options: {
selectors: selectorBlacklist,
regexen: regexenBlacklist,
},
},
{
module: require('postcss-discard-empty'),
options: {},
}
]
});
}
return tree;
},
};