-
-
Notifications
You must be signed in to change notification settings - Fork 765
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
ECMAScript Modules #1595
Comments
Do you mean that we currently export maplibre as commonjs and we should export it as esm? |
Yes, export maplibre as esm. Sure, I think we might be able to take small steps towards it. A major blocker previously was i.e. also all this node assert that would break the dev build if exported as esm. The first step is likely to have the build as esm. |
The published dist/ dir already has multiple versions: https://unpkg.com/browse/maplibre-gl@2.4.0/dist/ So shouldn't it be easy to just add a new one: maplibre-gl-esm.js? For now, skypack works:
.. but we'd like to avoid skypack's conversions which sometimes fail. And it would be nice to use other cdn's as well. |
@backspaces That doesn't work for me:
gives me an |
Try it using this:
|
Thanks, that works fine when run directly in the browser, and even shows a basic map:
But when I try to run it with node (using this https module loader),
|
The devicePixelRatio likely fail because there is no device/display. What do you hope to achieve running it in node ? Depending on your use-case, you might want to use the node distribution of maplibre-gl-native instead https://www.npmjs.com/package/@maplibre/maplibre-gl-native |
The use case would be including Maplibre as dependency, for example offering a maplibre renderer in Maputnik or other kind of web apps depending on Maplibre. So it still would be web and not Android nor iOS, that's why I think using maplibre-gl-native would not work right? |
If it's rendering the vector map in the browser, gl-js is the place to go. If it's server-rendering, the gl-native can be used. |
Related to maplibre/maplibre#159 through tree-shaking |
I have made a lot of change in version 4 and basically removed the global object that existed before. |
The article which I'm looking at right now is the following: Since we add a package.json file to dist folder in order to force bundling tools to treat maplibre as common js I think we'll need to place the esm output in a folder called mjs or something to avoid it being treated as commonjs. I'll do some experiments locally and see if I can get to a working version or not. |
Seems to work now as recent Maputnik versions (v2.0) use MapLibre 4.1.2 as dependency. |
Below is a link to an article on esm with rollup and worker configuration: If anyone has more information on this or knows this topic more than I do please feel free to let me know. |
@timocov sorry to tag and feel free to unsubscribe, but it would be great if you can take a peak at this and let me know if I'm missing anything - I know you have a lot of knowledge/experience around rollup, build, esm, commonjs amd etc, and any input would be helpful here. |
Am I understanding correctly that most of the code size is inside the worker? And that all of the code is used? In that case ESM builds wouldn't make much of a difference. It would be different if lots of worker code is not used, in that case it would be better to use dynamic imports and a cjs build for it, but still not ESM. If most of the code size is on the main thread, ESM would help. |
There's a lot (I think) of shared code between the workers and the main thread, which is what the current build try to improve in terms of package size - i.e. pack the shared code once to be used by both the worker and the main thread without increasing the package size. |
@HarelM , I think the situation has developed quite a lot since the article you linked here was written in 2020. As the article states few sections down "All things equal if we ran this in Chrome we’d be golden and we’ve have a magical day.". Thing is, both safari (2021) and firefox (2023) have stabilized support for esm modules in workers since, so now we're close to a point where esm can simply be used throughout. The current support levels for esm modules in workers is tracked here - I'm not sure how high it needs to be, but it's steadily growing. ESM won't make it into v5 anyways, adding at least half a year till next major, and we have blockers still with indeterminate timelines (#4735), so when we're technically ready, we might re-assess the esm support level, which if sufficient could save the majority of trouble outlined in the bundling article. |
@birkskyum is there a progress in the area of sharing code between worker thread and main thread with esm? I can't find anything on that... |
Progress? I'm not familiar with any ongoing work on browser apis in that direction. I don't think the ESM modules will make code sharing between worker/main better or worse - is there any problem with the current rollup bundle split strategy of client/worker/shared? and how does that relate to the module format? |
@birkskyum I wonder if bundle size would be smaller with ESM if you're not using summery features? |
It's not that there is a problem, but as far as I understand, the code sharing that is performed right now can't be achieved with esm. |
ESM is the new standard with import/export etc. We don't use it now because there are some old mapbox dependencies that only export .cjs, but nothing more than we can handle. Also, firefox has for 5 years struggled to support this in web workers, and now it's about time to move on:
Firefox (Update: Resolved from v114)
Web worker support ships with Firefox 114 PR, Release notes
The issue was around lack ESM support for web workers: https://caniuse.com/mdn-api_worker_worker_ecmascript_modules
5+ years of ongoing effort: https://bugzilla.mozilla.org/show_bug.cgi?id=1247687
Benchmarks
I once attempted to move the benchmark to ESM #964 , but found the change wasn't backward compatible with the already uploaded benchmarks of prev versions, so it was rolled back #969 .
Point of this issue
This can be a tracking issue where we first map the dependencies we have that aren't compatible, and then keep this updated when they are updated
The text was updated successfully, but these errors were encountered: