reactiverse / es4x

🚀 fast JavaScript 4 Eclipse Vert.x
https://reactiverse.io/es4x/
Apache License 2.0
883 stars 75 forks source link

Is it possible to use es4x in maven project? #223

Closed chengenzhao closed 5 years ago

chengenzhao commented 5 years ago

e.g. creating a javascript/ecmascript subfolder in the src/main and add npm dependencies in the pom.xml...

pmlopes commented 5 years ago

I don't see why not. There a few issues to solve though.

  1. Create a pom with the needed dependencies + es4x.
  2. Make the main class io.reactiverse.es4x.ES4X
  3. all sources should be in src/main/resources or configure maven to use other directory for resources.
  4. you still need to get the npm packages associated with the maven packages (the reverse of what the installer does) and ensure they are in src/main/resouces/node_modules

If you build the app it should probably work as a fatjar on graalvm. On jdk11 the graaljs artifacts are not shade friendly so you probably need a directory with all jars.

pmlopes commented 5 years ago

Perhaps we could add a "reverse-install" command. If we exclude dependency resolution, the npm packages can be referred by curl as:

GROUP=@vertx
ARTIFACT=core
VERSION=3.8.2
https://registry.npmjs.org/${GROUP}${ARTIFACT}/-/${ARTIFACT}-${VERSION}.tgz

So now from maven we would just need to get the resolved dependencies and then download the npm files.

An alternative would be use webjar's where they do this and package the npm packages into maven central.

pmlopes commented 5 years ago

After giving it a bit more though, a better solution would be pushing a big jar with all generated js code to maven. Then any pom/Gradle project could be used as usual by adding this dependency plus the required vertx dependencies.

pmlopes commented 5 years ago

I think this could solve it: https://github.com/reactiverse/es4x/pull/225

chengenzhao commented 5 years ago

wow looks great If we could use es4x in maven then it would be easy for us to program polyglotly

pmlopes commented 5 years ago

I think it needs some more tweaks and will try to push a release to maven this week

pmlopes commented 5 years ago

This has been released, add the following dependency to your pom:

    <dependency>
      <groupId>io.reactiverse</groupId>
      <artifactId>es4x-vertx-stack</artifactId>
      <version>0.9.4</version>
    </dependency>

Create a project as usual and a verticle in js:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
  <artifactId>myapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>es4x_maven</name>

  <properties>
    <vertx.verticle>verticle.js</vertx.verticle>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.testSource>1.8</maven.compiler.testSource>
    <maven.compiler.testTarget>1.8</maven.compiler.testTarget>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-stack-depchain</artifactId>
        <version>3.8.2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web</artifactId>
    </dependency>
    <dependency>
      <groupId>io.reactiverse</groupId>
      <artifactId>es4x-vertx-stack</artifactId>
      <version>0.9.4</version>
    </dependency>
  </dependencies>

  <build>
     <resources>
     <resource>
       <directory>src/main/resources</directory>
     </resource>
     <resource>
       <directory>src/main/javascript</directory>
     </resource>
   </resources>

    <plugins>
      <plugin>
        <groupId>io.reactiverse</groupId>
        <artifactId>vertx-maven-plugin</artifactId>
        <version>1.0.18</version>
        <executions>
          <execution>
            <id>vmp</id>
            <goals>
              <goal>initialize</goal>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <redeploy>true</redeploy>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The verticle: src/main/javascript/verticle.js:

/// <reference types="@vertx/core/runtime" />
// @ts-check
import { Router } from '@vertx/web';

const app = Router.router(vertx);

app.route('/').handler(ctx => {
  ctx.response()
    .end('Hello from Vert.x Web!');
});

vertx.createHttpServer()
  .requestHandler(app)
  .listen(8080);

console.log('Server listening at: http://localhost:8080/')
chengenzhao commented 4 years ago

Hi:

Today we tried to package this project and then run it by using command: path-to-graal/graalvm-ce-java11-19.3.0/Contents/Home/bin/java -jar myapp-0.0.1-snapshot.jar

but it failed we got:

Failed in deploying verticle caused by ReferenceError: global is not defined at :program(file:/Users/chengen/Downloads/myapp-0.0.1-snapshot.jar!/io/reactiverse/es4x/polyfill/global.js:152:3840-3845) at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:344) at io.reactiverse.es4x.Runtime.(Runtime.java:116) at io.reactiverse.es4x.ECMAEngine.newContext(ECMAEngine.java:209) at io.reactiverse.es4x.ESVerticleFactory.createRuntime(ESVerticleFactory.java:63) at io.reactiverse.es4x.ESVerticleFactory.createVerticle(ESVerticleFactory.java:88) at io.vertx.core.impl.DeploymentManager.createVerticles(DeploymentManager.java:234) at io.vertx.core.impl.DeploymentManager.lambda$doDeployVerticle$2(DeploymentManager.java:207) at io.vertx.core.impl.FutureImpl.setHandler(FutureImpl.java:80) at io.vertx.core.impl.DeploymentManager.doDeployVerticle(DeploymentManager.java:174) at io.vertx.core.impl.DeploymentManager.doDeployVerticle(DeploymentManager.java:147) at io.vertx.core.impl.DeploymentManager.deployVerticle(DeploymentManager.java:136) at io.vertx.core.impl.VertxImpl.deployVerticle(VertxImpl.java:711) at io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer.deploy(VertxIsolatedDeployer.java:42) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at io.vertx.core.impl.launcher.commands.ClasspathHandler.deploy(ClasspathHandler.java:168) at io.vertx.core.impl.launcher.commands.RunCommand.deploy(RunCommand.java:403) at io.vertx.core.impl.launcher.commands.RunCommand.run(RunCommand.java:270) at io.vertx.core.impl.launcher.VertxCommandLauncher.execute(VertxCommandLauncher.java:248) at io.vertx.core.impl.launcher.VertxCommandLauncher.dispatch(VertxCommandLauncher.java:402) at io.vertx.core.impl.launcher.VertxCommandLauncher.dispatch(VertxCommandLauncher.java:346) at io.vertx.core.Launcher.main(Launcher.java:45)

so any idea how to fix this? and I think I have set the path & java_home to the right path

chengenzhao commented 4 years ago
Screen Shot 2019-11-22 at 9 16 22 PM
pmlopes commented 4 years ago

0.10.0 is pushed to maven and fixes the issue.