Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lnolte/master
Automatically concat files
- Loading branch information
Showing
4 changed files
with
109 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
Lots of comments | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut lorem sem. | ||
Vestibulum vehicula dolor ac elit dictum convallis. Vivamus rhoncus, neque id euismod tempor, justo nulla pellentesque nibh, | ||
nec placerat mauris massa vel massa. Etiam cursus rutrum faucibus. | ||
Mauris sem turpis, lacinia eget faucibus vel, vulputate et ante. | ||
*/ | ||
|
||
someOtherNiftyCode="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,82 @@ | ||
var exec = require('child_process').exec; | ||
var minify = (function(undefined) { | ||
|
||
var minify = function(options) { | ||
this.type = options.type; | ||
this.fileIn = options.fileIn; | ||
this.fileOut = options.fileOut; | ||
this.options = options.options || []; | ||
if (typeof options.callback !== 'undefined') { | ||
this.callback = options.callback; | ||
} | ||
var exec = require('child_process').exec, | ||
_fs = require('fs'); | ||
|
||
this.compress(); | ||
}; | ||
|
||
minify.prototype = { | ||
type: null, | ||
fileIn: null, | ||
fileOut: null, | ||
callback: null | ||
}; | ||
|
||
minify.prototype.compress = function() { | ||
var self = this, command; | ||
var minify = function(options) { | ||
this.type = options.type; | ||
this.tempFile = new Date().getTime().toString(); | ||
|
||
if(typeof options.fileIn === 'string') { | ||
this.fileIn = options.fileIn; | ||
} | ||
|
||
if(typeof options.fileIn === 'object' && options.fileIn instanceof Array) { | ||
var out = options.fileIn.map(function(path) { | ||
return _fs.readFileSync(path, 'utf8'); | ||
}); | ||
|
||
_fs.writeFileSync(this.tempFile, out.join('\n'), 'utf8'); | ||
|
||
this.fileIn = this.tempFile; | ||
} | ||
|
||
this.fileOut = options.fileOut; | ||
this.options = options.options || []; | ||
this.buffer = options.buffer || 1000 * 1024; | ||
if (typeof options.callback !== 'undefined') { | ||
this.callback = options.callback; | ||
} | ||
|
||
switch (this.type) { | ||
case 'yui': | ||
command = 'java -jar -Xss2048k ' + __dirname + '/yuicompressor-2.4.7.jar ' + this.fileIn + ' -o ' + this.fileOut + ' ' + this.options.join(' '); | ||
break; | ||
case 'gcc': | ||
command = 'java -jar ' + __dirname + '/google_closure_compiler-r1810.jar --js=' + this.fileIn + ' --js_output_file=' + this.fileOut + ' ' + this.options.join(' '); | ||
break; | ||
case 'uglifyjs': | ||
command = __dirname + '/../node_modules/uglify-js/bin/uglifyjs --output ' + this.fileOut + ' --no-copyright ' + this.fileIn + ' ' + this.options.join(' '); | ||
break; | ||
} | ||
this.compress(); | ||
}; | ||
|
||
exec(command, function (err, stdout, stderr) { | ||
//console.log(err); | ||
//console.log(stdout); | ||
//console.log(stderr); | ||
minify.prototype = minify.fn = { | ||
type: null, | ||
fileIn: null, | ||
fileOut: null, | ||
callback: null, | ||
buffer: null, // with larger files you will need a bigger buffer for closure compiler | ||
compress: function() { | ||
var self = this, command; | ||
|
||
if(self.callback){ | ||
if (err) { | ||
self.callback(err); | ||
} else { | ||
self.callback(null); | ||
|
||
switch (this.type) { | ||
case 'yui': | ||
command = 'java -jar -Xss2048k ' + __dirname + '/yuicompressor-2.4.7.jar ' + this.fileIn + ' -o ' + this.fileOut + ' --type css ' + this.options.join(' '); | ||
break; | ||
case 'gcc': | ||
command = 'java -jar ' + __dirname + '/google_closure_compiler-r1810.jar --js=' + this.fileIn + ' --js_output_file=' + this.fileOut + ' ' + this.options.join(' '); | ||
break; | ||
case 'uglifyjs': | ||
command = __dirname + '/../node_modules/uglify-js/bin/uglifyjs --output ' + this.fileOut + ' --no-copyright ' + this.fileIn + ' ' + this.options.join(' '); | ||
break; | ||
} | ||
|
||
exec(command, { maxBuffer: this.buffer }, function (err, stdout, stderr) { | ||
//console.log(err); | ||
//console.log(stdout); | ||
//console.log(stderr); | ||
|
||
if(self.fileIn === self.tempFile) { | ||
// remove the temp concat file here | ||
_fs.unlinkSync(self.tempFile); | ||
} | ||
|
||
if(self.callback){ | ||
if (err) { | ||
self.callback(err); | ||
} else { | ||
self.callback(null); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
return minify; | ||
|
||
})(); | ||
|
||
exports.version = '0.3.6'; | ||
exports.minify = minify; | ||
exports.minify = minify; |