cocos2d-html5将js编译为jsc

在d:DevToolcocos2d-x-2.2.2cocos2d-x-2.2.2 oolscocos2d-consoleconsole

cocos2d_jscompile.py

cocos2d_version.py

cocos2d.py

cocos2d_new.py

 

d:DevToolcocos2d-x-2.2.2cocos2d-x-2.2.2	oolscocos2d-consoleconsole>python
cocos2d.py jscompile --help
Usage: cocos2d.py jscompile -s src_dir -d dst_dir [-c -o COMPRESSED_FILENAME -j
COMPILER_CONFIG]

Options:
  -h, --help            show this help message and exit
  -s SRC_DIR_ARR, --src=SRC_DIR_ARR
                        source directory of js files needed to be compiled,
                        supports mutiple source directory
  -d DST_DIR, --dst=DST_DIR
                        destination directory of js bytecode files to be
                        stored
  -c, --use_closure_compiler
                        Whether to use closure compiler to compress all js
                        files into just a big file
  -o COMPRESSED_FILENAME, --output_compressed_filename=COMPRESSED_FILENAME
                        Only available when '-c' option was True
  -j COMPILER_CONFIG, --compiler_config=COMPILER_CONFIG
                        The configuration for closure compiler by using JSON,
                        please refer to compiler_config_sample.json

下面说一下可选参数,可选参数的意思是使用closure compiler工具压缩代码为一个文件。

COMPRESSED_FILENAME是压缩后的文件名,最好使用xxx.js,因为工具会自动再后面加个c

COMPILER_CONFIG是压缩时调用的配置文件,需要根据项目需求自己填写,在bin目录下有一个做好的缺省例子可以使用,compiler_config_sample.json

android

由于android下先要执行pro.android/build_native.sh脚本,所以我们可以在脚本里加入自动编译的功能,代码如下:

COCOS2D_CONSOLE=$COCOS2DX_ROOT/tools/cocos2d-console/console/
python $COCOS2D_CONSOLE/cocos2d.py jscompile -s assets/script/ -d assets/src/;
cd assets/src;
find .-type f -name "*.js"| xargs rm -rf;
cd ../../;

以上代码要放在调用NDK编译之前。

ios

由于ios下编译没有对应的build_native.sh脚本,所以就没法使用类似的方式了。幸好xcode下编译时可以添加自定义的run script

选择 项目 -> TARGETS -> Build Phases -> Add Build Phases -> Add Run Script,添加如下的内容:

source ~/.bash_profile
CONSOLE_PATH=$COCOS2DX_ROOT"/tools/cocos2d-console/console/";
TARGET_DIR=$BUILT_PRODUCTS_DIR"/"$PRODUCT_NAME".app/";
JS_DIR=$TARGET_DIR"/src/";/usr/bin/python $CONSOLE_PATH/cocos2d.py jscompile -s $JS_DIR  -d $JS_DIR;
find $JS_DIR -type f -name "*.js"| xargs rm -rf;

这样在编译时就会自动调用该脚本。(需要在.bash_profile里定义cocos2dx的根目录,并且变量名为COCOS2DX_ROOT)

将js编译为字节码发布,就不用担心别人可以拿到js源代码了。

原文地址:https://www.cnblogs.com/linn/p/3556112.html