Skip to content
muddletoes edited this page Jan 22, 2015 · 4 revisions

Node loader

If you'd like to skip using the sjs command to compile your sweet.js code, you can use the node loader. This allows you to require sweet.js files that have the .sjs extension:

var sjs = require('sweet.js'),
    example = require('./example.sjs');

example.one;

Where ./example.sjs contains:

//  example.sjs
macro id {
    rule { ($x) } => {
        $x
    }
}

exports.one = id (1);

Note that require('sweet.js') must come before any requires of .sjs code. Also note that this does not import any macros, it just uses sweet.js to compile files that contain macros before requiring them. If you're looking to modularize macros check out the modules wiki page.

Alternatively, you can use sweet.loadMacro to achieve a similar effect to the --module command line flag:

var sweet = require('sweet.js');
// load all exported macros in `macros/str.sjs`
sweet.loadMacro('./macros/str');
// test.sjs uses macros that have been defined and exported in `macros/str.sjs`
require('./test.sjs');

This is basically equivalent to running sjs --module ./macros/str test.sjs.

Clone this wiki locally