-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Discuss: Website/browser build is not intended for use with Node.js. #1245
Comments
We have different build targets for node.js and for the browser/AMD environment. The |
I know. I'm saying that if you build for the browser, like so: |
Aha… Then it might be due to the bug #1220 that I fixed just yesterday. Could you please check again? |
I just checked. It still cannot be used from node when downloading from the site, but can be used from node when building locally for the browser (with 9.6.0 - same as it was on 9.5.0 and 9.4.0). At the site download, I selected a single language: javascript and clicked download. Inside the downloaded zip I opened I created a test file It fails with the downloaded I then unminified (or at least beautified) This code tries to use }); // <-- this is the end of the big function
hljs.registerLanguage("javascript", function(e) {
return {
// ... the rest of the javascript support is here However, there's no global object When I build it myself for the browser, it doesn't try to use a global }, e.registerLanguage("javascript", function(e) {
return {
// ... the rest of the javascript support is here and it works in node. |
Where is the website code which composes the package (according to the languages the user chooses via checkboxes) which is then downloaded? |
Quick fix for anyone else having this problem: (highlight.pack.js) !function(e){var n="object"==typeof window&&window||"object"==typeof self&&self;"undefined"!=typeof exports? |
A quick update. This is due to a discrepancy between the browser build and the CDN build. The site uses the latter to construct zips on the fly using simple concatenation of pre-compressed highlight.js and language files. At some point the real browser build started putting languages inside the contents of highlight.js. I'm not yet sure how best to fix it. Out of curiosity, why would you want to use a browser build from node? |
Because it's simpler/quicker/easier to check boxes and download a custom pack instead of building it. I have a small node script which highlights source code for the console and uses hljs as backend (still not public but It'll be at some stage) and it doesn't have other dependencies, so you just drop the pack at the same dir as the script and it works - except it doesn't now due to this issue (but it does work if I build the pack myself). I wanted to let users pick the languages they need and download the latest package themselves instead of asking them to build the node package or providing packages myself. |
Seems reasonable enough, even if unusual. And I want this fixed anyway. In the meantime however, you could probably just use the actual node build? If you need to constrain languages used in auto-detection it's as simple as |
Yeah, I could always just create a pack with all the languages and post it someplace and update it when hljs updates - it's not a big file anyway, so this issue is not blocking me in any way. Re auto detection, I actually found it a bit slow (relatively speaking), and found out that if I maintain a map of file extensions to languages as config then it works quite a bit faster than auto-detection (but I still have auto-detection as fallback). |
Why not the actual node package from npm https://www.npmjs.com/package/highlight.js? It has zero dependencies, just as the browser one. |
+1, I ran into the same issue described above and would love the fix to be in place for downloading the custom pack of languages, based on check box selections, instead of having to build it manually. Thanks for creating this great tool, @isagalaev. |
@entibo's suggestion worked for me (in Laravel 5.5) ...
require('./highlight.pack.js').initHighlightingOnLoad();
... |
Is this resolved now? If not could someone please summarize the issue? Is this just a duplicate of "please include a binary distributable with npm packages"? |
Closing due to inactivity and no one responding here. If this is still an issue please let us know and we can always reopen this. Although I'm pretty sure the right answer is to simply NOT use the browser build for nodejs includes... That seems wrong to me vs just requiring the library via Also moving forward we might do more "browser" specific things (better dynamic loading of languages) in the browser version that are purposely very different from the Node.js version... ie, there is a reason we have two very different build targets... They aren't expected to be interchangeable. |
Sorry, missed the question in October. The issue is still unresolved, I just tried with the default download from https://highlightjs.org/download/ and it still fails in the same way. The summary and steps to reproduce are described exactly at the first post. Please ask if you don't understand something, but I think the issue is pretty clear and easy to reproduce. The only question is if the project wants to fix it. Judging by the several thumbs-up on this issue, I'd say others would like it fixed too. |
Just to be clear, the steps to reproduce are as follows:
Expected result: nothing interesting happens. Actual result: the following error is shown:
|
I'm pretty sure it should be fairly trivial to fix, but the issue doesn't happen locally when I build for the browser, so I don't know how to reproduce and fix it locally. It only happens when when downloading from your site, and I don't know where is the code which is responsible for that composition of the content. |
Also note that your
And it does work when building for the browser locally, but doesn't work when you download a browser build from your site. |
How are you building the browser build locally exactly? Are you using :common? |
I think it's referring to the core library, not the BUNLDED library - which is very different. But it's obvious this could be cleared up or explained better. I'll try this and see what I come up with. |
|
But why not just fix it? the code clearly tries to detect the environment also when you download it from the site. It just fails to detect it correctly. I've asked few times before, where is the code which is responsible for creating the download and/or concatenating the fragments into a single download? I'm willing to have a go at it, but I don't know how to reproduce it except by downloading it from the site, but I need the actual source code of it to submit a patch. |
I'm not saying we won't, first we have to clearly understand the issue - your exact steps were helpful. But my point before does stand that the browser build is NOT necessarily intended to be the same as a NPM build. These things may diverge in the future and if it's never worked quite right this might be a blessing in disguise.
You'd think it was the build process, but it seems the results are different. Only @isagalaev knows I think. The code generated on the website is putting
Yeah, I'm not sure it's fixable by any changes to the repo. |
@egor-rogov Any thoughts? If the browser build is NOT truly the browser build but more of a general purpose build then it should be renamed accordingly. Right now my understanding is that the browser build ONLY needs to work well in the browser environment (and I think this is a good limitation for that build). That's also the same assumption that's embedded in our tests currently. Node build are tests with node, browser builds are tested in a fake browser environment. This has implications on future development and the new build system. |
Obviously the discrepancy here needs to be resolved I think (which would likely fix this in the short-term), but I'm asking more about this idea that the browser build should "just work" as a node require. I think that idea may be misguided. |
Thanks for the reply. Would you mind reopening this issue? It's still valid IMO, and apparently others are interested in it as well. I think it should either get fixed, or stated clearly that the browser build cannot be used with node, or stay open until one of those happen. |
Opening a new issue to track the actual discrepancy between local and website builds (which should NOT be the case IMHO).
Agree with your either/or here. |
One example: Future browser builds might come in ES5, ES6, ES* flavors. Once we had a build process that could do such things one could imagine a new feature supported by browsers before it was supported by Node (or where we purposely wanted a longer compatibility curve with Node)... and therefore the browser build might include newer technology. The browser build also currently includes a lot of code NOT necessary for Node.js usage at all... in a perfect world that wouldn't need to be parsed or compiled by JS in a node specific build. Obviously maintaining two forks would likely not be worth the effort, but if the build process did all this for us automatically it seem far more plausible. |
FWIW, I think it would be easiest to manage if the code is as unified as possible. Handling different code for different scenarios will only create more overhead IMO. But let's first wait for a way to reproduce/build the bad bundle locally, and then we can assess what can be done. |
Agree regarding the codebase in general. But the raw codebase itself and the built packages are a very, very different things - on only going to become more so over time. We already produce 3 different builds... browser, CDN, node.js... it's easy, and automated. I wouldn't want to maintain 3 sets of code by hand, but there are definitely good reasons to build and package the code differently for different environments. Though CDN is really browser + CDN assets... Node.js build is very different than browser builds though.
I assigned isagalaev to the other issue, so we'll have to see. I bet the server is simply using a slightly different/older build process and it really should be brought into alignment with the local build tools. |
Hopefully, because that sounds like it would be very easy to fix. Thanks again for your comments and the other issue. |
Well, we will see. :) I think the server is running Python, not JS... so that might be a small issue... Pretty sure my new build process will (as it stands currently) simply break this again though as my assumption is the |
See discussion and comment here: highlightjs/highlight.js#1245 (comment)
See discussion and comment here: highlightjs/highlight.js#1245 (comment)
See discussion and comment here: highlightjs/highlight.js#1245 (comment)
@egor-rogov Do you have a problem saying that what the website build is for WEB usage, and not Node? If someone wants to use Highlight.js with Node they should be using the That's my gut feeling at least. The exact issue that resulted in the creation of this issue isn't theoretically hard to fix (thought it'll require server changes that only isagalaev can do) but I think long-term I could see the web build as being a distinct thing from the node build. They are honestly different environments and with smarter build tools I could imagine auto-magically delivering different optimized versions for each. So if this has NEVER worked (the website version has never worked in Node)... and only a few people are asking for it, I think it's worth considering whether it's a good idea for "fix" this vs the official line being "just use NPM"... and whether this will prevent us from doing nice things in the future. |
I agree. We have different build targets for different purposes, and requiring a compatibility can prove restrictive. |
See comments above... So we're going to close this as "won't fix, working correctly"... but where exactly would you have expected to see this stated clearly? When you built the library on the website? I have no issues with making this clearer and explaining the logic behind it. |
If it's relatively easy to solve so that the web build can work in node, then you can always say that it works for now but not guaranteed to be maintained in the future. Afteral, there is some value in being able to use a single file instead of depending on npm or build deps. |
But we already know the long-term plan is likely to diverge - and then that would be a breaking change - which we do try to avoid except for major releases. So we'll just say it never worked and it likely never will. :-) If you really want it to work today you could manually (or automatically) hack the file to fix the actual issue. Though no promises it'll be easy to continue to do so in the future.
If it's a node.js project why wouldn't you simply put it in |
var <any-name-hljs-included> = require('./highlight.pack');
fails withReferenceError: hljs is not defined.
when executed in node.js.This happens when downloading and extracting highlight.zip from https://highlightjs.org/download/ (tested both the default selected languages or with just one - javascript)
But it doesn't fail (and I can later use it nicely) if I build it on my own with
node tools/build.js javascript
or any other languages which I tried.This happens with 9.50 but also happened with 9.40 (worked when I build it, didn't from the download).
Looking at
highlight.pack.js
- both the downloaded and the one which I built, it seems that the code is supposed to work also in a module environment (tests forexports
availability at the beginning - identical first few LOC at both files), but it doesn't with the download.Is it a bug? expected? IMO it should be possible to require it also with the downloaded package.
The text was updated successfully, but these errors were encountered: