-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new purgecss-from-jsx plugin #692
Conversation
…ew purgecss plugin for extracting id, class and tag from JSX content
Thanks!! That's great! I'll take a look at this PR this week |
@Nauja Looks good to me! I can see the types for acorn are not precise and you had to use
Are you thinking of adding support for cases like in the example below in this PR, or a following? If not in this PR, I will approve and merge this one. Let me know. import React from "react";
fonction getHello() {
return "class-hello" + " second-class"
}
const HELLO = getHello()
class MyComponent extends React.Component {
render() {
return (
<React.Fragment>
<div className={HELLO}>Well</div>
<div className="test-footer" id="an-id"></div>
<a href="#" id="a-link" className="a-link"></a>
<input id="blo" type="text" disabled/>
</React.Fragment>
);
}
}
export default MyComponent; |
What libraries did you find ? Maybe I can copy their type declarations and remove all those
Would be nice. For now I coded this plugin for a personal project and I didn't encounter the case where <div className={`total total-${obj.state}`}>...</div> I think it would be a nice but complex feature to have in a next PR. |
The types I have found for acorn-jsx:
I haven't tried them so I'm not suer how accurate they are |
From what I tested, it's impossible to do: JSXOpeningElement(node: JSXOpeningElement, state: any, callback) {
// JSXIdentifier | JSXMemberExpression | JSXNamespacedName
const nameState: any = {};
callback(node.name, nameState); Because But it's possible to do: JSXOpeningElement(node: unknown, state: any, callback) {
const n = node as JSXOpeningElement;
// JSXIdentifier | JSXMemberExpression | JSXNamespacedName
const nameState: any = {};
callback(n.name, nameState); However, I don't know if it's worth adding noise and I will do another commit for this PR because |
So I updated I searched why the checks failed but I see it's not related to this commit. |
Merged! Thanks for this new plugin. It will be published with the next version |
Cool, thanks :) |
Proposed changes
I wrote a new plugin for purgecss to extract id, class and tag attributes from JSX files.
Types of changes
Checklist
Further comments
You will find a working test in
purgecss-from-jsx/__tests__
.I added acorn acorn-walk acorn-jsx and acorn-jsx-walk dependencies to code this plugin and I also added necessary .d.ts files in
types
directory.For now it only support simple literal attributes for id, className and tag name, meaning it won't evaluate a JSX expression to a string and extract ids, classes or tags from it.
But this is sufficient for a simple JSX file as:
Hope you enjoy it :)