@fullhuman/purgecss-from-tsx 7.0.2 → 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 +130 -4
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,11 +1,137 @@
1
- # `purgecss-from-tsx`
1
+ # @fullhuman/purgecss-from-tsx
2
2
 
3
- > TODO: description
3
+ [![npm version](https://img.shields.io/npm/v/@fullhuman/purgecss-from-tsx.svg)](https://www.npmjs.com/package/@fullhuman/purgecss-from-tsx)
4
+
5
+ TSX extractor for [PurgeCSS](https://purgecss.com). This extractor parses TSX files (TypeScript + JSX) and extracts used selectors from component names, `className`, and `id` attributes.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @fullhuman/purgecss-from-tsx --save-dev
11
+ ```
4
12
 
5
13
  ## Usage
6
14
 
15
+ ### With PurgeCSS
16
+
17
+ ```js
18
+ import PurgeCSS from "purgecss";
19
+ import purgeFromTsx from "@fullhuman/purgecss-from-tsx";
20
+
21
+ const result = await new PurgeCSS().purge({
22
+ content: ["**/*.tsx"],
23
+ css: ["**/*.css"],
24
+ extractors: [
25
+ {
26
+ extractor: purgeFromTsx(),
27
+ extensions: ["tsx"],
28
+ },
29
+ ],
30
+ });
7
31
  ```
8
- const purgecssFromTsx = require('purgecss-from-tsx');
9
32
 
10
- // TODO: DEMONSTRATE API
33
+ ### With Custom Options
34
+
35
+ You can pass custom options for both the TypeScript compiler and the acorn parser:
36
+
37
+ ```js
38
+ import purgeFromTsx from "@fullhuman/purgecss-from-tsx";
39
+
40
+ const extractor = purgeFromTsx({
41
+ // Acorn parser options
42
+ acornOptions: {
43
+ ecmaVersion: 2020,
44
+ sourceType: "module",
45
+ },
46
+ // TypeScript compiler options
47
+ tsOptions: {
48
+ target: ts.ScriptTarget.ES2020,
49
+ },
50
+ });
51
+ ```
52
+
53
+ ### Standalone
54
+
55
+ You can use the extractor standalone to extract selectors from TSX content:
56
+
57
+ ```js
58
+ import purgeFromTsx from "@fullhuman/purgecss-from-tsx";
59
+
60
+ const tsx = `
61
+ interface Props {
62
+ title: string;
63
+ }
64
+
65
+ function Card({ title }: Props): JSX.Element {
66
+ return (
67
+ <div className="card">
68
+ <h2 id="card-title" className="card-title">{title}</h2>
69
+ <Button className="btn btn-primary" />
70
+ </div>
71
+ );
72
+ }
73
+ `;
74
+
75
+ const selectors = purgeFromTsx()(tsx);
76
+
77
+ console.log(selectors);
78
+ // ["div", "card", "h2", "card-title", "card-title", "Button", "btn", "btn-primary"]
79
+ ```
80
+
81
+ ## API
82
+
83
+ ### `purgeFromTsx(options?: Options): (content: string) => string[]`
84
+
85
+ Creates an extractor function for TSX content.
86
+
87
+ #### Parameters
88
+
89
+ - `options` (optional) - Configuration object with:
90
+ - `acornOptions` - [Acorn parser options](https://github.com/acornjs/acorn#interface). Defaults to `{ ecmaVersion: "latest" }`
91
+ - `tsOptions` - [TypeScript compiler options](https://www.typescriptlang.org/tsconfig). JSX is always preserved for parsing.
92
+
93
+ #### Returns
94
+
95
+ A function that takes TSX content as a string and returns an array of selectors.
96
+
97
+ ## How It Works
98
+
99
+ This extractor works in two steps:
100
+
101
+ 1. **TypeScript Transpilation** - Uses the TypeScript compiler to transpile TSX to JSX, stripping type annotations while preserving JSX syntax
102
+ 2. **JSX Extraction** - Uses [@fullhuman/purgecss-from-jsx](https://www.npmjs.com/package/@fullhuman/purgecss-from-jsx) to extract selectors from the resulting JSX
103
+
104
+ ## What It Extracts
105
+
106
+ The extractor parses TSX and extracts:
107
+
108
+ 1. **Component names** - JSX element names (e.g., `div`, `Header`, `MyComponent`)
109
+ 2. **className values** - String values from `className` attributes, split by spaces
110
+ 3. **id values** - String values from `id` attributes
111
+
112
+ ### Example
113
+
114
+ ```tsx
115
+ interface CardProps {
116
+ variant: "primary" | "secondary";
117
+ }
118
+
119
+ const Card: React.FC<CardProps> = ({ variant }) => (
120
+ <article className="card card-hover" id="featured">
121
+ <CardHeader className="p-4" />
122
+ <CardBody />
123
+ </article>
124
+ );
11
125
  ```
126
+
127
+ Extracted selectors: `["article", "card", "card-hover", "featured", "CardHeader", "p-4", "CardBody"]`
128
+
129
+ ## Limitations
130
+
131
+ - Only extracts static string values from `className` and `id`
132
+ - Dynamic expressions like `className={styles.container}` or template literals are not fully supported
133
+ - For dynamic class names, consider using PurgeCSS's [safelist](https://purgecss.com/safelisting.html) option
134
+
135
+ ## License
136
+
137
+ MIT License
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@fullhuman/purgecss-from-tsx",
3
- "version": "7.0.2",
3
+ "version": "8.0.0",
4
4
  "description": "TSX extractor for PurgeCSS",
5
5
  "author": "Ffloriel",
6
6
  "homepage": "https://github.com/FullHuman/purgecss#readme",
7
- "license": "ISC",
7
+ "license": "MIT",
8
8
  "main": "./lib/purgecss-from-tsx.js",
9
9
  "module": "./lib/purgecss-from-tsx.esm.js",
10
10
  "types": "./lib/purgecss-from-tsx.d.ts",
@@ -27,7 +27,7 @@
27
27
  "url": "https://github.com/FullHuman/purgecss/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@fullhuman/purgecss-from-jsx": "^7.0.2",
30
+ "@fullhuman/purgecss-from-jsx": "^8.0.0",
31
31
  "acorn": "^8.7.0",
32
32
  "typescript": "^5.6.2"
33
33
  },
@@ -35,5 +35,5 @@
35
35
  "access": "public",
36
36
  "registry": "https://registry.npmjs.org/"
37
37
  },
38
- "gitHead": "e95398d961e03a809b5e3da75e3dae1813b46b68"
38
+ "gitHead": "859266ccb4ea80e0911fa32f1e215397978b3c89"
39
39
  }