From the shell, I can call the Sweet.js compiler.
sjs -m macro-providing-module -o output-directory/file.js input-directory/file.sjs
How can I do the same from inside a Node.js module such that instead of outputting to a specified file, I get the compiled output as a string?
var sweetjs = require('sweet.js');
var input = require('fs').readSync('input-directory/file.sjs');
var module = 'macro-providing-module';
var output = sweetjs(/* ??? */);
Its undocumented because it will be changed/removed when sweet.js gets good module support (using ES6
import
s instead of command line flags). But for 0.4.0 you can use the following API to load macros from npm modules:The
root
argument is the location on the file system to start looking for node modules. Generally this isprocess.cwd()
if you are making a command line tool or something.compile
takes an options object where you can provide an array ofmodule
s.If you just want to compile a string as a module, you can use
sweet.loadModule(<contents>)
.Again, this API is merely a stop-gap and will be removed in the near future.
Edit: corrected the
compile
API