diff --git a/.gitignore b/.gitignore index d97eaa09..da080d93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store .tmp*~ *.local.* -.pinf-* \ No newline at end of file +.pinf-* +/dist diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..ee8561b5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2011 Mihai Bazon +Based on parse-js (http://marijn.haverbeke.nl/parse-js/). + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above + copyright notice, this list of conditions and the following + disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..23c7ab1a --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +all: dist test + +dist: dist/uglify.js + +dist/uglify.js: lib/*.js + mkdir -p dist + ./build.js > dist/uglify.js + +test: test/testparser.js test/unit + +test/testparser.js: lib/*.js + test/testparser.js + +test/unit: test/unit/scripts.js test/unit/compress/expected/*.js test/unit/compress/test/*.js lib/*.js + nodeunit test/unit/scripts.js + +clean: + rm -f dist/uglify.js diff --git a/README.html b/README.html index 4cd70115..90909073 100644 --- a/README.html +++ b/README.html @@ -7,7 +7,7 @@ UglifyJS -- a JavaScript parser/compressor/beautifier - + @@ -21,6 +21,9 @@ .target { } .timestamp { color: #bebebe; } .timestamp-kwd { color: #5f9ea0; } + .right {margin-left:auto; margin-right:0px; text-align:right;} + .left {margin-left:0px; margin-right:auto; text-align:left;} + .center {margin-left:auto; margin-right:auto; text-align:center;} p.verse { margin-left: 3% } pre { border: 1pt solid #AEBDCC; @@ -31,7 +34,13 @@ overflow:auto; } table { border-collapse: collapse; } - td, th { vertical-align: top; } + td, th { vertical-align: top; } + th.right { text-align:center; } + th.left { text-align:center; } + th.center { text-align:center; } + td.right { text-align:right; } + td.left { text-align:left; } + td.center { text-align:center; } dt { font-weight: bold; } div.figure { padding: 0.5em; } div.figure p { text-align: center; } @@ -90,7 +99,8 @@

Table of Contents

  • 1.2 Usage
  • 1.3 Compression – how good is it?
  • @@ -250,10 +260,10 @@

    1.1.1 Calls involving t -
    new Array(1, 2, 3, 4)  => [1,2,3,4]
    +
    new Array(1, 2, 3, 4)  => [1,2,3,4]
     Array(a, b, c)         => [a,b,c]
    -new Array(5)           => Array(5)
    -new Array(a)           => Array(a)
    +new Array(5)           => Array(5)
    +new Array(a)           => Array(a)
     
    @@ -271,38 +281,38 @@

    1.1.1 Calls involving t -
    // case 1.  globally declared variable
    -  var Array;
    -  new Array(1, 2, 3);
    +
    // case 1.  globally declared variable
    +  var Array;
    +  new Array(1, 2, 3);
       Array(a, b);
     
    -  // or (can be declared later)
    -  new Array(1, 2, 3);
    -  var Array;
    +  // or (can be declared later)
    +  new Array(1, 2, 3);
    +  var Array;
     
    -  // or (can be a function)
    -  new Array(1, 2, 3);
    -  function Array() { ... }
    +  // or (can be a function)
    +  new Array(1, 2, 3);
    +  function Array() { ... }
     
    -// case 2.  declared in a function
    -  (function(){
    -    a = new Array(1, 2, 3);
    +// case 2.  declared in a function
    +  (function(){
    +    a = new Array(1, 2, 3);
         b = Array(5, 6);
    -    var Array;
    +    var Array;
       })();
     
    -  // or
    -  (function(Array){
    -    return Array(5, 6, 7);
    +  // or
    +  (function(Array){
    +    return Array(5, 6, 7);
       })();
     
    -  // or
    -  (function(){
    -    return new Array(1, 2, 3, 4);
    -    function Array() { ... }
    +  // or
    +  (function(){
    +    return new Array(1, 2, 3, 4);
    +    function Array() { ... }
       })();
     
    -  // etc.
    +  // etc.
     
    @@ -455,20 +465,20 @@

    1.2.1 API

    -Symlink the lib directory as ~/.node_libraries/uglifyjs, so that the -require calls in the following sample will work: +Symlink the lib directory as ~/.node_libraries/uglify-js (or run +npm link) so that the require calls in the following sample will work:

    -
    var jsp = require("uglifyjs/parse-js");
    -var pro = require("uglifyjs/process");
    +
    var UglifyJS = require("uglify-js");
    +var parser = UglifyJS.parser, uglify = UglifyJS.uglify;
     
    -var orig_code = "... JS code here";
    -var ast = jsp.parse(orig_code); // parse code and get the initial AST
    -ast = pro.ast_mangle(ast); // get a new AST with mangled names
    -ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
    -var final_code = pro.gen_code(ast); // compressed code here
    +var orig_code = "... JS code here";
    +var ast = parser.parse(orig_code); // parse code and get the initial AST
    +ast = uglify.ast_mangle(ast); // get a new AST with mangled names
    +ast = uglify.ast_squeeze(ast); // get an AST with compression optimizations
    +var final_code = uglify.gen_code(ast); // compressed code here
     
    @@ -478,14 +488,14 @@

    1.2.1 API

    can see, there are a sequence of steps which you can apply. For example if you want compressed output but for some reason you don't want to mangle variable names, you would simply skip the line that calls -pro.ast_mangle(ast). +uglify.ast_mangle(ast).

    Some of these functions take optional arguments. Here's a description:

    • -jsp.parse(code, strict_semicolons) – parses JS code and returns an AST. +parser.parse(code, strict_semicolons) – parses JS code and returns an AST. strict_semicolons is optional and defaults to false. If you pass true then the parser will throw an error when it expects a semicolon and it doesn't find it. For most JS code you don't want that, but it's useful @@ -493,7 +503,7 @@

      1.2.1 API

    • -pro.ast_mangle(ast, options) – generates a new AST containing mangled +uglify.ast_mangle(ast, options) – generates a new AST containing mangled (compressed) variable and function names. It supports the following options: @@ -508,7 +518,7 @@

      1.2.1 API

  • -pro.ast_squeeze(ast, options) – employs further optimizations designed +uglify.ast_squeeze(ast, options) – employs further optimizations designed to reduce the size of the code that gen_code would generate from the AST. Returns a new AST. options can be a hash; the supported options are: @@ -526,7 +536,7 @@

    1.2.1 API

  • -pro.gen_code(ast, beautify) – generates JS code from the AST. By +uglify.gen_code(ast, beautify) – generates JS code from the AST. By default it's minified, but if you pass true for the second argument it will be nicely formatted and indented. Additionally, you can control the behavior by passing a hash for beautify, where the following options are @@ -553,10 +563,27 @@

    1.2.1 API

    -

    1.2.2 Beautifier shortcoming – no more comments

    +

    1.2.2 Browser version

    +

    +If you have the Stitch library installed (npm install stitch) you +can build a version of UglifyJS for use in the browser or any other +non-CommonJS environment. Run make dist to generate a distributable +script at dist/uglify.js. When loaded, the distributable version +adds an UglifyJS property to the global object without the need to +call require. Otherwise, the API is the same as described above. +

    +
    + +
    + +
    +

    1.2.3 Beautifier shortcoming – no more comments

    +
    + +

    The beautifier can be used as a general purpose indentation tool. It's useful when you want to make a minified file readable. One limitation, @@ -606,14 +633,14 @@

    1.3 Compression – h

    -+ - + - - + +
    LibraryOrig. sizeUglifyJSYUIGCL
    LibraryOrig. sizeUglifyJSYUIGCL
    DynarchLIB636896241441246452 (+5011)240439 (-1002) (buggy)
    jQuery1638557200679702 (+7696)71858 (-148)
    DynarchLIB636896241441246452 (+5011)240439 (-1002) (buggy)
    jQuery1638557200679702 (+7696)71858 (-148)
    @@ -707,7 +734,7 @@

    1.6 License

    -
    Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
    +
    Copyright (c) 2011 Mihai Bazon <mihai.bazon@gmail.com>
     Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
     
     Redistribution and use in source and binary forms, with or without
    @@ -754,8 +781,8 @@ 

    Footnotes:

    Author: Mihai Bazon

    -

    Date: 2011-02-19 11:12:48 EET

    -

    HTML generated by org-mode 7.01trans in emacs 23

    +

    Date: 2011-02-20 15:40:21 CST

    +

    HTML generated by org-mode 7.4 in emacs 24

    diff --git a/README.org b/README.org index bc2f55ca..1fc56fb9 100644 --- a/README.org +++ b/README.org @@ -221,42 +221,42 @@ Supported options: *** API -Symlink the *lib* directory as *~/.node\_libraries/uglifyjs*, so that the -*require* calls in the following sample will work: +Symlink the =lib= directory as =~/.node_libraries/uglify-js= (or run +=npm link=) so that the *require* calls in the following sample will work: #+BEGIN_SRC espresso -var jsp = require("uglifyjs/parse-js"); -var pro = require("uglifyjs/process"); +var UglifyJS = require("uglify-js"); +var parser = UglifyJS.parser, uglify = UglifyJS.uglify; var orig_code = "... JS code here"; -var ast = jsp.parse(orig_code); // parse code and get the initial AST -ast = pro.ast_mangle(ast); // get a new AST with mangled names -ast = pro.ast_squeeze(ast); // get an AST with compression optimizations -var final_code = pro.gen_code(ast); // compressed code here +var ast = parser.parse(orig_code); // parse code and get the initial AST +ast = uglify.ast_mangle(ast); // get a new AST with mangled names +ast = uglify.ast_squeeze(ast); // get an AST with compression optimizations +var final_code = uglify.gen_code(ast); // compressed code here #+END_SRC The above performs the full compression that is possible right now. As you can see, there are a sequence of steps which you can apply. For example if you want compressed output but for some reason you don't want to mangle variable names, you would simply skip the line that calls -=pro.ast_mangle(ast)=. +=uglify.ast_mangle(ast)=. Some of these functions take optional arguments. Here's a description: -- =jsp.parse(code, strict_semicolons)= -- parses JS code and returns an AST. +- =parser.parse(code, strict_semicolons)= -- parses JS code and returns an AST. =strict_semicolons= is optional and defaults to =false=. If you pass =true= then the parser will throw an error when it expects a semicolon and it doesn't find it. For most JS code you don't want that, but it's useful if you want to strictly sanitize your code. -- =pro.ast_mangle(ast, options)= -- generates a new AST containing mangled +- =uglify.ast_mangle(ast, options)= -- generates a new AST containing mangled (compressed) variable and function names. It supports the following options: - =toplevel= -- mangle toplevel names (by default we don't touch them). - =except= -- an array of names to exclude from compression. -- =pro.ast_squeeze(ast, options)= -- employs further optimizations designed +- =uglify.ast_squeeze(ast, options)= -- employs further optimizations designed to reduce the size of the code that =gen_code= would generate from the AST. Returns a new AST. =options= can be a hash; the supported options are: @@ -266,7 +266,7 @@ Some of these functions take optional arguments. Here's a description: - =dead_code= (default true) which will remove unreachable code. -- =pro.gen_code(ast, beautify)= -- generates JS code from the AST. By +- =uglify.gen_code(ast, beautify)= -- generates JS code from the AST. By default it's minified, but if you pass =true= for the second argument it will be nicely formatted and indented. Additionally, you can control the behavior by passing a hash for =beautify=, where the following options are @@ -277,6 +277,15 @@ Some of these functions take optional arguments. Here's a description: - =quote_keys: false= -- if you pass =true= it will quote all keys in literal objects +*** Browser version + +If you have the [[http://github.com/sstephenson/stitch][Stitch]] library installed (=npm install stitch=) you +can build a version of UglifyJS for use in the browser or any other +non-CommonJS environment. Run =make dist= to generate a distributable +script at =dist/uglify.js=. When loaded, the distributable version +adds an =UglifyJS= property to the global object without the need to +call *require*. Otherwise, the API is the same as described above. + *** Beautifier shortcoming -- no more comments The beautifier can be used as a general purpose indentation tool. It's @@ -365,7 +374,7 @@ I'd love to hear about them ([[http://groups.google.com/group/uglifyjs][use the UglifyJS is released under the BSD license: #+BEGIN_EXAMPLE -Copyright 2010 (c) Mihai Bazon +Copyright (c) 2011 Mihai Bazon Based on parse-js (http://marijn.haverbeke.nl/parse-js/). Redistribution and use in source and binary forms, with or without diff --git a/bin/uglifyjs b/bin/uglifyjs index ca6869fb..0e915ded 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -3,7 +3,7 @@ global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util"); var fs = require("fs"); -var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js +var uglify = require("uglify-js"), // `npm link` or symlink ../lib to ~/.node_libraries/uglify-js jsp = uglify.parser, pro = uglify.uglify; diff --git a/build.js b/build.js new file mode 100755 index 00000000..5b23413a --- /dev/null +++ b/build.js @@ -0,0 +1,29 @@ +#!/usr/bin/env node + +var fs = require("fs"); +var UglifyJS = require("./lib"); +var stitch = require("stitch"); +var package = stitch.createPackage({ paths: [__dirname + "/lib"] }); +var metadata = JSON.parse(fs.readFileSync(__dirname + "/package.json")); + +package.compile(function(err, source) { + if (err) throw err; + var uglify = UglifyJS.uglify; + + var ast = UglifyJS.parser.parse( + "(function(window) {" + source + ";" + + "window.UglifyJS = this.require('.')" + + "}).call({}, this)" + ); + + process.stdout.write( + "/*! UglifyJS v" + metadata.version + "\n" + + " * http://github.com/mishoo/UglifyJS/\n" + + " *\n" + + " * (c) 2011 Mihai Bazon\n" + + " * Released under the BSD License\n" + + " */\n" + + uglify.gen_code(uglify.ast_squeeze(uglify.ast_mangle(ast))) + + ";\n" + ); +}); diff --git a/index.js b/index.js deleted file mode 100644 index 1a0fc323..00000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -exports.parser = require("./lib/parse-js"); -exports.uglify = require("./lib/process"); diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 00000000..7144c32f --- /dev/null +++ b/lib/index.js @@ -0,0 +1,2 @@ +exports.parser = require("./parse-js"); +exports.uglify = require("./process"); diff --git a/package.json b/package.json index c267382f..3979a130 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,14 @@ -{"name" : "uglify-js", - "author" : "Mihai Bazon - http://github.com/mishoo", - "version" : "0.0.1", - "main" : "index.js", - "bin" : { "uglifyjs" : "./bin/uglifyjs" }, +{ "name" : "uglify-js" +, "author" : "Mihai Bazon - http://github.com/mishoo" +, "version" : "0.0.1" +, "main" : "./lib/index.js" +, "bin" : { "uglifyjs" : "./bin/uglifyjs" } +, "licenses" : [ + { "type" : "BSD" + , "url" : "http://github.com/mishoo/UglifyJS/raw/master/LICENSE" + }] +, "repository" : + { "type" : "git" + , "url" : "http://github.com/mishoo/UglifyJS.git" + } } diff --git a/test/unit/scripts.js b/test/unit/scripts.js index 94a4cddc..dc7499f4 100644 --- a/test/unit/scripts.js +++ b/test/unit/scripts.js @@ -1,5 +1,5 @@ var fs = require('fs'), - uglify = require('uglify-js'), + uglify = require('../../lib'), jsp = uglify.parser, nodeunit = require('nodeunit'), path = require('path'),