Script.prototype.getCode = function(parsedSource, minify) {
  var code, ast = parsedSource;
  if (minify) {
    ast = jspro.ast_mangle(ast);
    ast = jspro.ast_squeeze(ast);
  }
  code = jspro.gen_code(ast);
  return this.preCode + this.license + code + this.postCode;
};
Example #2
0
task({'default': ['coffee', 'handlebars']}, function (params) {
  var template = fs.readFileSync('src/faqjs-template.handlebars.js', 'utf8');
  var js = fs.readFileSync('src/faqjs.js', 'utf8');
  var ast = jsp.parse(template + js); // 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 minJs = uglify.gen_code(ast); // compressed code here
  fs.writeFileSync('faqjs.min.js', minJs, 'utf8');
});
   taskRunner.push(function(next) {
      var ast;

      ast = uglifyParser.parse(concatenatedOutput);    // 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

      fs.writeFile(jsOutputFile.replace(/\.js$/, '.min.js'), uglify.gen_code(ast), 'utf-8', next);
   }, 'Save minified JS file');
Example #4
0
function uglify(src, dst) {
  var jsp = require("uglify-js").parser;
  var pro = require("uglify-js").uglify;
  var orig_code = read(src);
  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
  fs.writeFileSync(dst, pro.gen_code(ast)); 
}
var js = function(inputFile, outputFile) {
  console.log('require: npm install uglify-js@1');
  console.log('minifier', inputFile, outputFile);
  var ast = jsp.parse(fs.readFileSync(inputFile).toString()); // 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
  fs.writeFileSync(outputFile, final_code, "utf-8");
};
Example #6
0
function instrument(code) {
    var ast = jsp.parse(code, false, true); // true for the third arg specifies that we want
    // to have start/end tokens embedded in the
    // statements
    var w = pro.ast_walker();

    // we're gonna need this to push elements that we're currently looking at, to avoid
    // endless recursion.
    var analyzing = [];

    function do_stat() {
        var ret;
        if (this[0].start && analyzing.indexOf(this) < 0) {
            // without the `analyzing' hack, w.walk(this) would re-enter here leading
            // to infinite recursion
            analyzing.push(this);
            ret = [ "splice", // XXX: "block" is safer
                [
                    [ "stat",
                        [ "call", [ "name", "trace" ],
                            [
                                [ "string", this[0].toString() ],
                                [ "num", this[0].start.line ],
                                [ "num", this[0].start.col ],
                                [ "num", this[0].end.line ],
                                [ "num", this[0].end.col ]
                            ]]],
                    w.walk(this)
                ]];
            analyzing.pop(this);
        }
        return ret;
    };
    var new_ast = w.with_walkers({
        "stat":do_stat,
        "label":do_stat,
        "break":do_stat,
        "continue":do_stat,
        "debugger":do_stat,
        "var":do_stat,
        "const":do_stat,
        "return":do_stat,
        "throw":do_stat,
        "try":do_stat,
        "defun":do_stat,
        "if":do_stat,
        "while":do_stat,
        "do":do_stat,
        "for":do_stat,
        "for-in":do_stat,
        "switch":do_stat,
        "with":do_stat
    }, function () {
        return w.walk(ast);
    });
    return pro.gen_code(new_ast, { beautify:true });
}
Example #7
0
	function packIntoJSHeader(js){
		if (!cSettings.debug === true) {
			var ast = jsp.parse(js);
			ast = pro.ast_mangle(ast);
			ast = pro.ast_squeeze(ast);
			js = pro.gen_code(ast);
		}
		return "<script type=\"text/javascript\">" + js + "</script>\n";
	}
Example #8
0
fs.readFile('src.js', 'utf-8', function(err, js) {
    var ast = parser.parse(js);
    ast = uglify.ast_mangle(ast);
    ast = uglify.ast_squeeze(ast);
    var final_code = uglify.gen_code(ast);

    fs.writeFileSync('bookmarklet.js', 'javascript:' + final_code);
    console.log('Saved a new build to bookmarklet.js');
    process.exit();
});
Example #9
0
  function () {
    console.log('uglify jquery.fenster.js');

    var ast = ugp.parse(fs.readFileSync('production/jquery.fenster.full.js').toString());
    ast = ugu.ast_mangle(ast);
    ast = ugu.ast_squeeze(ast);

    var result = ugu.split_lines(ugu.gen_code(ast), 1024 * 8);
    fs.writeFileSync(this.name, getComment() + result);
  }
Example #10
0
	uglifyjs = function(str){//使用uglify-js压缩,生成压缩后的js文件
		var jsp = require("uglify-js").parser,
		    pro = require("uglify-js").uglify,		
		    orig_code = str,
			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
		return final_code+';';
	},
 this._getApi(aRequest, aResponse, function (err, str) {
     if(err) {
         return aRequest.next(err);
     }
     var ast = jsp.parse(str);
     ast = pro.ast_mangle(ast);
     ast = pro.ast_squeeze(ast);
     var final_code = pro.gen_code(ast);
     aResponse.send(final_code);
 });
Example #12
0
 , uglify = function (src) {
     try {
       var ast = jsp.parse(src)
       ast = pro.ast_squeeze(ast)
       return pro.gen_code(ast)
     }
     catch (ex) {
       return src
     }
   }
Example #13
0
    fs.readFile(config.libOut, function (err, data) {
      var libCode = data.toString();

      var ast = jsp.parse(libCode); // parse code and get the initial AST
      ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
      var compiledCode = pro.gen_code(ast); // compressed code here
      fs.writeFile(config.libOut, compiledCode, function () {
        console.log('Built the lib code.');
      });
    })
function Squash(script) {
  // (let me (pretend (it-is-real LISP))) ;)
  if(opts.pretty_test) { return script; }
  else { // var Uglify = require ('uglify-js');
    return Uglify.uglify.gen_code(
      Uglify.uglify.ast_squeeze(
        Uglify.parser.parse(
          String(script)
          ))); }
} /* Squash */
Example #15
0
  minifySource: function() {
    var ast, opts = util.extend({}, this.builder.options.minify || {});
    util.extend(opts, this.builder.minifyOptions);

    ast = uglify.parser.parse(this.derived.generated);
    ast = uglify.uglify.ast_mangle(ast, opts);
    ast = uglify.uglify.ast_squeeze(ast, opts);

    return uglify.uglify.gen_code(ast, opts);
  },
Example #16
0
 function(file, path, index, isLast, callback) {
   if (env.production) {
     var ast = uglify_jsp.parse(file);
     ast = uglify_pro.ast_mangle(ast);
     ast = uglify_pro.ast_squeeze(ast);
     callback(uglify_pro.gen_code(ast, { beautify: true, indent_level: 0 }));
   } else {
     callback(file);
   }
 }
Example #17
0
 expect.addAssertion('to encode to', function (expect, subject, value) {
     var cldrPluralRuleSet = new CldrPluralRuleSet();
     Object.keys(this.obj).forEach(function (count) {
         cldrPluralRuleSet.addRule(subject[count], count);
     }, this);
     var beautifiedFunction = uglifyJs.uglify.gen_code(['toplevel', [['stat', ['function', null, ['n'], cldrPluralRuleSet.toJavaScriptFunctionBodyAst()]]]], {beautify: true});
     if (typeof value === 'function') {
         value = uglifyJs.uglify.gen_code(uglifyJs.parser.parse('(' + value.toString() + ')'), {beautify: true});
     }
     expect(beautifiedFunction, 'to equal', value);
 });
Example #18
0
    , uglify: function (source, out, passback, callback) {
        var tok = uglifyJS.parser.tokenizer(source)
          , c = tok()
          , min = FILE.copywrite(c.comments_before) || ''
          , ast = uglifyJS.parser.parse(source);

        ast = uglifyJS.uglify.ast_mangle(ast);
        ast = uglifyJS.uglify.ast_squeeze(ast);
        min += uglifyJS.uglify.gen_code(ast);
        out(min, passback, callback);
      }
function runUglify(mergedFile) {
    var ast;
    try {
        ast = jsp.parse(mergedFile); // parse code and get the initial AST
    } catch (e) {
        console.log('UglifyJS parse failed', e);
    }
    ast = pro.ast_mangle(ast); // get a new AST with mangled names
    ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
    return pro.gen_code(ast); // compressed code here
}
Example #20
0
    TARGETS.uglifyJS = function(code){
        console.log('[uglifyJS] compress js');

        var ast = jsp.parse(code);
        ast = pro.ast_mangle(ast);
        ast = pro.ast_squeeze(ast);

        return pro.gen_code(ast, {
            inline_script: true  //to escape occurrences of </script in strings
        });
    };
loopRead(function(){
	var ast;
	combineFile = '(function(){' + combineFile + '})();';
	ast = jsp.parse(combineFile);
	ast = pro.ast_mangle(ast);
	ast = pro.ast_squeeze(ast);
	ast = pro.gen_code(ast);
	fs.writeFile('youkuhtml5playerbookmark2.js', ast, function() {
		console.log('-----youkuhtml5playerbookmark2.js updated!----');
	});
});
Example #22
0
exports.run = function(path, data, o) {
    var ast = min.parser.parse(data);

    if (o.verbose) {
        min.uglify.set_logger(console.log);
    }

    ast = min.uglify.ast_mangle(ast);
    ast = min.uglify.ast_squeeze(ast);
    return min.uglify.gen_code(ast);
};
Example #23
0
var _compile = function(code, opts, mainModule) {
	var code = 'var __require__ = {}\n' + _compileModule(code, opts.basePath, mainModule)
	if (opts.minify === false) { return code } // TODO use uglifyjs' beautifier?

	var uglifyJS = require('uglify-js')

	var ast = uglifyJS.parser.parse(code, opts.strict_semicolons),
	ast = uglifyJS.uglify.ast_mangle(ast, opts)
	ast = uglifyJS.uglify.ast_squeeze(ast, opts)

	return uglifyJS.uglify.gen_code(ast, opts)
}
Example #24
0
    this.createDriverJs(testParams, function (err, driverjs) {
        if (err) {
            return callback(err);
        }
        driverJs = driverjs;

        if (self.minifyJS) {
            driverJs = uglifyParser.parse(driverJs); // parse code and get the initial AST
            driverJs = uglify.ast_mangle(driverJs); // get a new AST with mangled names
            driverJs = uglify.ast_squeeze(driverJs); // get an AST with compression optimizations
            driverJs = uglify.gen_code(driverJs); // compressed code here*
            self.logger.debug("Minified Driver js: " + driverJs);
        }

        webdriver.session_.then(function (val) {
            logger.trace("Selenium session id: " + val.id);
            if (!val.id) {
                self.errorCallback(logger, "Unable to get a valid session, check your selenium config", callback);
                return;
            }
            caps = val.capabilities;
        });

        // in case of html test, we need to load the test as a page
        if (".html" === path.extname(testJs)) {
            page = path.resolve("", testJs);
        }

        // Making sure we have the page, and its not a custom controller.
        if (page && testParams.customController === false) {
            // for local page ,has to add relative path
            if ("http:" !== page.substr(0, 5) && "https:" !== page.substr(0, 6)) {
                if (self.args.relativePath) {
                    page = path.resolve(global.workingDirectory, self.args.relativePath, page);
                } else {
                    page = path.resolve(global.workingDirectory, page);
                }
            }
            self.logger.info("Loading page: " + page);

            // local paths need to be served via test server
            url = self.getRemoteUrl(page);
            if (false === url) {
                self.errorCallback(logger, "Cannot load a local file without arrow_server running: " + page, callback);
            } else {
                webdriver.get(url).then(function () {
                    initTest();
                });
            }
        } else {
            initTest();
        }
    });
