-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
RCS replaces all words that are the same as the selectors #20
Comments
First of all the HTML problem is resolved by updating The JavaScript problem is following. I haven't found a good way of determining if a string is used for selectors or just as text. I came up with detecting non word characters in the text (in regex |
Hi @JPeer264 thanks for following up! I think a simple way to deal with the JS problem would be to only find and replace word matches with a prefix of "." or "#" in all strings. Do you think that's sufficient enough? |
Hm no that wouldn't be enough, as e.g. bootstrap is storing the selectors as plain strings sometimes: bootstrap.js#L405-L409, or jQuery has functions such as |
Hmm you're right, there's no easy way to distinguish class names accurately without missing something or overkilling. There're three more complicated ways I can think of that may work though:
|
Ya, those are good approaches and already had the same or similar approaches. In total I am more a fan of automation and less configuration. Here are my thoughts:
|
I took a look at other selector obfuscation solutions such as Google Closure Tools, it would seem that it enforces users to mark CSS classes with I'm not sure the use of mapping file is a good solution though. For starters, as you mentioned, the JS code will then contain a lot of redundant codes. The reference to the automatically generated |
I totally agree that it is risky in production. I guess this could work as you suggested in 1., replacing the code of However, your added documentation of the closure tools gave me an idea. I would like to add options for triggering classes in JS (so if I use another library, such as bootstrap, I am still able to rename those classes in an experimental way). I think following options for replacing selectors in JS would be neat:
I guess that works right? |
Btw, the original issue with your HTML should be fixed in v |
It's possible that if devs develop their own elements for styling, those won't be renamed? But yeah, only triggered by Adding an indirection layer like the Google Closure Tools did does seem like a better solution! In this way the |
No those elements are not yet removed. But this is actually a good feature. As I use Parse5 for HTML and PostCSS for AST it is not a big deal. Custom elements in HTML are easy to find, as it is well structured and reliable (maybe I am also missing something). Totally, I am also happy with that solution. It will just take some time I guess until this is implemented, as I also have to think about a good structure. |
With css-modules I don't have such problem, because in my case all class names are generated in that way If you want to add some |
@klimashkin good point. Then it is also not a breaking change 👍 |
Hey @Stephen-X .. I won't have much time the next few weeks. The implementation of this feature will take some time, I hope you'll understand. |
@JPeer264 No worries, take your time. I mostly use this utilities for personal projects 😄 Thanks for the great works so far! |
Would it be safe to assume that if a string (after concatenation) contains any word that isn't a CSS selector, then every word in that string should be excluded from renaming? I can't personally think of any situation where you pair selector names with other random text, except possibly in some error description. That would still miss a lot of cases, but it would solve the initial example and quite a few others. |
I am not sure if I understood it correctly, but I assume you meant something like following: Let's say we have two string in JavaScript, one with CSS selectors and one with normal string: const myString = 'Any string of my selector';
const mySelectors = 'selector another-selector'; And I have my CSS files: .selector {}
.another-selector {} In the variable If this is what you meant, then what about selectors which are not defined in any CSS, something like "JavaScript only selectors", such as |
Indeed, that's a destructive case I didn't consider. The aforementioned manual workarounds of adding a prefix/wrapper or similar would be needed to mark any JS-only selectors. |
For me, in JS you shouldn't deal with CSS's class or ID everywhere. Typically, it'll boils down to The list is somehow sparse. Unluckily, you need to compute the AST for the complete JS script to figure out if whatever string is being used for such methods. I wonder if you can compile the complete JS code (with all modules), compile to AST, track the methods using some string, and mark/flag such strings for renaming (using the source map ?).
|
The only bad thing is that there are a lot of modules which needs selectors, so everytime a new module is imported, which needs selectors, it must be included into some list of available "replaceable" functions. Those combined selectors are really hard to detect, as I already mentioned in here: JPeer264/node-rcs-core#85 (comment)
|
I guess with all the changes in rcs-core#95, this should be somehow fixed. At least, I had the same issues before and now it's working correctly on my project which is fairly large with many tricks (like Template, Id/Class mismatch, HTML using Hopefully, you'll be able to test soon! |
Just tried this tool and ran into similar issue. I have |
@waterplea which version do you use? |
You could also exclude import rcs from 'rcs-core';
// following methods also allow array of strings and RegExp
rcs.selectorLibrary.setExclude('error');
// or rcs.selectorsLibrary.setExclude (if you use rcs-core@3+) |
I used the latest. Thanks for the suggestion, in my case I ended up with no need to process js, so excluded it. |
Alright. I the next version |
Hi @JPeer264, I've found another error. Now I have this case - I used this error key in data attribute and it got minified: |
Alright, thanks for the update, I will add this to my tests. I think it is kinda confusing right now with the versions. Following should be true:
I think you have installed I guess it will be better for the future to have a monorepo and namespace everything, that all versions match across the packages; but this is now another topic. |
I'm having
Having a similar issue, but with EJS tags |
It's part of a series of issues I found, so I'm going to reuse the base test case and open separate issues for each of them 😄 I'm developing on Windows btw; not sure if these are all platform specific bugs.
Issue Description:
RCS replaces words that are the same as selectors in the wrong elements (e.g.
<meta>
,<p>
).Steps to reproduce this issue:
index.html
:style.css
:script.js
:selector_obfs.js
:node selector_obfs.js
. The "in" word inscript.js
file got renamed:As well as
<meta>
inindex.html
's<head>
:When I was working on my own projects, I found that words in
<p>
could get renamed as well, although for some reason I couldn't reproduce it in this example.The text was updated successfully, but these errors were encountered: