I'm working on a babel plugin that runs some code through an async module. I need to wait on that result before I modify the path.
visitor: {
TaggedTemplateExpression(path) {
if (path.node.tag.name !== 'Style') return;
ProcessStyle(path).then((data) => {
path.replaceWith(t.StringLiteral(data.data));
});
},
}
Is this currently possible?
Babel's API, for example babel.transform()
, synchronously returns the resulting transformed. This means that plugins have no way of being async, because Babel itself is fully synchronous.
Depending on what your requirements are, you could consider using child_process.execSync
to synchronously run another Node process to do your async work while blocking Babel.
https://github.com/ForbesLindesay/sync-rpc
this runs async code in a separate process, and communicates over a network connection
i found it surprisingly hard to do this reliably with child_process and execSync / spawnSync. i always had problems with limited buffer size, even when setting maxBuffer to Infinity, resulting in incomplete data transfers and mysterious syntax errors (code lines were cut off after some 1000 characters)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With