Skip to content

Commit

Permalink
simple "make" utility. ideas welcome.
Browse files Browse the repository at this point in the history
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(...).
  • Loading branch information
mishoo committed May 15, 2011
1 parent 86a1d8d commit 7c535e6
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions bin/uglifyjs
Expand Up @@ -29,6 +29,7 @@ var options = {
quote_keys: false,
space_colon: false
},
make: false,
output: true // stdout
};

Expand Down Expand Up @@ -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;
Expand All @@ -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 = "";
Expand Down

0 comments on commit 7c535e6

Please sign in to comment.