-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Cesium ES6 Module Support #8122
Comments
While I hacked together a ~80% complete script for the actual module conversion in the At first, I thought these tools were too limited, since we have a bunch of edge cases in the Cesium code but I wonder if a better solution could be to use UPDATE: After playing with a few options, I think rolling our own is probably the best approach to avoid uneeded code formatting churn. Ultimately these scripts are "one and done" anyway. |
roll-up has automatic shared code bundle generation when building multiple files at once (i.e. it would extract commonality between our workers and the primary Cesium.js file). This may greatly reduce overall file-size of loading Cesium apps in the browser if we use it properly. |
The biggest issue with shared code bundle generation is that workers cannot use es6 syntax (yet). You are still stuck with using the 'import' statement. |
Thanks, that does seem like a problem and a big limitation for Cesium's no-build goals during development. Some details here for anyone interested: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker A roll-up step for the workers that just produces non-ES6 module versions may be faster enough to get away with, but we'll have to try it out to see. |
FWIW, I did a mass AMD->ESM codemod migration on a work app last fall. I documented my workflow here: https://blog.isquaredsoftware.com/2018/11/git-js-history-rewriting/ The specific details on codemods are here: And I put up a repo with the conversion scripts I wrote: https://github.com/markerikson/git-js-repo-history-conversion-utils In my case it was even more complicated because I was trying to rewrite the entire app history to look like the code had always been written in ES6 syntax. |
Thanks @markerikson, will definitely check it out. |
Update Sandcastle is going to be interesting. I think we can get away with keeping it a AMD app for now and it can continue to use it's ancient version of Dojo. But for sucking in CesiumJS itself, it can use ES6 in the bucket during development and then pull in the built version (similar to what it does now). May actually not be that bad or it can turn into a total nightmare. We probably won't know until we get in there. |
I'm actually struggling a bit with how we may be able to run tests against the built version of Cesium the way we do now. I think one option is to have all of the Specs point to If that works, then to test against the non-ES6 combined/minified version, we could just have another auto-generated wrapper that exposes a ES6 module that just proxies to the built ES5 version. I think that gets us to what we have with the current set up, but like everything else, we won't know until we try. |
This is one of the big questions with our current es6 version. I’m just running a small subset of the combined tests against the built es6 file. It is hardly comprehensive. |
For those that haven't been following along, we're in the home stretch for ES6 support! One of the main open questions I'm trying to answer is how we ship ES6 (from a dist perspective). I think my preference is to not ship the Source directory in the zip at all and instead just ship a unified Cesium.js in ES6 format. Just like we have Users would then write code like the below and tools can use tree-shaking to remove the code users don't need. This seems to work great via roll-up import {
ArcGISTiledElevationTerrainProvider,
Math as CesiumMath,
Viewer
} from './Cesium.js'; The alternative would be to ship the Source directory and have people require modules in directly. The only benefit here is that you are definitely only including what gets used, but it's way more verbose. import ArcGISTiledElevationTerrainProvider from './Cesium/Source/Core/ArcGISTiledElevationTerrainProvider.js'
import CesiumMath from './Cesium/Source/Core/Math.js'
import Viewer from './Cesium/Source/Widgets/Viewer.js' Does anyone have a strong preference or argument for one or the other? I'm definitely leaning towards the first one so far. |
To clarify, the new packaging I'm suggesting something like
The |
@kring I know you've mostly migrated to TypeScript in your work, but if you have any thoughts about what I've been doing with ES6 here, please let me know. |
What would the difference be between shipping A major potential benefit of a proper ES6 module implementation would be tree shaking. I should be able to import just specific parts of Cesium, and have the rest left out of my final app bundle. Shoving everything into an "ES6 roll-up build" seems like it would likely make that very difficult. If the entire codebase is ES6 modules already, pointing the default entry point to I do agree that shrinking the published NPM package would be very nice. The Cesium NPM tarball is currently around 25MB, which is gigantic relatively to other packages. |
That's a good point, that's actually what I'm doing in CesiumViewer and then setting I assumed the same setting would work if I built a combined ES6 version of rollup, but I just tried it and it's not smart enough to eliminate the unused code in that case. So for code size and tree-shaking, we actually don't want to combine at all. So the answer is to ship some form of the Source directory (maybe renamed and streamlined) and the code would end up looking like: import {
ArcGISTiledElevationTerrainProvider,
Math as CesiumMath,
Viewer
} from './Source/Cesium.js'; Or whatever the directory gets named. I'm not sure the npm tarball will shrink much, if at all. But the release zip we put up on GitHub and the website might. The main goal is ES6, but there's definitely room for a separate task to look at file sizes and what else we're including in npm that may not need to be there. |
I was wrong, master is correct and clocks in at 25MB. However the ES6 port should shrink this by 9MB because our workers now share code. |
What @markerikson said. It needs to be separate modules for tree-shaking to work correctly. |
So the Assuming all goes well have the final PR open for review by tomorrow (though we'll hold off on merging until post-Oct 1 release). For anyone that wants to check it out in the mean time, either sync up that branch or just grab this release zip or point to this npm package Any and all feedback is appreciated. |
Thanks @mramato, I'm in the process of trying it out with Terria now and will let you know how it goes. All looks good so far though. |
Awesome, thanks. I just pushed fixes for running our tests against built Cesium (rollup is awesome) so I think I'm actually "finished". I'm going to open a new PR with a full summary of everything I did so people can actually review the code. I also have another list of ideas for future clean-up, stuff that's not ES6 related directly, but things we can now do because of it. |
@mramato Looking good so far over here. What is the purpose of the WorkersES6 folder? Future-proofing for when workers are able to |
@mramato Might I also recommend you create a HelloWorld es6 version with:
|
WorkersES6 is the actual source code for our workers. Porting them to ES6 was needed so that they can import and use modules from Core/Scene/etc.. The |
@mramato Makes sense. |
I definitely think we want a basic ES6 example somewhere, but I'm probably going to get the main PR into master first and then have a separate PR to clean up packaging/examples/etc.. I may even create a separate repository for rollup similar to what we have for webpack (which will need to be updated as well. |
@mramato I think now that you have everything as es6 modules, you shouldn't need separate repos for each build system. In any case, you did a great job, this stuff looks amazing and certainly makes life much easier! |
Agreed, but I just want to make sure we have good doc for best practices when working with Cesium, might just be a blog post or tutorial instead of a separate repository. Because you need to copy Assets/Workers/etc.. and often set CESIUM_BASE_URL, a lot of users struggle the first time they go through the process and I feel like we've never done a good enough job documenting it. |
#8224 is open! |
Great work. Looking forward to seeing the results! |
While there was some discussion in #2524 related to ES6 module support, we never actually wrote up an issue to track it specifically. Below is my current best guess for a list of tasks we'll need to implement and questions to sort out. This is a living document, so I expect to update this description as we learn more.
Assumptions
Prep work
Actual migration
es6-playground
branch)build
task should output shaders and Cesium.js as ES6.Workers
toWorkersES6
andWorkers/Build
toWorkers
Open Questions
The text was updated successfully, but these errors were encountered: