@fullhuman/purgecss-from-jsx 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/purgecss-from-jsx.esm.js +77 -1
- package/lib/purgecss-from-jsx.js +99 -1
- package/package.json +3 -2
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,77 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import * as acorn from 'acorn';
|
|
2
|
+
import jsx from 'acorn-jsx';
|
|
3
|
+
import { extend } from 'acorn-jsx-walk';
|
|
4
|
+
import * as walk from 'acorn-walk';
|
|
5
|
+
|
|
6
|
+
extend(walk.base);
|
|
7
|
+
/**
|
|
8
|
+
* Create function to extract selectors from jsx code
|
|
9
|
+
*
|
|
10
|
+
* @param options - acorn options
|
|
11
|
+
* @returns function to extract selectors from jsx code
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
function purgeFromJsx(options) {
|
|
16
|
+
return (content) => {
|
|
17
|
+
// Will be filled during walk
|
|
18
|
+
const state = { selectors: [] };
|
|
19
|
+
// Parse and walk any JSXElement
|
|
20
|
+
walk.recursive(acorn.Parser.extend(jsx()).parse(content, options || { ecmaVersion: "latest" }), state, {
|
|
21
|
+
JSXOpeningElement(acornNode, state, callback) {
|
|
22
|
+
var _a;
|
|
23
|
+
const node = acornNode;
|
|
24
|
+
const nameState = {};
|
|
25
|
+
callback(node.name, nameState);
|
|
26
|
+
if (nameState.text) {
|
|
27
|
+
(_a = state.selectors) === null || _a === void 0 ? void 0 : _a.push(nameState.text);
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < node.attributes.length; ++i) {
|
|
30
|
+
callback(node.attributes[i], state);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
JSXAttribute(acornNode, state, callback) {
|
|
34
|
+
var _a;
|
|
35
|
+
const node = acornNode;
|
|
36
|
+
if (!node.value) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const nameState = {};
|
|
40
|
+
callback(node.name, nameState);
|
|
41
|
+
// node.name is id or className
|
|
42
|
+
switch (nameState.text) {
|
|
43
|
+
case "id":
|
|
44
|
+
case "className":
|
|
45
|
+
{
|
|
46
|
+
// Get text in node.value
|
|
47
|
+
const valueState = {};
|
|
48
|
+
callback(node.value, valueState);
|
|
49
|
+
// node.value is not empty
|
|
50
|
+
if (valueState.text) {
|
|
51
|
+
(_a = state.selectors) === null || _a === void 0 ? void 0 : _a.push(...valueState.text.split(" "));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
JSXIdentifier(acornNode, state) {
|
|
58
|
+
const node = acornNode;
|
|
59
|
+
state.text = node.name;
|
|
60
|
+
},
|
|
61
|
+
JSXNamespacedName(acornNode, state) {
|
|
62
|
+
const node = acornNode;
|
|
63
|
+
state.text = node.namespace.name + ":" + node.name.name;
|
|
64
|
+
},
|
|
65
|
+
// Only handle Literal for now, not JSXExpressionContainer | JSXElement
|
|
66
|
+
Literal(acornNode, state) {
|
|
67
|
+
const node = acornNode;
|
|
68
|
+
if (typeof node.value === "string") {
|
|
69
|
+
state.text = node.value;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
}, { ...walk.base });
|
|
73
|
+
return state.selectors || [];
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { purgeFromJsx as default };
|
package/lib/purgecss-from-jsx.js
CHANGED
|
@@ -1 +1,99 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var acorn = require('acorn');
|
|
4
|
+
var jsx = require('acorn-jsx');
|
|
5
|
+
var acornJsxWalk = require('acorn-jsx-walk');
|
|
6
|
+
var walk = require('acorn-walk');
|
|
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 acorn__namespace = /*#__PURE__*/_interopNamespaceDefault(acorn);
|
|
26
|
+
var walk__namespace = /*#__PURE__*/_interopNamespaceDefault(walk);
|
|
27
|
+
|
|
28
|
+
acornJsxWalk.extend(walk__namespace.base);
|
|
29
|
+
/**
|
|
30
|
+
* Create function to extract selectors from jsx code
|
|
31
|
+
*
|
|
32
|
+
* @param options - acorn options
|
|
33
|
+
* @returns function to extract selectors from jsx code
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
function purgeFromJsx(options) {
|
|
38
|
+
return (content) => {
|
|
39
|
+
// Will be filled during walk
|
|
40
|
+
const state = { selectors: [] };
|
|
41
|
+
// Parse and walk any JSXElement
|
|
42
|
+
walk__namespace.recursive(acorn__namespace.Parser.extend(jsx()).parse(content, options || { ecmaVersion: "latest" }), state, {
|
|
43
|
+
JSXOpeningElement(acornNode, state, callback) {
|
|
44
|
+
var _a;
|
|
45
|
+
const node = acornNode;
|
|
46
|
+
const nameState = {};
|
|
47
|
+
callback(node.name, nameState);
|
|
48
|
+
if (nameState.text) {
|
|
49
|
+
(_a = state.selectors) === null || _a === void 0 ? void 0 : _a.push(nameState.text);
|
|
50
|
+
}
|
|
51
|
+
for (let i = 0; i < node.attributes.length; ++i) {
|
|
52
|
+
callback(node.attributes[i], state);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
JSXAttribute(acornNode, state, callback) {
|
|
56
|
+
var _a;
|
|
57
|
+
const node = acornNode;
|
|
58
|
+
if (!node.value) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const nameState = {};
|
|
62
|
+
callback(node.name, nameState);
|
|
63
|
+
// node.name is id or className
|
|
64
|
+
switch (nameState.text) {
|
|
65
|
+
case "id":
|
|
66
|
+
case "className":
|
|
67
|
+
{
|
|
68
|
+
// Get text in node.value
|
|
69
|
+
const valueState = {};
|
|
70
|
+
callback(node.value, valueState);
|
|
71
|
+
// node.value is not empty
|
|
72
|
+
if (valueState.text) {
|
|
73
|
+
(_a = state.selectors) === null || _a === void 0 ? void 0 : _a.push(...valueState.text.split(" "));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
JSXIdentifier(acornNode, state) {
|
|
80
|
+
const node = acornNode;
|
|
81
|
+
state.text = node.name;
|
|
82
|
+
},
|
|
83
|
+
JSXNamespacedName(acornNode, state) {
|
|
84
|
+
const node = acornNode;
|
|
85
|
+
state.text = node.namespace.name + ":" + node.name.name;
|
|
86
|
+
},
|
|
87
|
+
// Only handle Literal for now, not JSXExpressionContainer | JSXElement
|
|
88
|
+
Literal(acornNode, state) {
|
|
89
|
+
const node = acornNode;
|
|
90
|
+
if (typeof node.value === "string") {
|
|
91
|
+
state.text = node.value;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
}, { ...walk__namespace.base });
|
|
95
|
+
return state.selectors || [];
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = purgeFromJsx;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fullhuman/purgecss-from-jsx",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"description": "JSX extractor for PurgeCSS",
|
|
5
5
|
"author": "Ffloriel",
|
|
6
6
|
"homepage": "https://github.com/FullHuman/purgecss#readme",
|
|
@@ -35,5 +35,6 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public",
|
|
37
37
|
"registry": "https://registry.npmjs.org/"
|
|
38
|
-
}
|
|
38
|
+
},
|
|
39
|
+
"gitHead": "e95398d961e03a809b5e3da75e3dae1813b46b68"
|
|
39
40
|
}
|