Permalink
Browse files

Enhanced the optimizer so all the passed js code is minified

  • Loading branch information...
1 parent 6247b4a commit 62d01b239d60dd630c2143f8563f99c868fb612d @xcambar committed Apr 22, 2012
Showing with 74 additions and 14 deletions.
  1. +1 −1 build/shepherd.server.js
  2. +1 −1 src/flavours/optimizer.js
  3. +1 −1 src/flavours/server.js
  4. +71 −11 src/shears.js
View
2 build/shepherd.server.js
@@ -1225,7 +1225,7 @@
path = require.resolve(uri);
} catch (e) {}
try {
- path = !path ? require("fs").statSync(__dirname + "/../" + uri).isFile() && __dirname + "/../" + uri : path;
+ path = !path ? require("fs").statSync(__dirname + "/" + uri).isFile() && __dirname + "/../" + uri : path;
} catch (e) {}
try {
path = !path ? require("fs").statSync(process.cwd() + "/" + uri).isFile() && process.cwd() + "/" + uri : path;
View
2 src/flavours/optimizer.js
@@ -141,5 +141,5 @@
"onLoad" : function (initConfig, initModules, s6d) {
exports = module.exports = s6d;
}
- }
+ };
})();
View
2 src/flavours/server.js
@@ -84,7 +84,7 @@
path = require.resolve(uri);
} catch (e) { /** Nothing here **/ }
try {
- path = !path ? require('fs').statSync(__dirname + '/../' + uri).isFile() && (__dirname + '/../' + uri) : path;
+ path = !path ? require('fs').statSync(__dirname + '/' + uri).isFile() && (__dirname + '/../' + uri) : path;
} catch (e) { /** Nothing here **/ }
try {
path = !path ? require('fs').statSync(process.cwd() + '/' + uri).isFile() && process.cwd() + '/' + uri : path;
View
82 src/shears.js
@@ -1,19 +1,79 @@
-var s6d = require('../build/shepherd.optimizer.js');
-s6d({exposeAPI: true});
-s6d('fixtures/recursive/useCase2/index.js');
+
+var argv = require('optimist')
+ .usage('Minifies your dependencies declared with Harmony\'s modules syntax.')
+ .alias('b', 'basepath')
+ .describe('b', 'basepath for your modules. Useful if the location of your modules is not your root folder.')
+ .alias('o', 'out')
+ .describe('o', 'output file')
+ .demand('_')
+ .argv,
+ jsp = require("uglify-js").parser,
+ pro = require("uglify-js").uglify,
+ s6d = require('../build/shepherd.optimizer.js');
+
+s6d({exposeAPI: true}); // Allows to fetch the flavour and its data
+
+//
+// OPTION PARSING
+//
+var _input = argv._;
+if (_input.length > 1) {
+ console.error('You can only minify one app at a time.');
+}
+
+var cwd = process.cwd();
+if(argv.b) {
+ process.chdir(argv.b);
+}
+s6d(_input[0]);
+if(argv.b) {
+ process.chdir(cwd);
+}
+
+//
+// Optimizer
+//
+
+var mods = s6d.flavour.getModules();
+
+//
+// Custom JSON.stringify as we need to output functions and they get quoted if treated as strings
+//
+var fns = [];
+for (var i = 0; i < mods.length; i++) {
+ fns.push('{' +
+ 'fn:' + Function.apply({}, mods[i].argNames.concat(mods[i].body + '; return ' + mods[i].exports)).toString() + ',' +
+ 'argsRefs:' + JSON.stringify(mods[i].argRefs) +
+ '}');
+}
var buffer =
"(function () {" +
- "var mods = " + JSON.stringify(s6d.flavour.getModules()) + ";" +
- "for (var i=0; i<mods.length; i++) {" +
- "var module = mods[i];" +
- "var fn = Function.apply({}, module.argNames.concat(module.body + '; return ' + module.exports));" +
- "var args = [];" +
- "for (var j=0; j<module.argRefs.length; j++) {" +
- "args.push(mods[module.argRefs[j]]);" +
+ "var mods = [" +
+ fns.join(',') +
+ "];" +
+ "for (var i=0; i < mods.length; i++) {" +
+ "var module = mods[i]," +
+ " fn = module.fn," +
+ " args = [];" +
+ "for (var j=0; j<module.argsRefs.length; j++) {" +
+ "args.push(mods[module.argsRefs[j]]);" +
"}" +
"mods[i] = fn.apply(fn, args);" +
"}" +
"})()";
-console.log(buffer);
+buffer = jsp.parse(buffer);
+buffer = pro.ast_mangle(buffer);
+buffer = pro.ast_squeeze(buffer);
+buffer = pro.gen_code(buffer, {beautify: false});
+
+if (argv.o) {
+ require('fs').writeFile(argv.o, buffer, function (err) {
+ if (err) {
+ throw err;
+ }
+ });
+} else {
+ require('util').print(buffer);
+}

0 comments on commit 62d01b2

Please sign in to comment.