Our great sponsors
- Onboard AI - Learn any GitHub repo in 59 seconds
- InfluxDB - Collect and Analyze Billions of Data Points in Real Time
- SaaSHub - Software Alternatives and Reviews
-
Google loves Bazel (yeah, a monorepo, reproducible and stable builds and so on), we usually use Maven or Gradle for our projects. Luckily, we can try j2cl-maven-plugin that is developed by Colin Alworth from Vertispan, but for now let’s try the Bazel way.
-
First steps with Bazel are pain. What’s good, we can reuse 2 samples provided by the J2CL project, these 2 demos are more than enough to get us started. In short, the most important Bazel rules for us are j2cl_library and j2cl_application that can be used to set groups of files and transpile them into an executable JavaScript file. But for it to work one more step is required: we need to define an entry point for our application. It can be done in 2 ways: we can define it with a handwritten JavaScript script like it works in J2CL demos, or we can generate it with an annotation processor I wrote for this purpose, feel free to take a look at the demo too.
-
Onboard AI
Learn any GitHub repo in 59 seconds. Onboard AI learns any GitHub repo in minutes and lets you chat with it to locate functionality, understand different parts, and generate new code. Use it for free at www.getonboard.dev.
-
OK, so far so good. My favorite IDE is IntelliJ IDEA that has an official Bazel plugin by Google, but if you prefer VS Code you could try a plugin developed by salesforge.
-
To work with browser APIs from Java we would use Elemental2, which is abstractions for JavaScript APIs, the set of Java wrappers based on the Closure Compiler externs.
-
Externs are type definitions of, for instance, Browser APIs like HTML elements or WebGL classes, pretty much the same as d.ts, that helps Closure Compiler to recognize the types. By the way, Elemental2 libraries were generated from the built-in Closure Compiler externs using the JsInterop generator. Elemental2 does not cover the whole set of Web APIs, but it’s not too difficult to write missing parts like I did in my research project.
-
Externs are type definitions of, for instance, Browser APIs like HTML elements or WebGL classes, pretty much the same as d.ts, that helps Closure Compiler to recognize the types. By the way, Elemental2 libraries were generated from the built-in Closure Compiler externs using the JsInterop generator. Elemental2 does not cover the whole set of Web APIs, but it’s not too difficult to write missing parts like I did in my research project.
-
TIP: Peter Donald is working on an impressive project Akasha that aims at creating a unified set of APIs generated from the latest WebIDL specs. It’s kept mostly up-to-date but is incompatible with Elemental2 (as a matter of fact, it’s incompatible with the built-in Closure externs).
-
InfluxDB
Collect and Analyze Billions of Data Points in Real Time. Manage all types of time series data in a single, purpose-built database. Run at any scale in any environment in the cloud, on-premises, or at the edge.
-
So it looks like we have pretty much everything except for the most important part: three.js that we can use from Java. And it’s a little bit of a complex part. To interop with JavaScript we should use J2CL JsInterop API, but there is a little problem: Closure Compiler must be able to recognize types of (most of the) three.js objects. Here we have two options:
-
Well, sounds nice, writing externs from scratch for such a huge project like three.js is an overwhelming task for one person. But there was a little problem: how to convert d.ts to externs? The good news: we can use Tsickle, the project aimed at generating externs from d.ts.
-
The source code is published here: https://github.com/treblereel/bazel_three_demo
-
TIP: Some time ago I ported Quake2 by id Software to J2CL using j2cl-maven-plugin with Quarkus backend, so you can take a look, it rocks!
Related posts
- J2CL – Java to Closure JavaScript Transpiler Used by Gmail and Docs
- Creating a incremental game in Java
- When this will come to Java or when will the work start for this?
- is there a way to create a java library with JS/TS bindings that can be published to npmjs ?
- I like to discuss ui frameworks for java backend?