cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 159 forks source link
compiler java javascript javascript-transpiler jsweet language transpiler typescript

JSweet: a Java to JavaScript transpiler

Continuous integration build Download

JSweet leverages TypeScript to write rich and responsive Web applications in Java through the use of JavaScript libraries and frameworks. With JSweet, Java programs are transpiled (source-to-source compiled) to TypeScript and JavaScript for being run in browsers, mobile Web views, or in Node.js.

How does it work? JSweet depends on well-typed descriptions of JavaScript APIs, so-called "candies", most of them being automatically generated from TypeScript definition files. These API descriptions in Java can be seen as headers (similarly to *.h header files in C) to bridge JavaSript libraries from Java. There are several sources of candies for existing libraries and you can easily build a candy for any library out there (see more details).

With JSweet, you take advantage of all the Java tooling (IDE's, Maven, ...) to program real JavaScript applications using the latest JavaScript libraries.

Java -> TypeScript -> JavaScript

Here is a first taste of what you get by using JSweet. Consider this simple Java program:

package org.jsweet;

import static jsweet.dom.Globals.*;

/**
 * This is a very simple example that just shows an alert.
 */
public class HelloWorld {
    public static void main(String[] args) {
        alert("Hi there!");
    }
}

Transpiling with JSweet gives the following TypeScript program:

namespace org.jsweet {
    /**
     * This is a very simple example that just shows an alert.
     */
    export class HelloWorld {
        public static main(args : string[]) {
            alert("Hi there!");
        }
    }
}
org.jsweet.HelloWorld.main(null);

Which in turn produces the following JavaScript output:

var org;
(function (org) {
    var jsweet;
    (function (jsweet) {
        /**
         * This is a very simple example that just shows an alert.
         */
        var HelloWorld = (function () {
            function HelloWorld() {
            }
            HelloWorld.main = function (args) {
                alert("Hi there!");
            };
            return HelloWorld;
        }());
        jsweet.HelloWorld = HelloWorld;
    })(jsweet = org.jsweet || (org.jsweet = {}));
})(org || (org = {}));
org.jsweet.HelloWorld.main(null);

More with the live sandbox.

Features

For more details, go to the language specifications (PDF).

Getting started

More info at http://www.jsweet.org.

Examples

Sub-projects

This repository is organized in sub-projects. Each sub-project has its own build process.

Additionally, some tools for JSweet are available in external repositories.

How to build

Please check each sub-project README file.

Contributing

JSweet uses Git Flow. You can fork this repository. Default branch is develop. Please use git flow feature start myAwesomeFeature to start working on something great :) When you are done, you can submit a regular GitHub Pull Request.

License

Please read the LICENSE file.