Skip to content
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

Update Cesium to ES6 modules #8192

Merged
merged 1 commit into from
Oct 3, 2019
Merged

Update Cesium to ES6 modules #8192

merged 1 commit into from
Oct 3, 2019

Conversation

mramato
Copy link
Contributor

@mramato mramato commented Sep 19, 2019

This is a draft pull request that will bring native ES6 modules to Cesium's code base. The general plan and roadmap are in #8122 and I'll be PRing into this branch as things progress.

I don't expect anyone to ever review this PR directly (until maybe the very end), it's just open to track the overall progress of the refactor.

@cesium-concierge
Copy link

Thanks for the pull request @mramato!

  • ✔️ Signed CLA found.
  • CHANGES.md was not updated.
    • If this change updates the public API in any way, please add a bullet point to CHANGES.md.
  • ❔ Unit tests were not updated.
    • Make sure you've updated tests to reflect your changes, added tests for any new code, and ran the code coverage tool.

Reviewers, don't forget to make sure that:

  • Cesium Viewer works.
  • Works in 2D/CV.
  • Works (or fails gracefully) in IE11.

@mramato
Copy link
Contributor Author

mramato commented Sep 25, 2019

This PR now contains both ES6 boilerplate and the new Sandcastle example boilerplate, all of which would just be noise if it were included in the main PR.

@OmarShehata
Copy link
Contributor

This should now be ready right @mramato ? I can do a final test and merge.

@mramato mramato marked this pull request as ready for review October 3, 2019 15:43
See #8224 for details.

eslint
There are a handful of new .eslintrc.json files, mostly to identify the files that are still AMD modules (Sandcastle/Workers). These are needed because you can't change the parser type with a comment directive (since the parser is the thing reading the file). We can finally detect unusued modules! So those have all been cleaned up as well.

requirejs -> rollup & clean-css
requirejs, almond, and karma-requirejs have all been removed. We now use rollup for building and minifying (via uglify) JS code and clean-css for css. These changes are fairly straight-forward and just involve calling rollup instead of requirejs in the build process.

Overall build time is significantly faster. CI is ~11 minutes compared to ~17 in master. Running makeZipFile on my machine takes 69 seconds compared to 112 seconds in master. There's probably plenty of room for additional optimization here too.

We wrote an published a small npm module, rollup-plugin-strip-pragma, for stripping the requirejs pragmas we use out of the release builds. This is maintained in the Tools/rollup-plugin-strip-pragma directory.

As for what we produce. The built version of Cesium is now a UMD module. So it should work anywhere that hasn't made the jump to ES6 yet. For users that were already using the "legacy" combined/minified approach, nothing changes.

One awesome thing about roll-up is that it compiles all of the workers at once and automatically detects shared codes and generates separate bundles under the hood. This means the size of our worker modules shrink dramatically and Cesium itself will load them much faster. The total minified/gzipped size of all workers in master is 2.6 MB compared to 225 KB in this branch! This should be most noticeable on demos like Geometry & Appearances which load lots of workers for the various geometry typs.

roll-up is also used to build Cesium Viewer, which is now an ES6 app.

We use clean-css via gulp and it is also a straightforward change from requirejs that requires no special mention.

Workers
While the spec allows for ES6 Web Workers, no browser actually supports them yet. That means we needed a way to get our workers into non-ES6 form. Thankfully, roll-up can generate AMD modules, which means we now have a build step to compile our Worker source code back into AMD and use the existing TaskProcessor to load and execute them. This build step is part of the standard build task and is called createWorkers. During development, these "built" workers are un-optimized so you can still debug them and read the code.

Since there is a build step, that means if you are changing code that affects a worker, you need to re-run build, or you can use the build-watch task to do it automatically.

The ES6 versions of Worker code has moved into Source/WorkersES6 and we build the workers into their "old home" of Source/Workers. cesiumWorkerBootstrapper and transferTypedArrayTest which were already non-AMD ES5 scripts remain living in the Workers directory.

Surprisingly little was changed about TaskProcessor or the worker system in general, especially considering that I thought this would be one of the major hurdles.

ThirdParty
A lot of our ThirdParty either already had a hand-written wrapper for AMD (which I updated to ES6) or had UMD which created problems when importing the same code in both Node and the browser. I basically had to update the wrapper of every third-party library to fix these problems. In some cases I updated the library version itself (Autolinker, topojson). Nothing to be too concerned about, but future clean-up would be using npm versions of these libraries and auto-generating the wrappers as needed so we don't hand-edit things.

Sandcastle
Sandcastle is eternal and manages to live another day in it's ancient requirejs/dojo 1.x form. Sandcastle now automatically uses the ES6 version of Cesium if it is available and fallsback to the ES5 unminified version if it is now. The built version of Sandcastle always uses CesiumUnminified, just like master. This means Sandcastle still works in IE11 if you run the combine step first (or use the relase zip)

