Skip to content

Minification and export rules

kazuho edited this page Apr 30, 2013 · 4 revisions

JSX supports minification as of commit dc3f7cc.

This document describes how to use the feature.

Turning on minification

Minification is turned on by using the --minify compiler flag.

jsx --release --minify myapp.jsx

Exporting classes and functions

It is possible to export a JSX class to JavaScript by using the export attribute. Exported functions cannot be overloaded; others should be marked as noexport.

// the class is exported
__export__ class ExportedClass {
    // only one constructor can be exported
    function constructor() {
    }
    // other constructors should be marked __noexport__
    __noexport__ function constructor(a : number) {
    }
    // properties not starting with "_" of a exported class are exported by default
    function f() {
    }
    // properties that need not be exported can be marked as such
    __noexport__ var n : number;
    // properties starting with "_" can also be exported by explicitly specifying as such
    __export__ var _n2 : number;
}

_Main.main(args : string[]) and _Test.test*() are automatically marked as __export__.

Obtaining a JSX Class from JavaScript

Code generated by the JSX compiler is stored in a single object called JSX. The object has a property called require that returns an object that maps the name of classes and their bodies given a name of a source file.

The following JavaScript code loads a class called ExportedClass defined in file exported.jsx, instantiates an object, and calls its method named f.

var ExportedClass = JSX.require("exported.jsx").ExportedClass;
var obj = new ExportedClass;
obj.f();