sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.59k stars 211 forks source link

[Question] How does sweet.compile or sweet.parse function works? #753

Closed IdkGoodName closed 3 years ago

IdkGoodName commented 5 years ago

I want to use sweet.parse or sweet.compile

But for some reason I need to provide loader.compile function.

Is there anyway to use sweet.parse or sweet.compile? I need to parse Sweet JS inside of function.

temirlanzhangazy commented 4 years ago
var compile = require('@sweet-js/core').compile;
var NodeLoader = require('@sweet-js/core/dist/node-loader').default;
let loaderOptions = {
        noBabel: false  
};  
var loader = new NodeLoader(process.cwd(), loaderOptions);
try{
    var output = compile(fs.realpathSync("your_path"), loader, {
        noBabel: ''
    });
    console.log(output.code)
} catch (e) {
    console.log(e)
}

This is the way I could compile sweet.js with node.js.