react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / tools / node.js
80743 viewsvar path = require("path");1var fs = require("fs");2var vm = require("vm");34var UglifyJS = vm.createContext({5console : console,6process : process,7Buffer : Buffer,8MOZ_SourceMap : require("source-map")9});1011function load_global(file) {12file = path.resolve(path.dirname(module.filename), file);13try {14var code = fs.readFileSync(file, "utf8");15return vm.runInContext(code, UglifyJS, file);16} catch(ex) {17// XXX: in case of a syntax error, the message is kinda18// useless. (no location information).19console.log("ERROR in file: " + file + " / " + ex);20process.exit(1);21}22};2324var FILES = exports.FILES = [25"../lib/utils.js",26"../lib/ast.js",27"../lib/parse.js",28"../lib/transform.js",29"../lib/scope.js",30"../lib/output.js",31"../lib/compress.js",32"../lib/sourcemap.js",33"../lib/mozilla-ast.js",34"../lib/propmangle.js"35].map(function(file){36return fs.realpathSync(path.join(path.dirname(__filename), file));37});3839FILES.forEach(load_global);4041UglifyJS.AST_Node.warn_function = function(txt) {42console.error("WARN: %s", txt);43};4445// XXX: perhaps we shouldn't export everything but heck, I'm lazy.46for (var i in UglifyJS) {47if (UglifyJS.hasOwnProperty(i)) {48exports[i] = UglifyJS[i];49}50}5152exports.minify = function(files, options) {53options = UglifyJS.defaults(options, {54spidermonkey : false,55outSourceMap : null,56sourceRoot : null,57inSourceMap : null,58fromString : false,59warnings : false,60mangle : {},61output : null,62compress : {}63});64UglifyJS.base54.reset();6566// 1. parse67var toplevel = null,68sourcesContent = {};6970if (options.spidermonkey) {71toplevel = UglifyJS.AST_Node.from_mozilla_ast(files);72} else {73if (typeof files == "string")74files = [ files ];75files.forEach(function(file){76var code = options.fromString77? file78: fs.readFileSync(file, "utf8");79sourcesContent[file] = code;80toplevel = UglifyJS.parse(code, {81filename: options.fromString ? "?" : file,82toplevel: toplevel83});84});85}8687// 2. compress88if (options.compress) {89var compress = { warnings: options.warnings };90UglifyJS.merge(compress, options.compress);91toplevel.figure_out_scope();92var sq = UglifyJS.Compressor(compress);93toplevel = toplevel.transform(sq);94}9596// 3. mangle97if (options.mangle) {98toplevel.figure_out_scope(options.mangle);99toplevel.compute_char_frequency(options.mangle);100toplevel.mangle_names(options.mangle);101}102103// 4. output104var inMap = options.inSourceMap;105var output = {};106if (typeof options.inSourceMap == "string") {107inMap = fs.readFileSync(options.inSourceMap, "utf8");108}109if (options.outSourceMap) {110output.source_map = UglifyJS.SourceMap({111file: options.outSourceMap,112orig: inMap,113root: options.sourceRoot114});115if (options.sourceMapIncludeSources) {116for (var file in sourcesContent) {117if (sourcesContent.hasOwnProperty(file)) {118output.source_map.get().setSourceContent(file, sourcesContent[file]);119}120}121}122123}124if (options.output) {125UglifyJS.merge(output, options.output);126}127var stream = UglifyJS.OutputStream(output);128toplevel.print(stream);129130if(options.outSourceMap){131stream += "\n//# sourceMappingURL=" + options.outSourceMap;132}133134var source_map = output.source_map;135if (source_map) {136source_map = source_map + "";137}138139return {140code : stream + "",141map : source_map142};143};144145// exports.describe_ast = function() {146// function doitem(ctor) {147// var sub = {};148// ctor.SUBCLASSES.forEach(function(ctor){149// sub[ctor.TYPE] = doitem(ctor);150// });151// var ret = {};152// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;153// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;154// return ret;155// }156// return doitem(UglifyJS.AST_Node).sub;157// }158159exports.describe_ast = function() {160var out = UglifyJS.OutputStream({ beautify: true });161function doitem(ctor) {162out.print("AST_" + ctor.TYPE);163var props = ctor.SELF_PROPS.filter(function(prop){164return !/^\$/.test(prop);165});166if (props.length > 0) {167out.space();168out.with_parens(function(){169props.forEach(function(prop, i){170if (i) out.space();171out.print(prop);172});173});174}175if (ctor.documentation) {176out.space();177out.print_string(ctor.documentation);178}179if (ctor.SUBCLASSES.length > 0) {180out.space();181out.with_block(function(){182ctor.SUBCLASSES.forEach(function(ctor, i){183out.indent();184doitem(ctor);185out.newline();186});187});188}189};190doitem(UglifyJS.AST_Node);191return out + "";192};193194function readReservedFile(filename, reserved) {195if (!reserved) {196reserved = { vars: [], props: [] };197}198var data = fs.readFileSync(filename, "utf8");199data = JSON.parse(data);200if (data.vars) {201data.vars.forEach(function(name){202UglifyJS.push_uniq(reserved.vars, name);203});204}205if (data.props) {206data.props.forEach(function(name){207UglifyJS.push_uniq(reserved.props, name);208});209}210return reserved;211}212213exports.readReservedFile = readReservedFile;214215exports.readDefaultReservedFile = function(reserved) {216return readReservedFile(path.join(__dirname, "domprops.json"), reserved);217};218219exports.readNameCache = function(filename, key) {220var cache = null;221if (filename) {222try {223var cache = fs.readFileSync(filename, "utf8");224cache = JSON.parse(cache)[key];225if (!cache) throw "init";226cache.props = UglifyJS.Dictionary.fromObject(cache.props);227} catch(ex) {228cache = {229cname: -1,230props: new UglifyJS.Dictionary()231};232}233}234return cache;235};236237exports.writeNameCache = function(filename, key, cache) {238if (filename) {239var data;240try {241data = fs.readFileSync(filename, "utf8");242data = JSON.parse(data);243} catch(ex) {244data = {};245}246data[key] = {247cname: cache.cname,248props: cache.props.toObject()249};250fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8");251}252};253254255