5

I need to generate JavaScript (ECMAScript) code from inside a Java program. For that, I am looking for something like JavaPoet, but producing JavaScript as output.

I cannot use one of these transpilers that translates another language into JavaScript (e.g. GWT is not the answer) nor a tool that generates JavaScript from a syntax tree (only when there is a library that helps building that syntax tree ...).

Something like the already mentioned JavaPoet would be the answer because it has a very small footprint both in memory usage and in code size.

Target for the resulting JavaScript code is Java/JSR 223 (Nashorn), if this would be relevant.

To specify the requirements: JavaPoet uses this code

MethodSpec main = MethodSpec.methodBuilder("main")
    .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
    .returns(void.class)
    .addParameter(String[].class, "args")
    .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
    .build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
    .addMethod(main)
    .build();

JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld)
    .build();
javaFile.writeTo(System.out);

to create this Java code

package com.example.helloworld;

public final class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, JavaPoet!");
  }
}

(I've copied the sample from the JavaPoet project web site.)

I would like to have something similar that creates JavaScript code instead.

To my current knowledge, StringBuilder is in fact closest to this requirement. First generating Java to transform it to JavaScript should work, but looks really odd to me - and is all, but not really lightweight. Same as first generate Kotlin or Ceylon code and then transforming it to JavaScript.

tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • Perhaps `StringBuilder`? No, honestly, your requirements are not clear. – Axel Apr 27 '17 at 14:17
  • Kotlin language and also Ceylon lanugage can compile to both JVM and Javascript, but i wonder if it will meet your need. – pdem Apr 27 '17 at 14:21
  • +1 I'm looking for the same as you! I'm expanding this [restapi-codegen-maven-plugin](https://github.com/jansoren/restapi-codegen-maven-plugin) – Jan-Terje Sørensen Jul 11 '17 at 19:20
  • 1
    If you ever find a JavaPoet-like library for JavaScript, I'd love to hear about it. – Jorn Apr 20 '18 at 11:11

1 Answers1

0

I know you don't want a transpiler, but since it seems that there is no javascript generator for what you want, probably a light-weight java-js transpiler could do the job.

Try to produce some java with javapoet as you mentioned and then make the output pass through the jsweet transpiler, there is an online sandbox that you can use to see the output before deciding whether it is the best option.

Mcm
  • 109
  • 1
  • 8
  • Unfortunately, jsweet translates `System.out.println( "Hello World" );`to `console.info( "Hello World" );` - and that does not work in Nashorn. Basically, it looks like that the jsweet approach requires to use jsweet specific Java APIs to get quite complex JavaScript as output - that collides with my lightweight requirement. – tquadrat Apr 28 '17 at 23:33
  • Yep, it's thought for browser/v8 as runtime. Hopefully google open-sources their new gwt compiler (j2cl) soon, which should be another light transpiler and maybe does the work you need. – Mcm May 02 '17 at 07:11
  • 1
    I contributed to JSweet and it is now totally flexible in v2: it is quite easy to replace Sysout with something with a Nashorn compliant "print" AFAIK from jsweet, the idea is that simple Java should transpile to simple Javascript. That being said, if you want to write your own printer, JSweet API (v2) is definitely something you might want to try. It is now highly extensible. e.g. https://github.com/cincheo/jsweet/blob/master/doc/jsweet-language-specifications.md#extending-the-transpiler – Louis GRIGNON Aug 16 '17 at 20:39