gcc-rest
A node.js module for compiling JavaScript code using Google Closure Compiler's REST API.
This library is also available as a Grunt plugin.
How to install
npm install gcc-rest
How to use
Basic example
Compile file1.js and file2.js and write it to compiled.js
var gcc = ;gcc;gcc;
If you're into chaining; this does the same:
;
Note: Closing methods that perform compiling (output
, compile
and compilePassJson
) cannot be chained.
An advanced example:
// Load gcc-rest modulevar gcc = ; // Set Closure Compiler parametersgcc; // Add files that should be compiledgcc; // Replace code before compilinggcc; // Compile and write output to compiled.js,// Handle errors when something goes wrong.gcc;
Adding code that should be compiled
Add a single file:
gcc;
Add multiple files:
gcc;
Add a directory of Javascript files:
// Use the optional second argument to pass an array of files// that should be excluded when scanning the dir.gcc;
Manually add a snippet of Javascript:
gcc;
Replace code before compiling:
gcc; // Removes all console.log statements
Compiler request parameters
Documentation on Google Closure Compiler's request parameters can be found here. The additional web service options are also supported. Unsupported parameters will print a warning. gcc-rest does not overwrite Google Closure Compiler's default settings.
Set a Google Closure Compiler request parameter:
gcc;
Or set multiple request parameters at once:
gcc;
Handling the compiled output
Output to a file:
gcc;
You may also pass the compiled source to a callback function. The compiled source is passed as the first parameter.
Use a callback function by supplying a function reference:
gcc;
Or an anonymous function:
gcc;
Access the unmodified Json response from Google Closure Compiler by calling compilePassJson()
:
gcc;
Prepend the compiled source with a header:
gcc;
Handling errors
Handle errors by supplying an error handling function to the second argument of output
, compile
or compilePassJson
.
This function will be called when something goes wrong.
The error handling function accepts two parameters, errorType
and errorDetails
.
errorType
provides on of these error types:
service_error
: The Google Closure Compiler REST API could not process the request. Check the compiler docs for details..request_error
: The HTTP request could not be sent to the API.server_error
: The HTTP response did not return a200: Success
status. Please note that the API will return a200: Success
when aservice_error
occurs.
errorDetails
provides (unstructured) detailed information.
Example:
gcc; gcc; gcc
Custom logging
By default, gcc-rest uses the console
object for logging. If you want, you can
overwrite gcc.console.info
, gcc.console.warn
and gcc.console.error
with
your own functions. Example:
gccconsole { fs;}
Unit testing
- Clone from GitHub
- Run
npm install
to install the Mocha test framework - Run
npm test
Update history
- 2014-03-06: v0.2.5 Provider better error handling functionality documentation
- 2014-03-06: v0.2.4 Provider better error handling functionality
- 2013-12-01: v0.2.3 Consistency in error/warning logging
- 2013-08-08: v0.2.0 Add tests, travis, allow overwriting console
- 2013-04-07: v0.1.6 Allow chaining after the
addDir
method - 2013-02-02: v0.1.5 Add
addDir
method - 2013-01-12: v0.1.4 Fix issue where no header prepends "undefined" to output
- 2013-01-05: v0.1.3 Add support for debug parameter
- 2012-12-30: v0.1.2 Add support for use_types_for_optimization parameter
- 2012-10-02: v0.0.3 Prevent module cache
- 2012-10-02: v0.0.1 Initial release
License
gcc-rest is released under the MIT License.