Reputation: 1511
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(/* ??? */);
Upvotes: 1
Views: 616
Reputation: 161
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:
var sweet = require('sweet.js');
var mod = sweet.loadNodeModule(root, modulePath);
var out = sweet.compile(<contents>, {
modules: [mod]
});
The root
argument is the location on the file system to start looking for node modules. Generally this is process.cwd()
if you are making a command line tool or something. compile
takes an options object where you can provide an array of module
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
Upvotes: 2