Skip to content

Commit e556afc

Browse files
authored
feat: add purgecss-from-tsx (#716)
1 parent ddbdac3 commit e556afc

File tree

11 files changed

+13140
-12025
lines changed

11 files changed

+13140
-12025
lines changed

package-lock.json

+12,938-12,023
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"purgecss-from-jsx",
1010
"purgecss-from-pug",
1111
"purgecss-from-twig",
12+
"purgecss-from-tsx",
1213
"purgecss-webpack-plugin",
1314
"purgecss-with-wordpress",
1415
"vue-cli-plugin-purgecss"

packages/purgecss-from-jsx/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {extend} from "acorn-jsx-walk";
55

66
extend(walk.base);
77

8-
function purgeFromJsx(options: acorn.Options) {
8+
function purgeFromJsx(options?: acorn.Options) {
99
return (content: string): string[] => {
1010
// Will be filled during walk
1111
const state = {selectors: []};

packages/purgecss-from-tsx/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `purgecss-from-tsx`
2+
3+
> TODO: description
4+
5+
## Usage
6+
7+
```
8+
const purgecssFromTsx = require('purgecss-from-tsx');
9+
10+
// TODO: DEMONSTRATE API
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export const TEST_1_CONTENT = `
2+
import PropTypes from "prop-types";
3+
import React from "react";
4+
5+
type MyComponentProps = {
6+
login: any;
7+
};
8+
9+
type MyComponentState = {
10+
username: string;
11+
password: string;
12+
};
13+
14+
class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
15+
static propTypes: any;
16+
17+
render() {
18+
return (
19+
<React.Fragment>
20+
<div className="test-container">Well</div>
21+
<div className="test-footer" id="an-id"></div>
22+
<a href="#" id="a-link" className="a-link"></a>
23+
<input id="blo" type="text" disabled/>
24+
</React.Fragment>
25+
);
26+
}
27+
}
28+
29+
MyComponent.propTypes = {
30+
login: PropTypes.func.isRequired
31+
};
32+
33+
export default MyComponent;
34+
`;
35+
36+
export const TEST_1_TAG = ["div", "a", "input"];
37+
38+
export const TEST_1_CLASS = ["test-container", "test-footer", "a-link"];
39+
40+
export const TEST_1_ID = ["a-link", "blo"];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import purgeTsx from "../src/index";
2+
3+
import { TEST_1_CONTENT, TEST_1_TAG, TEST_1_CLASS, TEST_1_ID } from "./data";
4+
5+
const plugin = purgeTsx();
6+
7+
describe("purgeTsx", () => {
8+
describe("from a normal html document", () => {
9+
it("finds tag selectors", () => {
10+
const received = plugin(TEST_1_CONTENT);
11+
for (const item of TEST_1_TAG) {
12+
expect(received.includes(item)).toBe(true);
13+
}
14+
});
15+
16+
it("finds classes selectors", () => {
17+
const received = plugin(TEST_1_CONTENT);
18+
for (const item of TEST_1_CLASS) {
19+
expect(received.includes(item)).toBe(true);
20+
}
21+
});
22+
23+
it("finds id selectors", () => {
24+
const received = plugin(TEST_1_CONTENT);
25+
for (const item of TEST_1_ID) {
26+
expect(received.includes(item)).toBe(true);
27+
}
28+
});
29+
30+
it("finds all selectors", () => {
31+
const received = plugin(TEST_1_CONTENT);
32+
const selectors = [...TEST_1_TAG, ...TEST_1_CLASS, ...TEST_1_ID];
33+
for (const item of selectors) {
34+
expect(received.includes(item)).toBe(true);
35+
}
36+
});
37+
});
38+
});

packages/purgecss-from-tsx/package-lock.json

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "purgecss-from-tsx",
3+
"version": "4.0.3",
4+
"description": "TSX extractor for PurgeCSS",
5+
"author": "Ffloriel",
6+
"homepage": "https://github.com/FullHuman/purgecss#readme",
7+
"license": "ISC",
8+
"main": "./lib/purgecss-from-tsx.js",
9+
"module": "./lib/purgecss-from-tsx.esm.js",
10+
"types": "./lib/purgecss-from-tsx.d.ts",
11+
"directories": {
12+
"lib": "lib",
13+
"test": "__tests__"
14+
},
15+
"files": [
16+
"lib"
17+
],
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/FullHuman/purgecss.git"
21+
},
22+
"scripts": {
23+
"test": "echo \"Error: run tests from root\" && exit 1"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/FullHuman/purgecss/issues"
27+
},
28+
"dependencies": {
29+
"acorn": "^7.4.0",
30+
"purgecss-from-jsx": "^4.0.3",
31+
"typescript": "^4.3.2"
32+
}
33+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import acorn from "acorn";
2+
import * as ts from "typescript";
3+
import purgeFromJsx from "purgecss-from-jsx";
4+
5+
function purgeFromTsx(options?: {
6+
acornOptions?: acorn.Options,
7+
tsOptions?: ts.CompilerOptions
8+
}): (content: string) => string[] {
9+
return (content: string): string[] => {
10+
return purgeFromJsx(options?.acornOptions)(
11+
ts.transpileModule(content, {
12+
compilerOptions: {
13+
jsx: ts.JsxEmit.Preserve,
14+
...options?.tsOptions
15+
}
16+
}).outputText
17+
);
18+
};
19+
}
20+
21+
export default purgeFromTsx;

scripts/build.ts

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const packages = [
4545
{
4646
name: "purgecss-from-jsx",
4747
external: ["acorn", "acorn-walk", "acorn-jsx", "acorn-jsx-walk"],
48+
},
49+
{
50+
name: "purgecss-from-tsx",
51+
external: ["acorn", "purgecss-from-jsx", "typescript"],
4852
}
4953
];
5054

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"purgecss": ["packages/purgecss/src"],
2323
"@fullhuman/purgecss-from-html": ["packages/purgecss-from-html/src"],
2424
"@fullhuman/purgecss-from-pug": ["packages/purgecss-from-pug/src"],
25-
"@fullhuman/purgecss-from-jsx": ["packages/purgecss-from-jsx/src"]
25+
"@fullhuman/purgecss-from-jsx": ["packages/purgecss-from-jsx/src"],
26+
"@fullhuman/purgecss-from-tsx": ["packages/purgecss-from-tsx/src"]
2627
}
2728
},
2829
"include": [

0 commit comments

Comments
 (0)