Skip to content
Srikumar edited this page Jun 2, 2016 · 3 revisions

Modules

Note: This doc on modules is obsolete. Please see Sweet Modules

Using modules

At the moment sweet.js supports a primitive form of module support with the --module flag.

For example, if you have a file macros.js that defines the m macro:

// macros.js

macro m { /* ... */ }
export m;

and my_sweet_code.js uses m:

// my_sweet_code.js

m 42

You would compile this with:

$ sjs --module ./macros.js  my_sweet_code.js 

Note that modules must use the export keyword. This allows modules to define "private" macros that are not visible to the main code.

The --module flag uses the node path to look up the module file so you can publish and use macro modules on npm. Checkout lambda-chop for an example of this.

Limitations

The biggest limitation with the current approach is that you can't arbitrarily interleave importing compile-time values (macros) and run-time values (functions). This will eventually be handled with support for "proper" modules (issue #43).

Clone this wiki locally