ES6兼容的JavaScript迷你型程序
esprimajs的Python项目详细描述
esprima编译器是一个由python编写的与es6兼容的javascript小型化器。 esprima编译器使用esprima-python 作为javascript解析器库
功能
- ES6支持
- 损坏函数和变量名
- 识别输出
- 模糊字符串
安装
pip install esprima-compiler
示例
示例javascript文件
constAconst=Math.PI,Bconst="Bconst";constCconst="Cconst";functionadd(a,b){functionfoo(){returna+b;}returnfoo();}constDconst="Dconst";functionmul(a,b){returna*b;}constEconst="Econst";letAlet="Alet";constFconst="Fconst",Gconst="Gconst";
重新排列和更改变量:
python -m esprima_compiler -r --mangle-variable test.js
constAconst=Math.PI,Bconst="Bconst",Cconst="Cconst";functionadd($a,$b){functionfoo(){return$a+$b;}returnfoo();}constDconst="Dconst";functionmul($a,$b){return$a*$b;}constEconst="Econst",Fconst="Fconst",Gconst="Gconst";letAlet="Alet";
重新排列变量和损坏变量(包括顶层)以及函数名(顶层除外):
python -m esprima_compiler -r --mangle-variable-top --mangle-function test.js
const$a=Math.PI,$b="Bconst",$c="Cconst";functionadd($d,$e){function$f(){return$d+$e;}return$f();}const$d="Dconst";functionmul($e,$f){return$e*$f;}const$e="Econst",$f="Fconst",$g="Gconst";let$h="Alet";
重新排列变量、更改变量和函数名(包括顶层),但不标识:
python -m esprima_compiler -i 0 -r --mangle-variable-top --mangle-function-top --mangle-function 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";
API
编译javascript字符串:
>>>fromesprima_compiler.compilerimportCompiler>>>c=Compiler(rearrange=True,mangle_variable=True,mangle_variable_top=True)>>>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.compile(js)>>>print(buf.read())const$a=1,$b=2;let$c=3;classTestClassextendsObject{constructor($d,$e){this._a=$d;this.b=$e;}staticgeta(){returnthis._a;}setb($d){this.b=$d;}staticasyncsum(){returnthis.a+this.b;}}>>>