Removed Cesium usage from Sandcastle proper, since it wasn't really needed
Generate a VERSION propertyin the gallery index since Cesium is no longer being included.
Remove requirejs from Sandcastle bucket
Update bucket to use the built version of Cesium if it is available by fallbackto the ES6 version during development.
Standalone.html was also updated
There's a bit of room for further clean-up here, but I think this gets us into master. I did not rename bucket-requirejs.html because I'm pretty sure it would break previously shared demos. We can put in some backwards compatible code later on if we want. (But I'd rather just see a full Sandcastle rewrite).

Specs
Specs are now all ES6, except for TestWorkers, which remain standard JS worker modules. This means you can no longer run the unbuilt unit tests in IE11. No changes for Chrome and Firefox.

Since the specs use ES6 modules and built Cesium is an ES5 UMD, I added a build-specs build step which generates a combined ES5 version of the specs which rely on Cesium as a global variable. We then inject these files into jasmine instead of the standard specs and everything works exactly as it did before. SpecRunner.html has been updated to inject the correct version of the script depending on the build/release query parameters.

The Specs must always use Cesium by importing Source/Cesium.js, this is so we can replace it with the built Cesium as describe above.

There's a bunch of room for clean-up here, such as unifying our two copies of jasmine into a single helper file, but I didn't want to start doing that clean-up as part of this already overly big PR. The important thing is that we can still test the built version and still test on IE/Edge as needed.

I also found and fixed two bugs that were causing failing unit tests, one in BingMapsImageryProviderSpec.js (which was overwriting createImage andnot setting it back) and ShadowVolumeAppearance.js (which had a module level caching bug). I think these may have been the cause of random CI failures in master as well, but only time will tell.

For coverage, we had to switch to karma-coverage-istanbul-instrumenter for native ES6 support, but that's it.

Finally, I updated appveryor to build Cesium and run the built tests under IE. We still don't fail the build for IE, but we should probably fix that if we want to keep it going.

NodeJS
When NODE_ENV is production, we now require in the minified CesiumJS directly, which works great because it's now a UMD module. Otherwise, we use the excellant esmpackage to load individual modules, it was a fairly straightforward swap from our old requirejs usage. We could probably drop esm too if we don't care about debugging or if we provie source maps at some point.
@mramato
Copy link
Contributor Author

mramato commented Oct 3, 2019

As discussed offline, I'm going to squash everything down into a single commit so that it's trivial to go back to "pre-ES6" in case we need to bisect/triage anything.

@mramato
Copy link
Contributor Author

mramato commented Oct 3, 2019

Okay, I force pushed the single commit. I also pushed the old branch to old-es6-staging just in case anything comes up that warrants me needing it (I'll delete it at some point).

@OmarShehata this is ready for merge.

@mramato
Copy link
Contributor Author

mramato commented Oct 3, 2019

Just realized I never updated CHANGES.md 🤣 but we have a blog and other info in the works, so I would prefer to wait until those are ready and then update CHANGES separately, since there's no way we could explain any useful information in a one line bullet.

@mramato
Copy link
Contributor Author

mramato commented Oct 3, 2019

@OmarShehata bump.

@OmarShehata
Copy link
Contributor

@mramato I'm just doing a final test on IE11/Edge. I'm seeing some unexpected behavior. The built Sandcastle/Viewer do work in both IE11 and Edge. But unbuilt Sandcastle seems to work in IE11 (I guess it will automatically use the built version then?) but does not work in Edge. It fails with:

Error loading http://localhost:8080/Apps/Sandcastle/load-cesium-es6.js

Is this expected?

@mramato
Copy link
Contributor Author

mramato commented Oct 3, 2019

  • IE11 requires the built version of CesiumJS (but not the built version of Sandcastle)
  • I guess Edge currently requires both the built version of CesiumJS and Sandcastle because Edge does not support dynamic ES6 modules. I may be able to fix this in a follow-up PR, but for now Edge does work with built Sandcastle. Edge is supposed to switch over to Chromium "any day now" but I'll see what we can do. I would not let the "Edge case" (see what I did there?) hold this up.

@OmarShehata
Copy link
Contributor

Sounds good, I think it's fine as well since it does work with the built Sandcastle. Feel free to just write up an issue that explains that IE11 & Edge now require built Cesium that we could point people to (although this just one of several things I assume the big ES6 blog will cover that we'll link to from CHANGES.md).

@OmarShehata OmarShehata merged commit d26ccfe into master Oct 3, 2019
@OmarShehata OmarShehata deleted the es6-staging branch October 3, 2019 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants