[Enhancement request] Allow importing named language imports
See original GitHub issueIs 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:
import hljs from 'highlight.js/lib/core';
import js from 'highlight.js/lib/languages/javascript';
import json from 'highlight.js/lib/languages/json';
import yml from 'highlight.js/lib/languages/yaml';
import md from 'highlight.js/lib/languages/markdown';
hljs.registerLanguage('javascript', js);
hljs.registerLanguage('json', json);
hljs.registerLanguage('yaml', yml);
hljs.registerLanguage('markdown', md);
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:
import hljs from 'highlight.js/lib/core';
import { javascript, json, yaml, markdown } from 'highlight.js/lib/languages';
hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('json', json);
hljs.registerLanguage('yaml', yaml);
hljs.registerLanguage('markdown', markdown);
This only requires adding lib/languages/index.js
which has lines like:
export { default as json } from './json';
export { default as javascript } from './javascript';
// aliases could be done like this:
export { default as js } from './javascript';
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'
)
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
import - JavaScript - MDN Web Docs - Mozilla
The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be modified by ......
Read more >Auto import | IntelliJ IDEA Documentation - JetBrains
In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Enable the Optimize imports on the fly option and...
Read more >Import solutions - Power Apps | Microsoft Learn
The solution you are importing has only form changes (diff) in the form XML when it should have the full form XML. A...
Read more >Python import: Advanced Techniques and Tips
Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable. This tutorial will provide a...
Read more >Using Node.js require vs. ES6 import/export - Stack Overflow
You can use named imports to selectively load only the pieces you need. That can save memory. Import can be asynchronous (and in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.