Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installed jSweet. Now what?

I have a java project which I want to convert into Typescript (about 150 Files).

As per http://www.jsweet.org/getting-started/

...I checked out the jsweet Github project.

...I ran mvn generated-src

...I loaded index.html and it gave me 'It works!'

Now what? It is very unclear what happens next.

Ideally I would assume there is some runnable file eg. converter.java, where you would specify an input and an output directory. I can't see this being explained anywhere.

like image 451
Oliver Watkins Avatar asked Dec 24 '22 02:12

Oliver Watkins


1 Answers

I think that this question should be "How to transpile a Java project to TypeScript with JSweet".

DISCLAMER: JSweet's main purpose is not to transform existing Java programs to TypeScript. However, since JSweet produces intermediate TypeScript code, it could be used as a helper to migrate Java programs to TypeScript. Of course, this migration will fully work only if the Java libraries are also available in JavaScript, otherwise, you will have to provide an implementation or manually change the used libraries to existing JavaScript ones.

You can indeed start from the the QuickStart project on Github. When you run the mvn generate-sources, what happens is that the Java sources in src/main/java are transpiled to TypeScript. The JSweet generation process is configured in the pom.xml file with the Maven JSweet plugin. Here you can tell JSweet what to generate and where. See the full option list of the plugin here. So, according to the pom.xml file, JSweet is configured as:

        <plugin>
            <groupId>org.jsweet</groupId>
            <artifactId>jsweet-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <verbose>true</verbose>
                <tsOut>target/ts</tsOut>
                <outDir>target/js</outDir>
                <candiesJsOut>webapp</candiesJsOut>
                <targetVersion>ES3</targetVersion>
            </configuration> 
            [...]

Because of the tsOut option, you will find the generated TypeScript code in the target/ts directory.

So, in order to translate a full Java program from there, you need to copy-paste your Java source code within the src/main/java. Then, run again mvn generate-sources.

Please note that for this command to succeed, your Java files first need to compile from a Java standpoint. It means that if your Java source files use other Java libraries, these must be available in your classpath. So, like any Java project under Maven, you need to set up a <dependencies> section in your pom.xml.

Using external Java libraries can be a problem, because these Java libraries are probably not available in JSweet/TypeScript. So, it likely that you will get the TypeScript files generated in your target/ts directory, but that the transpilation will report many errors because of use of non-existing APIs in TypeScript.

From there, you can either:

  • Take the TypeScript code as is and finish the transformation manually (which means that you need to refactor your code to use valid TypeScript APIs rather than Java ones). This is probably you preferred option if you want to switch to TypeScript and trash the Java source code base.
  • Provide a JSweet implementation of the Java APIs you are using, in a similar way the J4TS project does. This video shows a proof-of-concept on how to implement the applet API in JSweet. This can be a good option if you want to remain working in Java with JSweet and use the Java tooling to safely and gradually migrate to the Web.
like image 116
Renaud Pawlak Avatar answered Jan 15 '23 20:01

Renaud Pawlak