purgecss-from-pug 5.0.0 → 7.0.1-alpha.0

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 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.
package/README.md CHANGED
@@ -1,11 +1,22 @@
1
1
  # `purgecss-from-pug`
2
2
 
3
- > TODO: description
3
+ A PurgeCSS extractor for PUG files that automatically generates a list of used CSS selectors from your PUG files. This extractor can be used with PurgeCSS to eliminate unused CSS and reduce the size of your production builds.
4
4
 
5
5
  ## Usage
6
6
 
7
7
  ```
8
- const purgecssFromPug = require('purgecss-from-pug');
8
+ import { PurgeCSS } from 'purgecss'
9
+ import { purgeCSSFromPug } from 'purgecss-from-pug'
10
+ const options = {
11
+ content: [], // files to extract the selectors from
12
+ css: [], // css
13
+ extractors: [
14
+ {
15
+ extractor: purgeCSSFromPug,
16
+ extensions: ['pug']
17
+ },
18
+ ]
19
+ }
9
20
 
10
- // TODO: DEMONSTRATE API
21
+ const purgecss = await new PurgeCSS().purge();
11
22
  ```
@@ -1,3 +1,15 @@
1
+ /**
2
+ * Pug extractor for PurgeCSS
3
+ *
4
+ * A PurgeCSS extractor for PUG files that automatically generates a list of
5
+ * used CSS selectors. This extractor can be used with PurgeCSS to eliminate
6
+ * unused CSS and reduce the size of your production builds.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+
11
+ import type { ExtractorResultDetailed } from 'purgecss';
12
+
1
13
  /**
2
14
  * Get the potential selectors from Pug code
3
15
  *
@@ -6,7 +18,6 @@
6
18
  *
7
19
  * @public
8
20
  */
9
- declare const purgeFromPug: (content: string) => string[];
10
- export default purgeFromPug;
21
+ export declare const purgeCSSFromPug: (content: string) => ExtractorResultDetailed;
11
22
 
12
23
  export { }
@@ -1 +1,68 @@
1
- import s from"pug-lexer";const a=a=>{const e=s(a),t=[];for(const s of e)switch(s.type){case"tag":case"id":case"class":t.push(...s.val.split(" "));break;case"attribute":if("class"===s.name||"id"===s.name){if("string"!=typeof s.val)continue;t.push(...(s.mustEscape?s.val.replace(/"/g,""):s.val).split(" "))}}return t};export{a as default};
1
+ import lex from 'pug-lexer';
2
+
3
+ /**
4
+ * Pug extractor for PurgeCSS
5
+ *
6
+ * A PurgeCSS extractor for PUG files that automatically generates a list of
7
+ * used CSS selectors. This extractor can be used with PurgeCSS to eliminate
8
+ * unused CSS and reduce the size of your production builds.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+ const getAttributeTokenValues = (token) => {
13
+ if (typeof token.val === "string") {
14
+ return (token.mustEscape ? token.val.replace(/"/g, "") : token.val).split(" ");
15
+ }
16
+ return [];
17
+ };
18
+ /**
19
+ * Get the potential selectors from Pug code
20
+ *
21
+ * @param content - Pug code
22
+ * @returns the attributes, classes, ids, and tags from the Pug code
23
+ *
24
+ * @public
25
+ */
26
+ const purgeCSSFromPug = (content) => {
27
+ const tokens = lex(content);
28
+ const extractorResult = {
29
+ attributes: {
30
+ // always add to attributes, to handle things like [class*=foo]
31
+ names: ["class", "id"],
32
+ values: ["true", "false"],
33
+ },
34
+ classes: [],
35
+ ids: [],
36
+ tags: [],
37
+ undetermined: [],
38
+ };
39
+ for (const token of tokens) {
40
+ if (token.type === "tag") {
41
+ extractorResult.tags.push(token.val);
42
+ }
43
+ else if (token.type === "class") {
44
+ extractorResult.classes.push(...token.val.split(" "));
45
+ extractorResult.attributes.values.push(...token.val.split(" "));
46
+ }
47
+ else if (token.type === "id") {
48
+ extractorResult.ids.push(token.val);
49
+ extractorResult.attributes.values.push(token.val);
50
+ }
51
+ else if (token.type === "attribute") {
52
+ const tokenValues = getAttributeTokenValues(token);
53
+ if (token.name === "class") {
54
+ extractorResult.classes.push(...tokenValues);
55
+ }
56
+ else if (token.name === "id") {
57
+ extractorResult.ids.push(...tokenValues);
58
+ }
59
+ else {
60
+ extractorResult.attributes.names.push(token.name);
61
+ }
62
+ extractorResult.attributes.values.push(...tokenValues);
63
+ }
64
+ }
65
+ return extractorResult;
66
+ };
67
+
68
+ export { purgeCSSFromPug };
@@ -1 +1,70 @@
1
- "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=e(require("pug-lexer"));module.exports=e=>{const a=t.default(e),s=[];for(const e of a)switch(e.type){case"tag":case"id":case"class":s.push(...e.val.split(" "));break;case"attribute":if("class"===e.name||"id"===e.name){if("string"!=typeof e.val)continue;s.push(...(e.mustEscape?e.val.replace(/"/g,""):e.val).split(" "))}}return s};
1
+ 'use strict';
2
+
3
+ var lex = require('pug-lexer');
4
+
5
+ /**
6
+ * Pug extractor for PurgeCSS
7
+ *
8
+ * A PurgeCSS extractor for PUG files that automatically generates a list of
9
+ * used CSS selectors. This extractor can be used with PurgeCSS to eliminate
10
+ * unused CSS and reduce the size of your production builds.
11
+ *
12
+ * @packageDocumentation
13
+ */
14
+ const getAttributeTokenValues = (token) => {
15
+ if (typeof token.val === "string") {
16
+ return (token.mustEscape ? token.val.replace(/"/g, "") : token.val).split(" ");
17
+ }
18
+ return [];
19
+ };
20
+ /**
21
+ * Get the potential selectors from Pug code
22
+ *
23
+ * @param content - Pug code
24
+ * @returns the attributes, classes, ids, and tags from the Pug code
25
+ *
26
+ * @public
27
+ */
28
+ const purgeCSSFromPug = (content) => {
29
+ const tokens = lex(content);
30
+ const extractorResult = {
31
+ attributes: {
32
+ // always add to attributes, to handle things like [class*=foo]
33
+ names: ["class", "id"],
34
+ values: ["true", "false"],
35
+ },
36
+ classes: [],
37
+ ids: [],
38
+ tags: [],
39
+ undetermined: [],
40
+ };
41
+ for (const token of tokens) {
42
+ if (token.type === "tag") {
43
+ extractorResult.tags.push(token.val);
44
+ }
45
+ else if (token.type === "class") {
46
+ extractorResult.classes.push(...token.val.split(" "));
47
+ extractorResult.attributes.values.push(...token.val.split(" "));
48
+ }
49
+ else if (token.type === "id") {
50
+ extractorResult.ids.push(token.val);
51
+ extractorResult.attributes.values.push(token.val);
52
+ }
53
+ else if (token.type === "attribute") {
54
+ const tokenValues = getAttributeTokenValues(token);
55
+ if (token.name === "class") {
56
+ extractorResult.classes.push(...tokenValues);
57
+ }
58
+ else if (token.name === "id") {
59
+ extractorResult.ids.push(...tokenValues);
60
+ }
61
+ else {
62
+ extractorResult.attributes.names.push(token.name);
63
+ }
64
+ extractorResult.attributes.values.push(...tokenValues);
65
+ }
66
+ }
67
+ return extractorResult;
68
+ };
69
+
70
+ exports.purgeCSSFromPug = purgeCSSFromPug;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "purgecss-from-pug",
3
- "version": "5.0.0",
3
+ "version": "7.0.1-alpha.0",
4
4
  "description": "Pug extractor for PurgeCSS",
5
5
  "author": "Ffloriel",
6
6
  "homepage": "https://github.com/FullHuman/purgecss#readme",
@@ -31,5 +31,6 @@
31
31
  "publishConfig": {
32
32
  "access": "public",
33
33
  "registry": "https://registry.npmjs.org/"
34
- }
34
+ },
35
+ "gitHead": "5a51b05a191ba48b5e55440ac2e66822f9e182fe"
35
36
  }