From 7c535e67d366c2c61f0622aac8577c9d9a69cdec Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Sun, 15 May 2011 13:15:34 +0300 Subject: [PATCH] simple "make" utility. ideas welcome. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For now, write a Makefile.uglify.js file with contents like this: { files: [ { name: "foo.js", module: "Foo" }, { name: "bar.js", hide: true } ] } If you run uglifyjs --make it will read and compress foo.js and wrap it like this: (function(){ return this })().Foo = (function(exports, global){ ... compressed contents of foo.js ... })({}, (function(){ return this })()); as for bar.js it'll wrap like this: (function(){ ... compressed contents of bar.js ... })() Both of them are concatenated and returned. The "module" is thus useful to use libraries designed for a CommonJS environment in browser. This is just a quick hack as I needed it fast, but the plan is to evolve it—handle compressor options in the Makefile, additional possibilities to wrap the files, maybe handle require(...). --- bin/uglifyjs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 93fd7123..85a10a7d 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -29,6 +29,7 @@ var options = { quote_keys: false, space_colon: false }, + make: false, output: true // stdout }; @@ -170,6 +171,9 @@ out: while (args.length > 0) { case "--ascii": options.codegen_options.ascii_only = true; break; + case "--make": + options.make = true; + break; default: filename = v; break out; @@ -186,12 +190,27 @@ jsp.set_logger(function(msg){ sys.debug(msg); }); -if (filename) { +if (options.make) { + options.out_same_file = false; // doesn't make sense in this case + var makefile = JSON.parse(fs.readFileSync(filename || "Makefile.uglify.js").toString()); + output(makefile.files.map(function(file){ + var code = fs.readFileSync(file.name); + if (file.module) { + code = "(function(){return this}())." + file.module + " = (function(exports, global){" + code + "\n;return exports;})({}, (function(){return this}()));"; + } + else if (file.hide) { + code = "(function(){" + code + "}());"; + } + return squeeze_it(code); + }).join("\n")); +} +else if (filename) { fs.readFile(filename, "utf8", function(err, text){ if (err) throw err; output(squeeze_it(text)); }); -} else { +} +else { var stdin = process.openStdin(); stdin.setEncoding("utf8"); var text = "";