Managing the size of the platform

Managing the size of the platform
0.0 0


I’m wondering with the upcoming package management tools for cocos2d-html5 if the ability to avoid including code supporting WebGL is going to be included.

Currently I have been culling the size of my code and the cocos platform so it can be delivered as quickly as possible to mobile clients.

Currently I bundle my code and the cocos code in a single closured js file. It ends up being about 434k in total and 134k when zipped. The 434k is significant because JS on mobile devices is slow to load (and even more so in WebViews). The cocos portion represents 300k of the 434k and this is cocos on a diet as well (I’ve removed a number of files that my product does not currently use like particle effects and some of the complex math).

I haven’t gone to the level of commenting out what is essentially dead code on mobile (i.e. the webGL versions of all the basic nodes) and I can’t really see a way in the current code construction for the closure compiler to see that it is dead (maybe someone knows how to do this and I am going about it wrong).

I think ideally the WebGL and the Canvas implementations of these nodes might be better served in separate files so that both manually (or automatically via a package manager) a super lightweight mobile version could be generated by file level exclusion. :slight_smile:

The alternative would be for me to comment out the code manually in all these files and do it each time for every cocos drop I integrate. :frowning:


Hi, Alan

Good news for you, we have done the work you need recently, you can check out the branch NPM on github:
I don’t know if you know node.js, in fact, we use NPM, the module management of node.js to separate cocos2d-html5 in packages. You can select whatever you want or don’t want via cocos-utils: an node.js tool to help you install, config and publish your project, it’s on github:

It’s a new try of our group, it may be not so easy to use. So all suggestion and critics is welcomed. Please help us to improve it if you find it useful. Thank you very much!



I may have answered my own question after digging a little based on my own posting

It might be possible to force the value of cc.renderMode to be cc.CANVAS on and have the closure compiler strip out the code for me.


Thanks Huabin,

I will take a look and see if it helps.



We hope you can use our NPM tool to manager packages instead of use the trick of closure complier. This can helps us to improve and it’s much easier to control everything also. Thanks!


Still getting to playing around with the NPM tool but I have found a simple way to shrink the cocos platform code by 5%.

I’ve been looking at removing the cc.log calls (and their subsequent allocation of memory for the strings) in builds.

What I do is add a closure controlled definition to the project.

/** @define {boolean} */
var ccLogEnabled = true;

Then in a particular ant build I can use the following directive to set it to false to indicate unreachable code.

<define name="ccLogEnabled" value="false" />

In order to remove code with the closure compiler it needs to be contained such as

if (ccLogEnabled) {
 ... do blah

The whole code inside and the if statement will be stripped by closure (I can’t yet get it to recognize that a piece of code relies on essentially an empty function to get it to strip which would be nicer and easier).

To strip out the cc.log calls I add the if (ccLogEnabled) in front of the cc.log call. This can be done in a preprocessor task that is reversible if not wanted to be permanent. It is essentially a search and replace of cc.log( with if (ccLogEnable) cc.log(. So a cc.log statement in the platform looks like

if (ccLogEnabled) cc.log("cocos2d: ERROR: Failed to compile shader:\n" + this._glContext.getShaderSource(shader));

The resulting advanced closure compiled js file is about 5% smaller. This doesn’t negate the NPM package management but should in addition allow for the removal of “unused” code and memory in the final platform.


We hope that you can try our cocos-utils and give suggestions to us.:slight_smile:
In cocos-utils, we have done the function to delete cc.log in code by using "delLog" : true in cocos.json.
We haven’t used the method like you because we considered other reasons, and we will make the utils work better and better for developers.