[SOLVED] Problems compiling sources

Hi, I’m not sure if these are bugs or I’m doing something wrong, but I’m having the some issues when compiling a JavaScript project using the cocos console command. For ease of testingm I’m working with the default “empty/hello world” project that Cocos Code IDE creates when you select “File/New/Cocos JavaScript Project”:

  1. Compiling without -m release in the command doesn’t do anything:
    If I type cocos compile -p web all I get is “Running command: compile Building mode: debug” in the console, but no files are created. If I use cocos compile -p web -m release, I get feedback in the console and the files are created in the “publish” folder as expected and they work just fine.

  2. Compiling for android does not create an “.apk” file (plus nothing happens without -m release):
    If I type cocos compile -p android all I get is “Running command: compile Building mode: debug” in the console, but no files are created. If I use cocos compile -p android -m release, all I get is a “build.xml” file inside “publish/html5”, but I remember this command used to generate an “.apk” file that I could use in my Android emulator.

  3. Build succeeds, but running fails if including underscore.js
    I’ve been told that cocos2d-js should work alright with underscore.js, but if I include either the full underscore.js or minified underscore-min.js downloaded from the offficial site, after I run cocos compile -p web -m release I get a message of compilation succeeding, but when I try to run the sources I get “undefined is not a function” in the browser console in my “game.min.js”… This is really bothersome since I’m depending a lot on underscore’s functions for working with collections.

Can anyone give me a hand? (specially with problem 3).


EDIT / SOLUTION: all problems have been fixed as of Cocos2D-JS v3.0 final release.

  1. It’s normal, the debug mode for web works with the sources files, so nothing new is created
  2. release mode for android should create an apk, I’m not sure of this, I will check it out tomorrow
  3. You may need to manually copy underscore.js into the publish folder, we haven’t check all dependencies in cocos console, in fact, it’s rely on your project.json’s configuration jsList, so if you include any external javascript file in index.html, please copy them manually into the publish folder, then it will work correctly

I’m, of course, putting "src/underscore.js" in jsList inside “project.json”. Then it does get compiled and in the minified code there’s no _.something to be found, so I can’t just re-add it to the compiled code…

After re-thinking this, I tried, instead of adding underscore.js to jsList, to add it as an external javascript file in index.html, and uncompiled it works, but after minifiying the _ get turned to something else either way… Is there a way to change the behaviour of the compiler so it would leave this kind of code untouched?

So… Any news about problem 3? It’s starting to become an issue (since I want to publish my games and I need to minify my code for that).

I’ve tried to create an issue report so this takes some priority, but it seems to be gone from the Issue Tracker for some reason :confused:

Hi, Sorry for being late for this response, you can do it like this:

In underscore.js, find the definition of _, and add a comment before it:

/** @expose */
var _ = ...

Then google closure compiler will ignore it for compressing.
Hope this solution can solve your problem

Awesome!

That made it work just fine.

Big thanks! :smiley:

Frustrated update:

At this point, I thought the minified/compiled game was working, because if I go to the publish/html5 folder and run cocos run -p web the game works, but if I look at the sources with the browser’s developer tools I find out that it is actually running the code in the root folder of the project!

If I take the contents of the publish/html5 game and put the somewhere else (ie, in my web server) they still won’t work! D: (it says undefined is not a function for some line of the compiled code).

Any thoughts?

cocos run -p web -m release will run your game in release mode with the content in publish/html5

How did you add the comment I mentioned before?

Lines 48 trhu 53 of my underscore.js file look like this:

  /** @expose */
  var _ = function(obj) {
    if (obj instanceof _) return obj;
    if (!(this instanceof _)) return new _(obj);
    this._wrapped = obj;
  };

Try this:

  if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = _;
    }
    exports._ = _;
  } else {
    root._ = _;
  }

  /** @expose */
  root._;

Still no luck :frowning:

Is there a way to tell the compiler to leave that file completely unchanged to see if that fixes things?

Does the code run for you if you add that expose flag like that?

All I did was add the following to the init of the game layer in the boilerplate that’s automatically created with a new project:

var myArray= ["asd", "fgh", "jkl"];
_.forEach(myArray,function(o){cc.log(o);});

Hi, I found a solution

  1. Do not include underscore.js in your project.json’s jsList
  2. Embed it directly in your index.html
  3. Expose _ and all usage in your game code
    /** @expose */
    window._;
    /** @expose */
    _.each;
    
  4. When you publish, you need to copy manually underscore.js into the published folder

Hi!

Thanks! It works!

Althought you don’t really need to expose every used function, exposing window._ in main.js seems to work just fine.

I wonder thought: what would happen if I try to compile this into native for Android/win32/iOS?

You will need to require underscore.js manually as it doesn’t exist in jsList

if (cc.sys.isNative)
    require("underscore.js");
1 Like

OK, that’s an interesting solution.

But I’ve realized something, now (after upgrading to v3 final) if I put "underscore.js" in project.json's jsList, I don’t need to do anything else for the compilation to work! :smiley:

Has something changed in how the compiler works for Cocos2D-JS v3.0 Final?

I’m taking a look at game.min.js's code (re-formatted) and maybe I’m imagining things, but it doesn’t seem to be as obfuscated as it used to in previous releases.

We have changed a lit cocos console, now for closure compiler’s advanced mode, you need to explicitly add --advanced to cocos compile command

cocos compile -p web --advanced
// or 
cocos compile -p web -m release --advanced
1 Like

Aha! I thought it’d be something like that :stuck_out_tongue:

Thanks!