Javascript-friendly preprocessor dilemma
I've been working on a (nearly finished) Javascript project for a little over 14 months now. The project started out as a hack I expected to finish overnight, but over time the Javascript part has grown up to be 68 separate files and 10,314 non-empty lines, sadly currently dependent on the C preprocessor for building.
It's hard to explain how I ended up using cpp for the Javascript: this was only a hack, and I needed something that gave me macro expansion, ifdef, define, and includes without too much effort. After about 3 second's consideration开发者_如何转开发, I realized cpp was "perfect" for the task:
- Provide a LOG() macro that disappears in release mode.
- Provide an ASSERT() macro that disappears in release mode, and generates exceptions tagged with original file and line number.
- Swap LOG() and ASSERT() implementations for code that allows producing a "checked" build, which logs events in a compact form as they occur, that are reported back to the server if a crash occurs.
- Replacing PROJECT_SOME_CONFIG_VAR_NAME with "1", based on a configuration file shared with the Python backend.
Like all true hacks, this hack is now so hard-coded into the project that I truly hate to even think about the work that will be required to replace it. My project is reaching the stage where I want to move the code base to its own dedicated test server (rather than my laptop), however on setting up a Linux instance, I've discovered that GNU cpp versions after 4.1 no longer preprocess the Javascript with crashing out with an error.
Rather than add a specific version of GCC to the list of build requirements, I figured now would be a good time to truly fix this mess. My problem is, though, that I can't find a replacement preprocessor with the same power and features as cpp! I have considered m4, but m4 is a world of pain unto itself. Other Javascript-specific preprocessors I have found lack all the features I am currently depending on, e.g.:
__FILE__
&__LINE__
- Variadic macros
- Include guards
- Token concatenation
- Conditional compilation
I am seriously considering implementing a brand new preprocessor just for Javascript, that completely borrows the C preprocessor syntax since it has worked so well. I'm just wondering if there are better options before doing so. :) Perhaps there is already a cpp-like generic preprocessor I could swap in? The work required to replace all the preprocessor syntax in those 68 files approaches the amount of work required to simply reimplement the preprocessor.
I'm quite surprised I ever made it so far while relying on the C preprocessor; it is more effective at this task than healthy for the mind to comprehend. Another option open to me is statically building versions of cpp-4.1 for Linux, Darwin-i386, Win32, and storing those binaries in the project's repository.
Help!
Have you already tried mcpp? It's a "portable C preprocessor"
Should that fail, you could try using a generic macro processor (like gema) and build enough cpp on top of it.
I looked at cpp and a few other contenders based on answers to similar questions. Then found what I think is the best answer by far - PHP. Consider:
1) PHP is indeed a preprocessor (its in the acronym).
2) PHP is ready to go on most servers. All you have to do is either change your file extensions from ".js" to ".php" or ".js.php", or (my preference) make a simple edit to a server config file (you can find the details elsewhere).
3) Many JavaScript developers are already familiar with or expert at PHP.
4) PHP is Turing-complete, unlike many other preprocessors.
5) You only need basic PHP knowledge to benefit a lot.
6) There is way more documentation/tutorials/forums for PHP than other preprocessors.
But mostly, it's so weird it's cool. Thanks to whoever gave me the idea - I can't find their post at the moment.
精彩评论