Add exportmaps for lib/* entrypoints to allow for CDNs to use roughly what they need.#62
Conversation
|
Hi!
ESM is also made with this in mind right? Weren’t browsers supposed to lex files and exclude what is not imported?
a) it reads like that issue describes a “workaround” too? That is to say, ESBuild has problems with different levels, not with top-level? Hmm, but looking at https://github.com/wooorm/lowlight/blob/main/index.js, it does export symbols from places? And it does not seem to do what the linked esbuild issue is about? That Sounds like you are running into some other bug? Is your problem perhaps esmsh specific? |
To my knowledge, no browser actually does this, even though they could.
Yes and no. ESM.sh offers a way to try to treeshake, but is unable to for some reason. I wouldnt consider it a bug because every bundler has different semantics for what's considered treeshakeable, and clearly ESBuild is determining it's not treeshakeable (I could probably put together a Vite / ESBuild project to see if it treeshakeable there or not) however , every other CDN will not even attempt to treeshake, so for people not using esm.sh they will have to import the entire library no matter what and cannot use the "lib/" files to selectively import only what they need. |
|
I cannot reproduce the problems you describe with esbuild:
import {common} from 'lowlight'
console.log(common)…then running: ./node_modules/.bin/esbuild example.mjs --bundle…does not include Whereas, through esmsh:
<!doctype html>
<html lang=en>
<title>Example</title>
<script type=module>
import {common} from 'https://esm.sh/lowlight@3?exports=common'
console.log(common)
</script>…then opening that in a browser (Safari), does connect to the abnf and zephir grammars. Which is likely a bug in esmsh. @ije do you think this is a bug in esmsh? That the |
|
it does only export the |
It seems that Lowlight is written with Bundlers in mind to treeshake. However, if someone is using a CDN, or is using a bundler which does not support tree-shaking re-exported namespaces, then they have to pull in every single language from highlight.js
I would like to propose that the entrypoints in
./lib/*.jsbe exposed for consumption as a "quasi-treeshaking" mechanism as proposed in this PR.I did try to use esm.sh 's treeshaking mechanism:
But it made no difference in size. My guess is that this doesnt work because esm.sh uses ESBuild, and ESBuild will not treeshake re-exported namespaces:
evanw/esbuild#1420
Which is what
./index.jsis doing.Anyways, I hope you'll consider this PR so that more people can use lowlight :)