Fork me on GitHub

Getting started

Step 1: Install (or check that you have installed) Git, Node.js and Maven (commands git, node, npm and mvn should be in your path). Java JDK 8 is required (check the version with java -version).

Step 2: Clone the jsweet-quickstart project from Github

> git clone https://github.com/cincheo/jsweet-quickstart.git

Step 3: Run the transpiler to generate the JavaScript code

> cd jsweet-quickstart
> mvn generate-sources

Step 4: Check out the result in your browser

> firefox webapp/index.html

Step 5: Edit the project and start programming

  • Checkout the examples to see various use cases
  • Read the following sections to learn more about candies and how to use them and generate bundles
  • Refer to the language specifications to know more about programming with JSweet
  • Eclipse users: install the Eclipse plugin to get inline error reporting, build-on-save, and easy configuration UI

Candies

A JSweet candy is a Maven artifact that corresponds to a Java/JavaScript library/framework/API, which you can use in your JSweet application simply by adding a dependency to it in your pom.xml.

All you need to know about using existing candies (or creating your own) is summarized here.

Run your programs for the WEB

Candies are just typed definitions required for Java compilation (pure APIs). So, when running your program in a browser, you must not forget to include the JavaScript bundles that correspond to the candies you are using.

When having a lot of files in a program, it is useful to create a bundle so that you don’t need to include your files one by one. Also bundling your programs will take care of the dependencies and avoid the problem of referencing a class or a variable that has not been defined yet. To enable JSweet bundling, you just need to set the bundle option to true in your pom.xml. Check out the Maven plugin documentation for more information on available options.

<project ...>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.jsweet</groupId>
        <artifactId>jsweet-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
           <sourceMap>true</sourceMap>
           <outDir>target/js</outDir>
           <targetVersion>ES3</targetVersion>
           <!-- bundle your JS automatically -->
           <bundle>true</bundle>
           <!-- optionally, you can ask JSweet to put your bundle in a specific directory -->
           <bundlesDirectory>target/js/dist</bundlesDirectory>
           <!-- end of bundle configuration -->
        </configuration>
        ...
      </plugin>
...

Finally, you can use an external bundler, such as Browserify, to create bundles. Bundlers usually require the use of modules (for instance Browserify works with commonjs modules). See the next section to learn how to build your application with modules.

Run with Node.js

To launch your application with Node.js, you just need to use the module option, and select the commonjs module kind. Our candy repository contains all basic node packages (node, http, express, …), which are ready to use and just need to be included in your pom.xml. To run your application with node (assuming that you do not use the DOM and that you have installed the right packages with npm), just type the command line:

> node path/to/Main.js

Where Main.js is the entry point of your program, i.e. the module that corresponds to the Java class where the main method is defined.

Transpiler options

JSweet transpiler provides the following options. You can modify your pom.xml to tune way your files are transpiled.

Name Type Values Default Example
targetVersion enum ES3, ES5, ES6 ES3 <targetVersion>ES3</targetVersion>
module enum commonjs, amd, system, umd none <module>commonjs</module>
outDir string JS files output directory .jsweet/js <outDir>js</outDir>
tsOut string Temporary TypeScript output directory .jsweet/ts <tsOut>temp/ts</tsOut>
tsOnly boolean If true, JSweet will not generate any JavaScript false <tsOnly>true</tsOnly>
includes string[] Java source files to be included N/A <includes><include>**/*.java</include></includes>
excludes string[] Source files to be excluded N/A <excludes><exclude>**/lib/**</exclude></excludes>
bundle boolean Concats all JS file into one bundle false <bundle>true</bundle>
bundlesDirectory string JS bundles output directory N/A <bundlesDirectory>js/dist</bundlesDirectory>
sourceMap boolean In-browser debug mode – true for java, typescript else true <sourceMap>true</sourceMap>
encoding string Java files encoding UTF-8 <encoding>ISO-8859-1</encoding>
noRootDirectories boolean Output is relative to @jsweet.lang.Root package’s directories false <noRootDirectories>true</noRootDirectories>
enableAssertions boolean Java assert statements are transpiled as JS check false <enableAssertions>true</enableAssertions>
verbose boolean Verbose transpiler output false <verbose>true</verbose>
jdkHome string Alternative JDK >= 8 directory, for instance if running Maven with a JRE ${java.home} <jdkHome>/opt/jdk8</jdkHome>
declaration boolean Generates TypeScript d.ts false <declaration>true</declaration>
dtsOut string TypeScript d.ts output directory when the declaration option is true outDir <dtsOut>typings</dtsOut>
candiesJsOut string Directory where to extract candies’ Javascript <candiesJsOut>www/js/candies</candiesJsOut>
definitions boolean Generates only definitions from def.* packages in d.ts definition files, in the tsOut directory (do not confuse with the ‘declaration’ option) false <definitions>true</definition>
disableJavaAddons boolean Disables Java-specific code generation behavior (for advanced users only) false <disableJavaAddons>true</disableJavaAddons>