Are there any projects that used node.js and closure-compiler (CC for short) together?
The official CC recommendation is to compile all code for an application together, but when I compile some simple node.js code which contains a require("./MyLib.js")
, that line is put directly into the output, but it doesn't make any sense in that context.
I see a few options:
- Code the entire application as a single file. This solves the problem by avoiding it, but is bad for maintenance.
- Assume that all files will be concatenated before execution. Again this avoids the problem, but makes it harder to implement a un-compiled debug mode.
- I'd like to get CC to "understand" the node.js require() function, but that probably can't be done without editing the compiler itself, can it?
I replaced my old approach with a way simpler approach:
New approach
Funny thing is that I didn't even had to add an extern for the
require()
calls. The Google Closure compiler understands that automagically. I did have to add externs for nodejs modules that I use.Old approach
As requested by OP, I will elaborated on my way of compiling node.js code with Google Closure Compiler.
I was inspired by the way bolinfest solved the problem and my solution uses the same principle. The difference is that I made one node.js script that does everything, including inlining modules (bolinfest's solution lets GCC take care of that). This makes it more automated, but also more fragile.
I just added code comments to every step I take to compile server code. See this commit: https://github.com/blaise-io/xssnake/commit/da52219567b3941f13b8d94e36f743b0cbef44a3
To summarize:
In my case, this file is start.js.
require()
calls, including the assignment part.In start.js, this matches one require call:
var Server = require('./lib/server.js');
require()
call, so I repeat step 3 and 4 recursively until allrequire()
calls are gone and I'm left with one huge string containing all code.You could also use the offline compiler.
I have externs for every core node.js module. This tool is useful for generating externs.
require
calls to the compiled code.Pre-Compiled code.
All
require
calls are removed. All my code is flattened.http://pastebin.com/eC2rVMiN
Post-Compiled code.
Node.js core
require
calls have been prepended manually.http://pastebin.com/uB8CaejN
Why you should not do it this way:
require
calls, inlining and removingmodule.exports
. This is fragile, as it does not cover all syntax variations.Why you should:
Option 4: Don't use closure compiler.
People in the node community don't tend to use it. You don't need to minify node.js source code, that's silly.
There's simply no good use for minification.
As for the performance benefits of closure, I personally doubt it actually makes your programs faster.
And of course there's a cost, debugging compiled JavaScript is a nightmare
The svn HEAD of closure compiler seems to have support for AMD
Closure Library on Node.js in 60 seconds.
It's supported, check https://code.google.com/p/closure-library/wiki/NodeJS.
I have been using the Closure Compiler with Node for a project I haven't released yet. It has taken a bit of tooling, but it has helped catch many errors and has a pretty short edit-restart-test cycle.
First, I use plovr (which is a project that I created and maintain) in order to use the Closure Compiler, Library, and Templates together. I write my Node code in the style of the Closure Library, so each file defines its own class or collection of utilities (like
goog.array
).The next step is to create a bunch of externs files for the Node functions you want to use. I published some of these publicly at:
https://github.com/bolinfest/node-google-closure-latitude-experiment/tree/master/externs/node/v0.4.8
Though ultimately, I think that this should be a more community driven thing because there are a lot of functions to document. (It's also annoying because some Node functions have optional middle arguments rather than last arguments, making the type annotations complicated.) I haven't started this movement myself because it's possible that we could do some work with the Closure Complier to make this less awkward (see below).
Say you have created the externs file for the Node namespace
http
. In my system, I have decided that anytime I needhttp
, I will include it via:Though I do not include that
require()
call in my code. Instead, I use theoutput-wrapper
feature of the Closure Compiler the prepend all of therequire()
s at the start of the file, which when declared in plovr, in my current project looks like this:In this way, my library code never calls Node's
require()
, but the Compiler tolerates the uses of things likehttp
in my code because the Compiler recognizes them as externs. As they are not true externs, they have to be prepended as I described.Ultimately, after talking about this on the discussion list, I think the better solution is to have a new type annotation for namespaces that would look something like:
In this scenario, an externs file would define the
NodeHttpNamespace
such that the Closure Compiler would be able to typecheck properties on it using the externs file. The difference here is that you could name the return value ofrequire()
whatever you wanted because the type ofhttp
would be this special namespace type. (Identifying a "jQuery namespace" for$
is a similar issue.) This approach would eliminate the need to name your local variables for Node namespaces consistently, and would eliminate the need for that giantoutput-wrapper
in the plovr config.But that was a digression...once I have things set up as described above, I have a shell script that:
RAW
mode.node
on the file generated by plovr.Using
RAW
mode results in a large concatenation of all the files (though it also takes care of translating Soy templates and even CoffeeScript to JavaScript). Admittedly, this makes debugging a pain because the line numbers are nonsense, but has been working well enough for me so far. All of the checks performed by the Closure Compiler have made it worth it.