Skip to content

Node.js PrePrcoessor – Extend Node.js to support macros/utility functions

bbarrows.com
2 pointsbebrws2 comments
On HN

Comments

In this blog post, I show how I created a preprocessor that takes any line starting with “// log -” and creates a temporary intermediary file where those lines are replaced with console.log statements to print any expressions following “// log -”. This approach is useful in several ways. It’s easier than using console.log directly, and when run without using the preprocessor, these log lines are simply ignored. The entire setup is straightforward, utilizing a Node.js environment variable/option to run the preprocessor.

You should look into how this is done at scale with something like Babel.

Babel parses JS into a AST (abstract syntax tree) and then lets you react to specific pieces of code.

Here is an example of the power of a tool like Babel: https://github.com/christina-de-martinez/babel-plugin-glowup...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.