从shell中,我可以调用Sweet.js编译器。
sjs -m macro-providing-module -o output-directory/file.js input-directory/file.sjs
如何在Node.js模块中执行相同的操作,这样就可以将编译后的输出作为字符串而不是输出到指定的文件中?
var sweetjs = require('sweet.js');
var input = require('fs').readSync('input-directory/file.sjs');
var module = 'macro-providing-module';
var output = sweetjs(/* ??? */);发布于 2014-01-11 13:40:16
它没有文档化,因为当sweet.js获得良好的模块支持时,它将被更改/删除(使用ES6 import而不是命令行标志)。但是对于0.4.0,您可以使用以下API从npm模块加载宏:
var sweet = require('sweet.js');
var mod = sweet.loadNodeModule(root, modulePath);
var out = sweet.compile(<contents>, {
modules: [mod]
});root参数是文件系统中开始查找节点模块的位置。通常,如果您正在制作命令行工具或其他东西,这就是process.cwd()。compile接受一个options对象,您可以在其中提供一个module的数组。
如果您只想将字符串编译为模块,则可以使用sweet.loadModule(<contents>)。
同样,这个API仅仅是一个止损间隙,并将在不久的将来被删除.
编辑:更正了compile API
https://stackoverflow.com/questions/21062468
复制相似问题