Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributable version for browsers and other non-CommonJS environments #97

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.tmp*~
*.local.*
.pinf-*
.pinf-*
/dist
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2011 Mihai Bazon <[email protected]>
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.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
125 changes: 76 additions & 49 deletions README.html

Large diffs are not rendered by default.

37 changes: 23 additions & 14 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <[email protected]>
Copyright (c) 2011 Mihai Bazon <[email protected]>
Based on parse-js (http://marijn.haverbeke.nl/parse-js/).

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 29 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -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"
);
});
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.parser = require("./parse-js");
exports.uglify = require("./process");
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion test/unit/scripts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var fs = require('fs'),
uglify = require('uglify-js'),
uglify = require('../../lib'),
jsp = uglify.parser,
nodeunit = require('nodeunit'),
path = require('path'),
Expand Down