cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 161 forks source link

Question: correct way to transpile Java API to Javascript module? #666

Closed moksamedia closed 3 years ago

moksamedia commented 3 years ago

Hello,

I am trying to transpile a Java API (from this page) to a Javascript module that I can use in Node.

I've managed to get it to transpile without errors. However, when I import the module into a Node script, I keep getting ReferenceError: java is not defined errors.

I tried manually requiring the e4ts.js distribution, but this doesn't seem to work. I also tried using the NPM dependency.

What is the correct way to go about this?

My goal is to transpile the Java APIs to Javascript APIs that I can use in a node project and Vue.js client.

Thanks for your time.

Java version 1.8 Node version 12.14

andrewcarterhughes@pop-os: java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
andrewcarterhughes@pop-os: node -v
v12.14.0

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myCandyGroup</groupId>
  <artifactId>myCandy</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <properties>
        <jsweet.transpiler.version>1.1.1</jsweet.transpiler.version>
    </properties>
    <repositories>
        <repository>
            <id>jsweet-central</id>
            <name>libs-release</name>
            <url>http://repository.jsweet.org/artifactory/libs-release-local</url>
        </repository>
        <repository>
            <snapshots />
            <id>jsweet-snapshots</id>
            <name>libs-snapshot</name>
            <url>http://repository.jsweet.org/artifactory/libs-snapshot-local</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>jsweet-plugins-release</id>
            <name>plugins-release</name>
            <url>http://repository.jsweet.org/artifactory/plugins-release-local</url>
        </pluginRepository>
        <pluginRepository>
            <snapshots />
            <id>jsweet-plugins-snapshots</id>
            <name>plugins-snapshot</name>
            <url>http://repository.jsweet.org/artifactory/plugins-snapshot-local</url>
        </pluginRepository>
    </pluginRepositories>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jsweet</groupId>
                <artifactId>jsweet-maven-plugin</artifactId>
                <version>2.3.7-SNAPSHOT</version>
                <configuration>
                    <!-- <verbose>true</verbose> -->
                    <bundle>true</bundle>
                    <declaration>true</declaration>
                    <outDir>target</outDir>
                    <dtsOut>target</dtsOut>
                    <targetVersion>ES3</targetVersion>
                    <module>none</module>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-js</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>jsweet</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <echo message="copying generated bundles to dist..." />
                                <copy
                                    file="src/main/resources/META-INF/resources/webjars/${project.artifactId}/${project.version}/bundle.js"
                                    tofile="dist/${project.artifactId}.js" verbose="true" />
                                <copy
                                    file="src/main/resources/src/typings/${project.artifactId}/${project.version}/bundle.d.ts"
                                    tofile="dist/${project.artifactId}.d.ts" verbose="true" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.jsweet
                                        </groupId>
                                        <artifactId>
                                            jsweet-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.1.0-SNAPSHOT,)
                                        </versionRange>
                                        <goals>
                                            <goal>jsweet</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.jsweet.candies/jsweet-core -->
        <dependency>
            <groupId>org.jsweet</groupId>
            <artifactId>jsweet-core</artifactId>
            <version>5.3.0</version>
        </dependency>

        <dependency>
                <groupId>org.jsweet</groupId>
                <artifactId>j4ts</artifactId>
                <version>0.5.0</version>
        </dependency>
    </dependencies>

</project>
moksamedia commented 3 years ago

Just noticed I got issue 666. Whoa. The demon issue. Haha.

lgrignon commented 3 years ago

Hello @moksamedia Did you take a look at https://github.com/lgrignon/j4ts-example ?

renaudpawlak commented 3 years ago

There are two ways to generate usable JavaScript code with JSweet.

  1. In a bundle: bundle=true, module=none. This way you should be able to use it from a node project as a non-node bundle. Apparently that how Louis is doing in the j4ts example. @lgrignon btw, I am not sur how your example finds the j4ts runtime? Is it bundled altogether in the final bundle?
  2. The more standard way for node (and npm) would be to use bundle=false, module=ES6. It is supposed to work, but it is complicated because you need to configure your modules. I plan to work again on this feature soon, since I have an ongoing project that requests it.
moksamedia commented 3 years ago

@renaudpawlak Do you mean module=ES6 or targetVersion=ES6?

moksamedia commented 3 years ago

@lgrignon I've looked at a number of different examples. All of them show how to build an entire Java application and transpile it into Javascript. I just want to build a self-contained API that I can use in an existing Node/Javascript codebase. How to do that is not super clear from that example.

moksamedia commented 3 years ago

If either of you have the time, I'd appreciate you looking at the project I'm working on to see if you see something obvious that needs to be fixed. I updated the j4ts.js and myCandy.js files to work more appropriately as modules (exporting the java object from the j4ts.js file and passing it into the myCandy.js file) and I'm on longer getting the java is not found error, but now I get a new error: TypeError.

Here is the project: https://github.com/moksamedia/ewts-jsweet

I'm using a relatively older version of JSweet with Java 8 because when I tried to update it to the latest version, I was unable to get it to transpile without errors (EwtsConverter.java(32,53)[ts] 'const' declarations must be initialized). Not sure why it worked with the older version and not the newer version.

This is the error:

TypeError: o1.compareTo is not a function
    at NaturalComparator.compare (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/j4ts.js:2419:31)
    at TreeMap.insert (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/j4ts.js:23838:38)
    at TreeMap.put$java_lang_Object$java_lang_Object (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/j4ts.js:23524:34)
    at TreeMap.put (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/j4ts.js:23516:33)
    at Function.TransConverter.addMapping (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:45:57)
    at Function.TransConverter.init (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:57:29)
    at Function.TransConverter.__static_initializer_0 (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:41:29)
    at Function.TransConverter.__static_initialize (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:17:29)
    at Function.TransConverter.replMapEwtsToAlalc_$LI$ (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:37:80)
    at module.exports (/home/andrewcarterhughes/Development/jsweet-candy-quickstart/node/myCandy.js:2714:25)
moksamedia commented 3 years ago

I actually think I got it working. But I would love to figure out how to fix the error that crops up when I try and use the updated version of the transpiler. This error: EwtsConverter.java(32,53)[ts] 'const' declarations must be initialized