Well, with C#.NET.
I maintain a service which updates the minified versions of reused Javascript libraries. It runs at a specified time each night, and will check through all javascript code within a directory for two things:
1) If a .js doesn’t have a .min.js file; then a minified version of the file is created.
2) If a .js file does have a .min.js file which is out of date; then a new minified version of the file is created.
(aka, its a nightly run service which updates all minified version of javascript librarys on a server)
This service has a couple of problems on the surface. Why doesn’t it also make a packed file version? Does the minified version always perform (without error) the same as the development version? And, what “minifier” does it use?
Here’s are some quick answers:
1) “Packed Version”:
Unfortunately, when I first created the service only minified files were being created (aka, a long time ago in a land far far away). And, I haven’t yet had time to implement “packed versions”. It should only take a night to do update the source code. But when will I have that night?
2) What language?
C#. Sorry all, but it’s the easiest for me to use.
3) Does the minified version always perform (without error) the same as the development version?
It depends on the “minifier”.
I have found that “jsmin” doesn’t really translate the code that well. YUI is pretty good; but not 100% accurate. It’s about 99% accurate.
This system does support the Microsft Implementation of javscript minifying … but I have never used it. Because …
Google’s implementation was pretty damn good. I have never had an error/problem with a file minimized through the google closure compiler (http://closure-compiler.appspot.com/home).
3) What “minifier” does it use?
Thankfully, I made the program agnositic to what minifier was used. Currently, it supports “jsmin”, “ajaxmin” (MS), “YuiCompressor”, and “Closure” (Google).
Hopefully, you can copy/paste this into your project and be able to use Google Closure-Compiler.
// So, this is different than most of the other compilers because it uses a remote (web) service.
// the first thing is to read in the javascript file.
string fileContents;
using( var fileReader = new StreamReader( info.FullName ) )
{
fileContents = fileReader.ReadToEnd();
}
// The files contents have been read. Now they need to be formatted in a POST request with the
/// file contents URL encoded.
const string postFormat = "js_code={0}&compilation_level={1}&output_format={2}&output_info={3}";
var data = string.Format(
postFormat,
HttpUtility.UrlEncode( fileContents ),
"WHITESPACE_ONLY",
"text",
"compiled_code"
);
// finally, create the Http Request (POST request)
var request = WebRequest.Create( "http://closure-compiler.appspot.com/compile" );
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// and, write the contents to the request.
using( var dataStream = new StreamWriter( request.GetRequestStream() ) )
{
dataStream.Write( data );
}
// the response contains the javascript
var response = request.GetResponse();
using( var responseStream = response.GetResponseStream() )
{
var streamReader = new StreamReader( responseStream );
return streamReader.ReadToEnd();
}
If you would like to see more than just the Google Code portion, or even the whole code for the service please email or comment.
2 comments:
I know this is an old post, but is this project still active? Please let me know because I have a project on hold that is waiting for something like this. Thanks much, Dustin
It's still in use, but no really active. The only real change is that we changed the system to use the command line version of closure compiler. The java command looks like: string.Format( "java -jar \\\\\\closure.jar --js \"{0}\" --js_output_file \"{1}\"", javascriptPath, tempOutputPath )
Post a Comment