Research on compile time communication abilities
Inspired by Jai, John Blow
__compilerJob($compiler => {
$compiler.addListener('item', (arg: any) => {
const items = $compiler.globalStore('items');
if (!items.list) items.list = [];
items.list.push(arg);
});
$compiler.replaceWithCode('(void 0)');
});
__compilerJob($compiler => {
$compiler.emitEvent('item', '({ hello: "world" })');
$compiler.replaceWithCode('(void 0)');
});
__compilerJob($compiler => {
$compiler.emitEvent('item', '({ foo: "boo" })');
$compiler.replaceWithCode('(void 0)');
});
__compilerJob($compiler => {
const cmds = $compiler.globalStore('items');
$compiler.replaceWithCode('[' + cmds.list.join(', ') + ']');
});
Result:
(void 0);
(void 0);
(void 0);
[({ hello: "world" }), ({ foo: "boo" })];
__compilerJob($compiler => {
const ts = $compiler.getTs();
// compiler processing node hook
$compiler.addListener('$onEnterNode', (node, replaceNode) => {
if (ts.isSmth(node)) {
replaceNode(...)
}
});
});
__compilerJob($compiler => {
const ts = $compiler.getTs();
$compiler.addListener('$onProjectCompiled', ($compiler) => {
// append smth when everything is compiled
// eg cli commands
$compiler.appendNode($compiler.globalStore('data').items.map(x => ...));
});
});