Skip to content

Instantly share code, notes, and snippets.

@dobesv
Created January 21, 2012 11:06
Show Gist options
  • Save dobesv/1652368 to your computer and use it in GitHub Desktop.
Save dobesv/1652368 to your computer and use it in GitHub Desktop.
Combine, minify, and compress javascript files. Supports "watching" the files for automatic updates
#!node
var fs = require('fs'),
path = require('path'),
gzip = require('./gzip');
function process(fileBody, options, cb) {
if(typeof(fileBody) === 'object') {
options = fileBody;
fileBody = undefined;
cb = options;
}
if(typeof options === 'function') {
cb = options;
options = undefined;
}
options = options || {};
var filename = options.filename || file;
var dot = path.dirname(filename);
var paths = options.paths || [dot];
var watch = (options.watch && filename && (options.writeTo || cb)) || false;
var processIncludedFiles = options.recursive || false;
var filesWatched = {};
function processThisFile() {
var filesIncluded = [];
var fileBody = fileBody || fs.readFileSync(filename, 'utf8');
var output = fileBody.replace(/^\s*require\(\s*("([^"\n]+)"|'([^'\n]+)')\s*\)\s*;*\s*$/gm, function(_, withQuotes, p1, p2) {
var fileToInclude = p1 || p2; // Allow single or double quotes
var replacement = '';
var searchPaths = paths;
//console.log('Trying to include', fileToInclude, 'into', filename);
if(/^(.|..)\//.test(fileToInclude)) {
searchPaths = [dot];
}
if(!paths.some(function(p) {
var includeFilePath = path.join(p, fileToInclude);
var body;
try {
body = fs.readFileSync(includeFilePath, 'utf8');
} catch(e) {
return false;
}
body = body.replace(/^\uFEFF/, '');
var head = '/*! >>> require('+withQuotes+") */\n";
var tail = ";\n/*! <<< require("+withQuotes+") */\n";
replacement = (head + body + tail);
if(watch && !(includeFilePath in filesWatched) && fs.watch) {
filesWatched[includeFilePath] = includeFilePath;
fs.watch(includeFilePath, processThisFile);
}
filesIncluded.push(includeFilePath);
return true;
})) {
replacement = '/*! Failed to include '+fileToInclude+' in search path '+searchPaths.join(',')+" */";
}
return replacement;
});
if(options.uglify) {
try {
var uglify = require('uglify-js');
output = uglify(output);
} catch(e) {
console.log(String(new Date()), 'Uglify failed', e.toString(), '; will return original code');
}
}
if(options.writeTo) {
fs.writeFileSync(options.writeTo, output);
console.log(String(new Date()), 'Wrote', options.writeTo, 'from', filename, 'and', filesIncluded.length, 'included files');
}
if(options.writeGzipTo) {
if(gzip.writeFileSync(options.writeGzipTo, output)) {
console.log(String(new Date()), 'Wrote', options.writeGzipTo, 'from', filename, 'and', filesIncluded.length, 'included files');
}
}
if(typeof cb === 'function') {
cb(null, output);
}
return output;
}
if(watch) {
fs.watch(filename, processThisFile);
}
return processThisFile();
}
exports.process = process;
var fs = require('fs');
/**
* Compress the src string and write it to the given file name.
*
* Operates synchronously.
*
* Return false if gzip is not available or failed to initialize.
*/
exports.writeFileSync = function writeGzipFileSync(outPath, text, textEnc) {
var gzip;
try {
require('zlib').gzip(text, function(err, gzipped) {
fs.writeFileSync(outPath, gzipped, 'binary');
});
return true;
} catch(e) {
// No zlib module ...
console.log('Error using zlib module to gzip to', outPath, String(e));
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment