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

[WIP] Convert to ESM and ES classes #438

Closed
wants to merge 28 commits into from

Conversation

trusktr
Copy link

@trusktr trusktr commented Aug 31, 2021

  • Do the similar for roslib. Converted to ESM in the roslib PR.
  • Make GitHub releases contain build artifacts (or open a new issue for it and consider the task here done).

fixes #314

@trusktr trusktr changed the title Convert to es classes Convert to ES classes Aug 31, 2021
Gruntfile.js Outdated Show resolved Hide resolved
This was referenced Aug 31, 2021
@trusktr trusktr force-pushed the convert-to-es-classes branch from 89f0bb6 to cd8afba Compare September 10, 2021 21:25
@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

@MatthijsBurgh Merged develop, updated eslint (no errors on class fields or for-of), tests passing, build updated.

@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

My longer-term plan is

  • convert to TypeScript (which is compilable to WebAssembly with AssemblyScript)
  • once I finish GLAS (a port of Three.js to AssemblyScript) and asdom (DOM bindings for AssemblyScript) then I can compile ros3d to Wasm.

@@ -3,16 +3,15 @@ const rollup = require('rollup');
// plugin that transpiles output into commonjs format
const commonjs = require('@rollup/plugin-commonjs');
// plugin that transpiles es6 to es5 for legacy platforms
const buble = require('@rollup/plugin-buble');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code output is cleaner without Buble, and all browser nowadays support all the syntax being used (I've been using these ES module output at NASA with no issues). This eliminated errors Buble was having on newer syntax that I didn't want to spend time configuring (by wasting time adding plugins to acorn) when all browsers already support the syntax out of the box.

…tEmitter2 into the repo shims/ folder and convert it to an ES Module).

It would be better to use something else other than EventEmitter2, which is 1600 lines of needless code just for an event pattern. @lume/eventful is only 100 lines of documented code.
@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

I updated the code to be compatible with native ES Modules (and it now requires a very minimal import-map.json at runtime). To do this, I had to move the needlessly-large EventEmitter2 class into the shims/ folder (I suggesting replacing those 1600 lines of code with the 100 lines of code (half of which is documentation) of the Eventful class from @lume/eventful, but that would be for a breaking release).

The .js file extensions in the import statements are needed for two reasons:

  • native ES Module systems (f.e. web browsers) do not automatically append .js extensions to import specifiers. Import specifiers in browsers are simply URLs relative to the current file. Importing path/to/foo will not automatically make a network request for current-file-folder/path/to/foo.js which means trying to download current-file-folder/path/to/foo will fail if that file doesn't exist on the server
  • static file servers could be modified to automatically look for a file after adding the .js extension in their implementation, but in practice the average static file server does not do this (they are not js-specific static file servers).

So, by placing .js file extensions in the import specifiers, this makes it easiest to get things working in a native ES Module system without the following workarounds:

  • add a build step that appends .js to import specifiers, or
  • use a special server that appends .js extensions during it's file lookup.

@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

Still need to run examples to make sure I didn't break something.

@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

Added a TODO for roslib. It is not yet classes, and not yet ES Module friendly. I'll do the same thing there.

@trusktr
Copy link
Author

trusktr commented Sep 11, 2021

roslib got in the way of making this native ES Module friendly. I am going to update roslib to ES Modules first. The good thing is ES Modules are here to stay, officially.

@trusktr
Copy link
Author

trusktr commented Sep 12, 2021

WIP PR in roslib: RobotWebTools/roslibjs#475

It is possible we can complete this PR without the roslib PR (it just won't have browser-ESM compatible source files, but the built .esm.js bundle will work).

@trusktr trusktr marked this pull request as draft September 12, 2021 03:17
@trusktr trusktr changed the title Convert to ES classes [WIP] Convert to ES classes Sep 12, 2021
@trusktr trusktr changed the title [WIP] Convert to ES classes [WIP] Convert to ESM and ES classes Oct 1, 2021
@trusktr
Copy link
Author

trusktr commented Oct 1, 2021

What are your thoughts on that last commit (ignore build output)?

f56c58a

Please let me know if that's ok or if I should keep build output (the reasoning is in the commit message).

…asses

* upstream/fix_440:
  Fix RobotWebTools#440, correctly delete old grid
  Add missing semi-colon
  Update Build
  Bump rollup from 2.56.3 to 2.57.0 (RobotWebTools#443)
  Bump mocha from 9.1.1 to 9.1.2 (RobotWebTools#442)
  Bump @rollup/plugin-node-resolve from 13.0.4 to 13.0.5 (RobotWebTools#441)
@MatthijsBurgh
Copy link
Contributor

What are your thoughts on that last commit (ignore build output)?

f56c58a

Please let me know if that's ok or if I should keep build output (the reasoning is in the commit message).

I think that is OK. I think a repo should never contain compiled files. But these were already there, when I joined. So I kept them.

Might be good that the build files are added as artifacts to releases.

@trusktr
Copy link
Author

trusktr commented Oct 5, 2021

Might be good that the build files are added as artifacts to releases.

That's true. For that, I think we can automate it.

  • Make GitHub releases contain build artifacts (or open a new issue for it and consider the task here done).

options = options || {};
var path = options.path || '/';
var resource = options.resource;
var material = options.material || null;
Copy link
Author

@trusktr trusktr Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not being used anywhere. I noticed as I was looking at build output that Rollup strips the var material so it becomes a floating expression (not used for anything, but the expression remains because there could be an unknown side effect from reading the property).

…, or they end up installing a second version of Three.js which can lead to issues where singletons are expected, or they'll copy and fork a local ros3d lib, etc.

function onTouchEnd(event3D) {
const onTouchEnd = (event3D) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a bug: the functions were passed unbound as event handlers further below, so this would not work inside of them.

We need unit tests! ros3d is hardly covered.

// (I've seen this problem happen in real-world ROS3D projects, especially with
// roboticists or data scientists that are inexperienced with web
// development).
globalThis.THREE = THREE;
Copy link
Author

@trusktr trusktr Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, this solves issues like #314 by allowing people to use ROS3D's THREE, rather than them trying to import an external THREE instance and causing issues. In a following update, this should be improved to allow more use cases such as supplying THREE from outside or as a peer dependency via npm.

@trusktr
Copy link
Author

trusktr commented Oct 6, 2021

Tests are passing in Node 16. There appears to be some difference between ESM in NOde 14 and 16 that makes tests fail in Node 14.

We can try to fix it, or we can also move forward with Node 16 (this whole change will be a major version bump anyway).

https://github.com/trusktr/ros3djs/runs/3809448165?check_suite_focus=true

k-aguete pushed a commit to k-aguete/ros3djs that referenced this pull request Oct 21, 2022
* Widen package requirements

As suggested in RobotWebTools/roslibjs#437 (comment)

* Fix wrong socket.io version
@haoming29
Copy link

Any updates regarding this PR? Thanks

@MatthijsBurgh
Copy link
Contributor

Stale

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.

Issue using GLTFLoader and multiple THREE instances
3 participants