Code Monkey home page Code Monkey logo

esprimajs's Introduction

esprimajs is a es6-compatible javascript minifier written by python. esprimajs uses esprima-python as javascript parser library

Features

  • ES6 support
  • Mangling function and variable names
  • Ident output
  • Obfuscate strings

Installation

pip install esprimajs

Examples

Example javascript file

const Aconst = Math.PI, Bconst = "Bconst";
const Cconst = "Cconst";

function add(a, b){
        function foo(){
                return a+b;
        }
        return foo();
}

const Dconst="Dconst";

function mul(a, b){
        return a*b;
}


const Econst="Econst";
let Alet="Alet";
const Fconst="Fconst", Gconst="Gconst";

Rearrange and mangle variables:

python -m esprimajs -r -mv 2 test.js
const Aconst=Math.PI,Bconst="Bconst",Cconst="Cconst";
function add($a,$b){
  function foo(){
    return $a+$b;
  }
  return foo();
}
const Dconst="Dconst";
function mul($a,$b){
  return $a*$b;
}
const Econst="Econst",Fconst="Fconst",Gconst="Gconst";
let Alet="Alet";

Rearrange variables and mangle variables (include top-level) and function names (except top-level):

python -m esprimajs -r -mv 1 --mf 2 test.js
const $a=Math.PI,$b="Bconst",$c="Cconst";
function add($d,$e){
  function $f(){
    return $d+$e;
  }
  return $f();
}
const $d="Dconst";
function mul($e,$f){
  return $e*$f;
}
const $e="Econst",$f="Fconst",$g="Gconst";
let $h="Alet";

Rearrange variables and mangle variables and function names (include top level) without identing:

python -m esprimajs -i 0 -r -mf 1 -mf 1 test.js
const $a=Math.PI,$b="Bconst",$c="Cconst";function $d($e,$f){function $g(){return $e+$f;}return $g();}const $e="Dconst";function $f($g,$h){return $g*$h;}const $g="Econst",$h="Fconst",$i="Gconst";let $j="Alet";

Obfuscate string and integers:

echo 'const A=100,B=200; console.log(`a+b=${A+B}`);' | python -m esprimajs -os -oi -mv 1 -i 0
const $a=0x64,$b=0o310;console.log(`\x61\x2b\x62\x3d${$a+$b}`);

API

Minify javascript string:

>>> from esprimajs import Minifier
>>> c = Minifier(rearrange=True, mangle_variables=1)
>>> js = """
... const A=1;
... const B=2;
... let C=3;
...
... class TestClass extends Object{
...   constructor(a, b){
...     this._a=a;
...     this.b=b;
...   }
...   static get a(){
...     return this._a;
...   }
...   set b(b){
...     this.b=b;
...   }
...
...   static async sum(){
...     return this.a+this.b
...   }
... }
... """
>>> buf = c.minify(js)
>>> print(buf.read())
const $a=1,$b=2;
let $c=3;
class TestClass extends Object{
  constructor($d,$e){
    this._a=$d;
    this.b=$e;
  }
  static get a(){
    return this._a;
  }
  set b($d){
    this.b=$d;
  }
  static async sum(){
    return this.a+this.b;
  }
}
>>>

esprimajs's People

Stargazers

Aliaksiej M avatar  avatar DM_ avatar

Watchers

James Cloos avatar  avatar

Forkers

bitplorer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.