-
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
[Enhancement request] Allow importing named language imports #3470
Comments
Aren't dynamic imports the true solution to this problem?
It's definitely not as aliases aren't even guaranteed to be unique (not even in core, sadly). |
Ok for aliases, but..
..misses the point (the issue is about DX). I'll try to rephrase: requiring a user (developer) of highlight.js to write 10 separate import lines is way less developer-friendly then allowing them to write a single import line with named imports, like Static vs dynamic imports will work with both. |
Exactly, when I said dynamic I was imagining of something like: const supportedLanguages = ["basic", "cobol", "other"]
supportedLanguages.forEach(lang => {
import(`${languages_dir}/${lang}`).then(hljs.registerLanguage)
}); No need to repeat anything. |
Aha ok, but that makes the import async (and may trigger unintentional bundle splitting in tools like webpack. In webpack this would generate x chunks where x=language. It doesn't read as nicely and doesn't allow import type hints/clickthrough (VS code) |
I'm always cautious with new feature requests because often the full expense is rarely counted (many times it's impossible to know and paid later). For example this small request leads to:
And that's just only off the top of my head with only a minutes thought. :) So I ask myself is all that (and potential unknowns) worth it for a nice idea (truly) that no one else has ever brought to our attention before. I wonder if this wouldn't be worth re-considering more after we drop CJS support in the future, when our build pipeline theoretically becomes much simpler.
Were you imagining tackling all that? Thoughts? |
@joshgoebel Thx for the detailed analysis, I need to have a go at it. I assumed the ES6/CJS/CDN compatibility was handled by a build tool from a single source, but it can also be done manually without too much effort. It would definitely be useful for CJS too:
I would say yes, hide in the docs (but keeping the ability in the code)
I think that's not an issue: import { lang1, lang2, lang3 } from 'highlight.js/lib/languages';
import thirdPartyLang from 'thirdpartyLang';
This & some of the other questions I can only know after getting more familiar with the dev setup & build of highlight.js I did imagine having to create docs, unit tests etc as I maintain the metalsmith SSG (whose website metalsmith.io uses highlight.js) |
There is a single set of canonical source files, yes - I was only saying the build scripts would have to be updated to generate the necessary files, etc... I didn't know if you were considering ES6 + CJS and weren't sure if you were aware we have multiple deliverable assets (CDN, node, browser, etc)...
Sure if you want to look into it with said caveat, go right ahead. |
I think this line of thinking is why modern software can be so slow when computers are faster than they'd ever been... I just don't see a large enough win here to justify these changes. We increase code complexity, compile times, increase RAM usage...
...and potentially vastly increase the download size if someone's bundler isn't properly tree pruning or analyzing side effects properly... (a bundler or config problem) Too many negatives for a tiny quality of life change that truly only effects someone just getting started with Highlight.js on a new project. Just adding a new language to an existing file takes almost no time and the list of imports isn't something someone one needs to constantly fiddle with... I did add this to the v12 list to re-review to see if anything has changed. Closing this issue and the PR for now. |
In reviewing this again (just now) for consideration for v12 I still find it lacking in that it would potentially force a ~ megabyte of unneeded JS to be downloaded and parsed. IMHO this makes more sense if/when we go fully async so that registering the languages doesn't actually perform any actual requests or parsing... so one could either say:
OR
But in both cases only the specified languages would ever be download and parsed, not every single language that was in the index. |
Is your request related to a specific problem you're having?
When using a bundler on the front-end it is important to only include the required languages to reduce bundle size,
but it feels a bit clunky to register languages like this:
The solution you'd prefer / feature you'd like to see added...
It would be much better if it could just be done like this:
This only requires adding
lib/languages/index.js
which has lines like:I could make a PR for this (please mention whether aliasing is desirable, I believe this could also be implemented in client code as
import { javascript as js } from 'highlight.js/languages'
)The text was updated successfully, but these errors were encountered: