I have a Coffeescript/Javascript project that is a large web application and I’m using the C++ preprocessor to piece the files together with some #ifdef and #include directives. It has dramatically simplified and organized my source. I’m using rake as my build tool.
I can call the cpp -E -Xpreprocessor directive as a system call from rake, but I’d love to be able to just use it within the Rake script itself.
e.g. current usage
pre_processed_code = `cpp -E -Xpreprocessor -DDebug app.coffee`
Are the any ruby implementations of the C++ preprocessor that you can run a string through or can anyone suggest a better pre-processing workaround?
Rails solves problems like this with the asset pipeline. It can orchestrate the CoffeeScript -> JavaScript transforms, and you can readily include source files (or directory trees) to have them concatenated and minified.
Sprockets supports chaining filters. For example,
foo.js.coffeewill be treated as CoffeeScript that produces JavaScript, which may itself include other dependencies and is a candidate for minification. Pulling in external variables for conditional compilation, inlining objects from a database, and other wacky behavior can be readily accommodated by chaining in other filters, e.g.foo.js.coffee.erb:All this is customarily executed on the fly in development and via in a compilation step in production, where individual source files are processed, joined, and optimized before delivery to the web server. This all works for CSS preprocessors and JavaScript template languages, too — if there’s a web-related thing that someone wants to compile, there’s likely support for it somewhere.
You’re probably not using Rails, but that shouldn’t stop you from leveraging all this work: the asset pipeline is actually a standalone gem called Sprockets. You can likely bolt it on in place of your existing build process. This isn’t a C++ preprocessor, but it is what a lot of other people are doing to solve this problem.