How to use the uglify-js.TreeTransformer function in uglify-js

To help you get started, we’ve selected a few uglify-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spmjs / spm / lib / utils / ast.js View on Github external
Ast.replaceDefine = function(code, id, deps) {
  var ast = getAst(code);

  var find = false;
  var replace = new UglifyJS.TreeTransformer(null, function(node, descend) {
    if (isCmdDefine(node)) {
      find = true;
      var args = node.args;  

      // 添加 id 和 依赖 节点.
      var idNode = new UglifyJS.AST_String({
        value: id 
      });

      var elements = [];
      deps.forEach(function(dep) {
        elements.push(new UglifyJS.AST_String({value: dep}));
      });
      var depsNode = new UglifyJS.AST_Array({
        elements: elements 
      });
github petrosagg / papagal / tools / beautify.js View on Github external
const walker = new UglifyJS.TreeWalker((node, descend) => {
		if (node.TYPE === 'Assign' && node.left.TYPE === 'Dot' && node.right.TYPE === 'Call') {
			if (node.right.TYPE === 'Call' && node.right.expression.TYPE === 'Function') {
				const ret = node.right.expression.body[node.right.expression.body.length - 1]
				if (ret.TYPE === 'Return' && ret.value.TYPE === 'Sequence') {
					const last = ret.value.expressions[ret.value.expressions.length - 1]
					if (last.TYPE === 'SymbolRef') {
						last.thedef.name = node.left.property
					}
				}
			}
		}
	})

	const sequenceTransformer = new UglifyJS.TreeTransformer(function (node, descend) {
		// Eliminate spurious blocks
		if (node instanceof UglifyJS.AST_Block) {
			// TODO: check scopes when merging blocks
			let body = []
			for (const child of node.body.map(n => n.transform(this))) {
				if (child instanceof UglifyJS.AST_BlockStatement) {
					body = body.concat(child.body)
				} else if (child.TYPE === 'SimpleStatement' && child.body instanceof UglifyJS.AST_SimpleStatement) {
					body.push(child.body)
				} else if (child.TYPE === 'SimpleStatement' && child.body instanceof UglifyJS.AST_BlockStatement) {
					body = body.concat(child.body.body)
				} else if (child.TYPE === 'SimpleStatement' && child.body instanceof UglifyJS.AST_Sequence) {
					const block = new UglifyJS.AST_BlockStatement({
						body: child.body.expressions.map(node2statement)
					})
					body = body.concat(block.transform(this).body)
github erzu / porter / lib / cmd-util / ast.js View on Github external
var replaceChild = function(node, fn) {
    var args = node.args[0],
      children = args instanceof UglifyJS.AST_Array ? args.elements : [args]

    children.forEach(function(child){
      if (child instanceof UglifyJS.AST_String) {
        child.value = fn(child.getValue())
      }
    })
  }

  requirefn = makeFunction(requirefn)
  asyncfn = makeFunction(asyncfn)

  var trans = new UglifyJS.TreeTransformer(function(node, descend) {
    // require('foo')
    if (requirefn && node instanceof UglifyJS.AST_Call && node.expression.name === 'require' && node.args.length) {
      return replaceChild(node, requirefn)
    }

    // require.async('foo', function(foo){ // callback })
    if (asyncfn && node instanceof UglifyJS.AST_Call && node.start.value === 'require' && node.expression.property === 'async' && node.args.length) {
      return replaceChild(node, asyncfn)
    }
  })
  return ast.transform(trans)
}
github spmjs / spm / test / utils / uglifyjs.js View on Github external
it('test replace ast_call node', function() {

    var ast = UglifyJS.parse('var c = seajs.importStyle(".a{color:#000;background:#fff}", "alice/a.css");');
    var seajsNode = null;
    var findImportStyle = new UglifyJS.TreeWalker(function(node, descend) {
      if (node instanceof UglifyJS.AST_Call && node.start.value === 'seajs') {
        seajsNode = node.clone();
        return true;
      }
    });

    ast.walk(findImportStyle);

    var ast2 = getAst(astModule, 'src/foo.js');

    var replace = new UglifyJS.TreeTransformer(function(node, descend) {
      if (node instanceof UglifyJS.AST_Call && node.start.value === 'require') {
        //console.info(node);
        if (seajsNode) {
          return seajsNode.clone();
        }
        return node.clone();
      }
    });
    ast2.transform(replace);
    var code2 = ast2.print_to_string({beautify: true});
    code2.should.include('seajs.importStyle');
    code2.should.not.include('require(');
  });
github Bobris / bobril-build / dist / bundler.js View on Github external
order.forEach((f) => {
        if (f.difficult)
            return;
        let transformer = new uglify.TreeTransformer((node) => {
            if (node instanceof uglify.AST_Label) {
                return node;
            }
            if (node instanceof uglify.AST_Symbol) {
                let symb = node;
                if (symb.thedef == null)
                    return undefined;
                let rename = symb.thedef.bbRename;
                if (rename !== undefined || symb.thedef.bbAlwaysClone) {
                    symb = symb.clone();
                    if (rename !== undefined)
                        symb.name = rename;
                    symb.thedef = undefined;
                    symb.scope = undefined;
                    return symb;
                }
github stephenmathieson / node-obfuscator / lib / utils.js View on Github external
function mangleStrings(ast) {
  var transformer = new uglifyjs.TreeTransformer(null, mangleString);
  return ast.transform(transformer);
}
github spmjs / spm / lib / sdk / ast.js View on Github external
function replaceRequire(ast, fn, output) {
  output = output || 'string';
  ast = getAst(ast);

  var trans = new UglifyJS.TreeTransformer(function(node, descend) {
    if (node instanceof UglifyJS.AST_String) {
      var parent = trans.parent();
      if (parent instanceof UglifyJS.AST_Call && parent.start.value === 'require') {
        return new UglifyJS.AST_String({
          start: node.start,
          end: node.end,
          value: fn(node.getValue())
        });
      }
    }
  });
  ast = ast.transform(trans);

  if (output === 'ast') return ast;

  return ast.print_to_string({
github appcelerator / alloy / Alloy / commands / compile / optimizer.js View on Github external
var transformer = new uglifyjs.TreeTransformer(function(node, descend){
		var convert = false;
		if (dotSubCheck(node, 'name') || dotSubCheck(node, 'osname')) {
			descend(node, new uglifyjs.TreeTransformer(function(node, descend) {
				if (dotSubCheck(node, 'Platform')) {
					descend(node, new uglifyjs.TreeTransformer(function(node) {
						if (node instanceof uglifyjs.AST_SymbolRef && 
							(node.name === 'Titanium' || node.name === 'Ti')) {
							convert = true;
						} 
						return node;
					}));
				} 
				return node;
			}));
			if (convert) {
				var value = node instanceof uglifyjs.AST_Dot ? node.property : node.property.value;
				if (platform[value]) {
					return new uglifyjs.AST_String({
						start: node.start,
github telerik / kendo-ui-core / build / kendo-meta.js View on Github external
function cloneAST(ast) {
    var labels = [];
    var tw = new U2.TreeTransformer(function(node, descend) {
        if (node instanceof U2.AST_Label) {
            return node;
        }
        var clone = node.clone();
        if (node instanceof U2.AST_LabeledStatement) {
            clone.label = node.label.clone();
            clone.label.references = node.label.references;
            labels.push(clone.label);
            descend(clone, this);
            labels.pop();
            return clone;
        }
        if ((node instanceof U2.AST_Break ||
             node instanceof U2.AST_Continue) && node.label) {
            clone.label = clone.label.clone();
            for (var i = labels.length; --i >= 0;) {
github spmjs / spm / lib / utils / ast.js View on Github external
var replaceRequireNode = Ast.replaceRequireNode = function(code, isNeedValue, fn) {
  var ast = getAst(code);

  var find = false;
  var replace = new UglifyJS.TreeTransformer(function(node, descend) {
    if (node instanceof UglifyJS.AST_Call &&
        node.start.value === 'require' &&
        node.expression.name === 'require') {

      var value = node.args[0] && node.args[0].value;
      if (isNeedValue(value)) {
        find = true;
        return fn(node, value);
      }
      return node;
    }
  });

  var ast2 = ast.transform(replace);

  if (find) {