3 ms·
Coming from a Scheme background, if there's one thing I can't believe we live without in Javascript, it's macros. Hell even C has macros... I've looked at Sweet
by eatonphil 9y ago
Coming from a Scheme background, if there's one thing I can't believe we live without in Javascript, it's macros. Hell even C has macros... I've looked at Sweet.js a few times to add support for macros to the Linode Manager. Between m4, the gcc preprocessor, and something native, this seemed like the easiest potential sell. However, I've been turned off by how complicated this looks compared to any of the other options and I'm not sure it actually even supports the feature I want: allow some code to show up in "development" mode but for that code to not even exist in the compiled output of "production" mode. (The other feature that's just as important though is being able to wrap shared code up in a single function that gets compiled in at compile time, but I think Sweet.js supports that.)
- ykler 9y agoWho uses m4? It has been around forever, but I have never really seen it used except in autoconf. When I looked at it (a long time ago) it struck me as really tricky and confusing, even more than lisp macros (but maybe on a par with TeX). However, this was just a first impression -- I didn't go too deep.
- chas 9y agoI have used it recently. I have a similar attitude towards m4 as shell scripts: they are good for quick hacks, proofs of concept, and very simple systems. When I need to sprinkle just a bit of macros on something, m4 is great. For example, a one-off piece of technical documentation I wrote is markdown interspersed with code examples. I wanted to make sure the code passed its unit tests, so it lives in separate files and I use m4 to include it into the bigger document. This is nicer than catting separate file chunks together because the text can all be contiguous, but if I needed to do anything more sophisticated I would probably switch to purpose-built templating or static site system.
- dflock 9y agoI've used it for really simple templating stuff with Dockerfiles - which it works fine for.
- kazinator 9y agoI once learned a lot about m4; more than twenty years ago. I remember all the features it has, like redefining the delimiters for macros, diversions, for loops and all that jazz. So it's not due to lack of familiarity that I won't use it; it is simply atrocious. Recently I needed a preprocessor (featuring just simple if/ifdef/elif/endif) in a Linux distro build system (Yocto). I didn't want to add anything new. To avoid inflicting m4 on myself and everyone who has to interact with this in the future, I wrote a simple awk script wrapped around cpp. The tiny awk script implements this pipeline, which involves encoding and decoding the input so that cpp never sees the syntax of the file: this is our 1 1 this is our original 2 2 --> [dec] original @ifdef FOO --[enc]--> #ifdef FOO --[cpp]--> 6 file input 4 @endif #endif file 6 this works with nearly any textual input. Your input can contain broken C tokens and comments and whatnot. I switched the # to @ to remind people that this isn't the C preprocessor, and because # is a comment character in the preprocessed language, which could look confusing.
- girvo 9y agoHeh, I've used it with Docker to generate config files at build time using build arguments and environment variables. Worked surprisingly well!
- dtzur 9y agoWebpack can do that for you via dead code removal
- abritinthebay 9y agoUglify has been doing it for years too - just do a quick env var string substitution and it’s trivial, Webpack or not.