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
#265: Replace PhantomJS with jsdom #280
Conversation
@vseventer Can you force-push to re-trigger the travis build? |
@giakki @XhmikosR @mikelambert Thoughts on this? |
Current status of this: #265 (comment) |
You can test this branch with this package: https://www.npmjs.com/package/uncss-jsdom. |
jsdom saved me from 2h of trying to find why phantomjs wasnt doing the job. please merge it :) |
@RyanZim I tried out this branch and had a problem. JSDom did not find an asset (a JS file) that PhantomJS was loading without trouble. I wonder if there's something wrong with the asset lookup. |
What is an "asset" specifically, and how are you loading it? Is it a basic |
Here is a simple test repo that reproduces the problem I was seeing: https://github.com/davidtheclark/uncss-jsdom-test With the PhantomJS version, this test works. With uncss-jsdom, I see this error:
@mikelambert sorry if the terminology wasn't clear. By "asset" I meant a file that is referenced with a |
@davidtheclark The issue is the leading The PhantomJS uncss had couldn't load these assets either, however, it failed silently, jsdom correctly reports the error. You need to use |
@RyanZim I tried it with |
uncss allows Don't think this is a particular issue with jsdom, especially if PhantomJS ignores the JS file anyway. But something that should be fixed regardless, imo. |
@vseventer @RyanZim Thanks for chiming in! I opened up #287 for this issue. I guess it still seems to me that this problem needs to be addressed before the JSDom implementation merges, or else the JSDom implementation should deliver a warning but not throw an error when it encounters root-relative paths. If the behavior is not changed, then a very common and widespread practice will break uncss, right? |
Yes and No. PhantomJS would quietly output without processing the JS; jsdom fails hard (which is better IMO). Either way, this should be addressed (see #287 (comment)). To be clear, switching to jsdom was always intended to be a breaking change. |
@RyanZim: I agree that it's important to bring to the user's attention the fact that their JS is not loading; but a hard failure would mean that we suddenly can't use uncss with our valid HTML that worked before, right? For my case, as one example, I do not care whether the JS loads, would even prefer to be able to tell uncss not to try to load it. Just wanted to make sure I was being clear about my concern. Thanks for your work on the transition! |
Nice work! Any estimations when it will be released? |
We've tested this branch on our relatively large project, which used PhantomJS-based uncss before. Everything seems to work perfectly, except few things:
However, there are also few positive things:
|
|
Yeap, tested with full paths — seems to be no difference. I thought that issue might be related to Grunt task. But I desicted it and simplified to plain example, and it made no difference too. Here is simplified case: Grunt task: const uncss = require('uncss-jsdom')
module.exports = function (grunt) {
grunt.registerMultiTask('uncss', 'Remove unused CSS', function () {
const done = this.async()
uncss('build/index.html', { htmlroot: 'build' }, function () {
done()
})
})
}
<!doctype html>
<html>
<head>
<link rel='stylesheet' href='/assets/styles/style.css'>
</head>
<body>
<script src='/assets/scripts/main.js'></script>
</body>
</html>
.test { border: 0 }
console.log('Hi!') You can see full structure in Kotsu repository (though, you will need to build it to see Hm, I also peeked into error message again and thought — maybe it's related to OS-specific path normalization? Windows handles paths differently from Linux (uses |
@ArmorDarks It might be - I'm using the test files (on Mac) as you described and |
So sad without this PR :( @vseventer Did you have any time to do test on PC? |
@ArmorDarks Sorry, this escaped my attention, I'm on Mac so this is not high priority for me. That said, I'll try and get to it in the next couple of days. |
So this has been sitting around awhile, while I left it unintegrated hoping for more testing (didn't realize it would take so long and lose momentum, sorry!) AFAIK, these are the only two known issues?
Is it worth adding a built-in ignore to catch the svg/root issue? I'm not clear on this syntax, to know if a valid uncss ignore regex can be built that catches all these cases? (Then we could ship without breaking clients, and could just remove the built-in ignore when jsdom/jsdom#1750 gets fixed.) And assuming it is broken on Windows (and not something unique to @ArmorDarks setup), I would consider that blocking bug (even though I also am only on Mac). @ArmorDarks , do you think you'd have any time to dig into the uncss/jsom code yourself and see where things are going wrong and where a patch might be devised? Otherwise we're just hoping @vseventer can find the time to do more testing... :) |
@mikelambert Yeap, this seems to be right
So far yes, That's the only ones we were able to find on our codebase. It should cover most part of use cases, but we don't use some part of CSS at all (for instance, deeply nested selectors with high specificity), so I can only hope that it won't break anything on more tricky codebases. But I think we'll never know untill will let it go into the wild, since it seems that not much people finding that PR and even less doing testing.
👍
Well, I can try to do something, but I'm not very skilled JavaScript developer. So far I'm pretty much sure that it's related to Windows-specific paths formatting ( It would be great if @vseventer or someone else on PC would finally test it, so we can be sure that it isn't related to my specific configuration, though, I tried to make build with stripped everything else, to minimize the effect of possible configuration flaws. |
@mikelambert That svg root problem is actually a hack in normalize css for an old version of IE (I think IE9?) You'd never use a selector like that in normal real life. Windows does need fixed, but I'm stranded on a Linux box, so can't help there. |
I'm able to reproduce the issue on Windows - a quick peek shows that the drive letter in Windows is the issue. (Root-)relative paths will always have the drive letter in front, so prepending the Unfortunately, JSDom does some manipulation on the script I will do some further testing later this week. |
I added a commit which greatly simplifies the @ArmorDarks - maybe you can give it a go to see if it works for you? |
Hi Thanks for update! Issue with local js-files finally fixed, thanks! But there are a couple of other issues:
|
I think we're pushing the limits of jsdom here. Regarding 1 and 2, ideally scripts should be executed within a sandbox, but don't think this is a priority for the jsdom maintainers. Given that jsdom loads the source from disk, yeah, it uses the Honestly, the issues you mention should be resolved. Not sure how, though. |
Are 1 and 2 warnings, or failures? Warning about bad loads and missing resources is fine. Erroring-out and dying completely, is not fine. If the latter, can you give an example stacktrace? I feel like page-resource loads should be executed within a try/catch in jsdom, to allow the page to finish loading as best it can... 3 and 4, I don't believe we can do anything about. And I don't believe this is anything "new" about jsdom. Phantomjs loading a file from local disk, would have the same issue, right? (It possibly was too-silent about errors, and maybe the file just went missing when run from PhantomJS) |
I think that's what was happening; haven't verified though. |
All errors aren't warning, they are full errors with stack trace, but they do not crash However, that's true only if it is impossible to load or execute properly remote script, but not CSS. For instance, one of 3rd party scripts containing following line: window.location.protocol + "//widgets.mango-office.ru/css/widget-button.css" or "//widgets.mango-office.ru/css/widget-button.css" will result in
Easy fix is to change relative protocol to static Though, I guess, it can be fixed by simply ignoring such specific CSS files with something like Also, in general, situation with |
So I wouldn't call it an anti-pattern. It certainly sucks for file:// based pages. But for any website (or script library!) that needs to work on both http and https, scheme-relative paths are actually the correct solution. But yes, it's an unfortunate consequence how they break with file:// pages. :( As far as CSS error-breakage you list above, this is actually an UnCSS error, not a jsdom error. See https://github.com/giakki/uncss/blob/master/src/utility.js#L123 . So on the bright side, this error is:
Just want to check though, does your hard-breakage error occur even with the original PhantomJS implementation? (It shouldn't) Looks like the code currently is designed to raise an error when attempting to load a local (relative) file that does not exist. I can see the pros of making local uncss references failures, but also the cons since scheme-relative URLs become local references in a way that I think is difficult for uncss to detect... |
I definitely had to avoid mention about "anti" part to avoid this offtopic. Not, it isn't good solution. Whenever you have Anyway, it doesn't make any point, since a lot of sites still using it and we have to live with it for now.
PhantomJS version raises identical error: So, in this matter jsdom doesn't bring any breaking changes. |
@mikelambert What else needs done here? (other than fixing merge conflicts) |
Ahhh sorry for the delay. @ArmorDarks Thanks, good points there, I should have known that. :) @RyanZim Nope, don't think anything else is required. Does someone want to fix up the merge conflicts, and then we can try a major-semver release with a few pending changes? |
@vseventer Can you fix merge conflicts? (or should someone else do it) |
2 similar comments
Just for the note, with ::placeholder {
color: #5a6066;
font-family: inherit;
font-size: 16px;
font-style: inherit
} While browsers-specific placeholders will be left intact: ::-webkit-input-placeholder {
color: #5a6066;
font-family: inherit;
font-size: 16px;
font-style: inherit
}
:-ms-input-placeholder {
color: #5a6066;
font-family: inherit;
font-size: 16px;
font-style: inherit
} Though, As of right now, this is easily workaroundable with ignore rule. |
…uncss` with `jsdom` Ref jsdom/jsdom#1750 Ref uncss/uncss#280 (comment)
See discussion at #265 for background.