@fullhuman/purgecss-from-jsx 7.0.1-alpha.0 → 8.0.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.
Files changed (2) hide show
  1. package/README.md +104 -4
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,11 +1,111 @@
1
- # `purgecss-from-jsx`
1
+ # @fullhuman/purgecss-from-jsx
2
2
 
3
- > TODO: description
3
+ [![npm version](https://img.shields.io/npm/v/@fullhuman/purgecss-from-jsx.svg)](https://www.npmjs.com/package/@fullhuman/purgecss-from-jsx)
4
+
5
+ JSX extractor for [PurgeCSS](https://purgecss.com). This extractor parses JSX files (React, Preact, etc.) and extracts used selectors from component names, `className`, and `id` attributes.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @fullhuman/purgecss-from-jsx --save-dev
11
+ ```
4
12
 
5
13
  ## Usage
6
14
 
15
+ ### With PurgeCSS
16
+
17
+ ```js
18
+ import PurgeCSS from "purgecss";
19
+ import purgeFromJsx from "@fullhuman/purgecss-from-jsx";
20
+
21
+ const result = await new PurgeCSS().purge({
22
+ content: ["**/*.jsx"],
23
+ css: ["**/*.css"],
24
+ extractors: [
25
+ {
26
+ extractor: purgeFromJsx(),
27
+ extensions: ["jsx"],
28
+ },
29
+ ],
30
+ });
31
+ ```
32
+
33
+ ### With Custom Acorn Options
34
+
35
+ You can pass custom [acorn](https://github.com/acornjs/acorn) parser options:
36
+
37
+ ```js
38
+ import purgeFromJsx from "@fullhuman/purgecss-from-jsx";
39
+
40
+ const extractor = purgeFromJsx({
41
+ ecmaVersion: 2020,
42
+ sourceType: "module",
43
+ });
44
+ ```
45
+
46
+ ### Standalone
47
+
48
+ You can use the extractor standalone to extract selectors from JSX content:
49
+
50
+ ```js
51
+ import purgeFromJsx from "@fullhuman/purgecss-from-jsx";
52
+
53
+ const jsx = `
54
+ function App() {
55
+ return (
56
+ <div className="container">
57
+ <Header id="main-header" />
58
+ <button className="btn btn-primary">Click me</button>
59
+ </div>
60
+ );
61
+ }
62
+ `;
63
+
64
+ const selectors = purgeFromJsx()(jsx);
65
+
66
+ console.log(selectors);
67
+ // ["div", "container", "Header", "main-header", "button", "btn", "btn-primary"]
7
68
  ```
8
- const purgecssFromJsx = require('purgecss-from-jsx');
9
69
 
10
- // TODO: DEMONSTRATE API
70
+ ## API
71
+
72
+ ### `purgeFromJsx(options?: acorn.Options): (content: string) => string[]`
73
+
74
+ Creates an extractor function for JSX content.
75
+
76
+ #### Parameters
77
+
78
+ - `options` (optional) - [Acorn parser options](https://github.com/acornjs/acorn#interface). Defaults to `{ ecmaVersion: "latest" }`
79
+
80
+ #### Returns
81
+
82
+ A function that takes JSX content as a string and returns an array of selectors.
83
+
84
+ ## What It Extracts
85
+
86
+ The extractor parses JSX and extracts:
87
+
88
+ 1. **Component names** - JSX element names (e.g., `div`, `Header`, `MyComponent`)
89
+ 2. **className values** - String values from `className` attributes, split by spaces
90
+ 3. **id values** - String values from `id` attributes
91
+
92
+ ### Example
93
+
94
+ ```jsx
95
+ <Card className="card card-primary" id="featured-card">
96
+ <CardHeader />
97
+ <CardBody className="p-4" />
98
+ </Card>
11
99
  ```
100
+
101
+ Extracted selectors: `["Card", "card", "card-primary", "featured-card", "CardHeader", "CardBody", "p-4"]`
102
+
103
+ ## Limitations
104
+
105
+ - Only extracts static string values from `className` and `id`
106
+ - Dynamic expressions like `className={styles.container}` or template literals are not fully supported
107
+ - For dynamic class names, consider using PurgeCSS's [safelist](https://purgecss.com/safelisting.html) option
108
+
109
+ ## License
110
+
111
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fullhuman/purgecss-from-jsx",
3
- "version": "7.0.1-alpha.0",
3
+ "version": "8.0.0",
4
4
  "description": "JSX extractor for PurgeCSS",
5
5
  "author": "Ffloriel",
6
6
  "homepage": "https://github.com/FullHuman/purgecss#readme",
@@ -36,5 +36,5 @@
36
36
  "access": "public",
37
37
  "registry": "https://registry.npmjs.org/"
38
38
  },
39
- "gitHead": "5a51b05a191ba48b5e55440ac2e66822f9e182fe"
39
+ "gitHead": "859266ccb4ea80e0911fa32f1e215397978b3c89"
40
40
  }