Example #25
0
function uglifyJs(code) {
    var uglify = require('uglify-js');
    var ast;
    try {
        ast = uglify.parser.parse(code);
        ast = uglify.uglify.ast_mangle(ast);
        ast = uglify.uglify.ast_squeeze(ast);
        return uglify.uglify.gen_code(ast);
    } catch (err) {
        return code;
    }
}
Example #26
0
environment.jsCompressor = function (context, data, callback) {
  try {
    var ast = UglifyJS.parser.parse(data);

    ast = UglifyJS.uglify.ast_mangle(ast);
    ast = UglifyJS.uglify.ast_squeeze(ast);

    callback(null, UglifyJS.uglify.gen_code(ast));
  } catch (err) {
    callback(err);
  }
};
Example #27
0
/**
 * 压缩Javascript代码
 * 
 * @inner
 * @param {string} code Javascript源代码
 * @return {string} 
 */
function compressJavascript( code ) {
    var jsp = require("uglify-js").parser;
    var pro = require("uglify-js").uglify;

    var ast = jsp.parse( code );
    ast = pro.ast_mangle( ast, {
        except: [ '$', 'require', 'exports', 'module' ]
    });
    ast = pro.ast_squeeze( ast );

    return pro.gen_code(ast);
};
Example #28
0
  minifySource: function(source) {
    this.log('- Minifying bundle');

    var ast, opts = util.extend({}, this.options.minify || {});
    util.extend(opts, this.minifyOptions);

    ast = uglify.parser.parse(source);
    ast = uglify.uglify.ast_mangle(ast, opts);
    ast = uglify.uglify.ast_squeeze(ast, opts);

    return uglify.uglify.gen_code(ast, opts);
  },
Example #29
0
function _uglify(code) {
  var ast = uglifyjs.parser.parse(code);
  process.stderr.write('consolidating property names ... ');
  ast = uglifyjs.consolidator.ast_consolidate(ast);
  process.stderr.write('mangling variable names ... ');
  ast = uglifyjs.uglify.ast_mangle(ast);
  process.stderr.write('squeezing AST ... ');
  ast = uglifyjs.uglify.ast_squeeze(ast);
  ast = uglifyjs.uglify.ast_squeeze_more(ast);

  return uglifyjs.uglify.gen_code(ast);
}
Example #30
0
function bundle() {

    var scripts = [
        'jquery.min',
        'json2',
        'underscore-min',
        'handlebars.min',
        'backbone-min',
        'jquery.masonry.min',
        'jquery.tagsinput.min',
        'bootstrap-modal',
        'jquery-ui.min',
        'models/Bookmark',
        'models/BookmarksCollection',
        'models/Tag',
        'models/TagsCollection',
        'views/PublicView',
        'views/AccountView',
        'views/EditView',
        'views/BookmarkView',
        'views/BookmarksView',
        'views/TagView',
        'views/TagsView',
        'views/AppView',
        'routers/BookmarklyRouter',
        'App'
    ];
    
    var templates = ['account', 'bookmark', 'edit', 'footer', 'header', 'index', 'pub', 'tag', 'bookmarks'];
    
    var bundle = '';
    scripts.forEach(function(file) {  
        bundle += "\n/**\n* " + file + ".js\n*/\n\n" + fs.readFileSync(__dirname + '/public/js/' + file + '.js') + "\n\n";
    });
    
    var ast = parser.parse(bundle);
    ast = uglifyer.ast_mangle(ast);
    ast = uglifyer.ast_squeeze(ast);
    bundle = uglifyer.gen_code(ast)
    
    fs.writeFileSync(__dirname + '/public/js/bundle.js', bundle, 'utf8');
    
    bundle = "Templates = {};\n";
    templates.forEach(function(file) {
        var html = fs.readFileSync(__dirname + '/public/templates/' + file + '.html', 'utf8');
        html = html.replace(/(\r\n|\n|\r)/gm, ' ').replace(/\s+/gm, ' ').replace(/'/gm, "\\'");
        bundle += "Templates." + file + " = '" + html + "';\n";
    });
    
    fs.writeFileSync(__dirname + '/public/js/templates.js', bundle, 'utf8');

}