HTML5 -minify flag and anticache in Assets

Hello!

  1. When i build my HTML5-target game with ‘-minify’ flag i can’t call any ExternalInterface methods. For example
ExternalInterface.call("console.log", "TEST TEST");

works perfect without -minify flag. How can i keep my EI functionality?

  1. And another question. Every my asset automatically receives to its url anticache number like .../sounds/result.mp3?782875. Every build number is new . Can i manage it by myself or disable it?

Thanks!

You can try using @:keep on your unreferenced methods to prevent Haxe dead-code-elimination from removing them

@:keep public function myFunction ():Void {
   ...
}

Is the cache-break string hurting something in your project? We do this in order to prevent like-named assets from being wrongfully cached if it changed in your build. The cache break value does not change after your project is compiled, so it will still cache properly for new users

Yes, i know about @:keep and @:keepSub, but i just put example code in constructor of my Main class and nothing happend with -minify. Meta @:keep didn’t help too.

@:keep
class Main extends Sprite 
{
	@:keep
	public function new() 
	{
		super();
		
		ExternalInterface.call("alert", "foobarstring");
	}
}

I can find in output js file my marker string:

var Main = function() {
	openfl_display_Sprite.call(this);
	openfl_external_ExternalInterface.call("alert","foobarstring");
};

After minifying i can’t find it there…


And i'd like to create list of all my assets with hashes and add hash to load requests. Is it possible?

Hmm, perhaps the @:keep is preserving it during the Haxe dead-code elimination step, but minifying (which is unaffected by the flag) may be doing something to it.

Oh, wait, is it not finding “Main” or is it not finding something in “ExternalInterface”?

If you can handle using the same hash value for all your assets, the current behavior is customizable. Otherwise, we’d have to talk more about the value of a unique hash value per asset, and whether we could support that officially

I’ve found EI call in output JS file but i haven’t found it in minified version:

Original:

@:keep
class Main extends Sprite 
{

	@:keep
	public function new() 
	{
		super();
		trace("foobar");
		ExternalInterface.call("alert", "foobarstring");
	}
}

Output:

var Main = function() {
	openfl_display_Sprite.call(this);
	haxe_Log.trace("foobar",{ fileName : "Main.hx", lineNumber : 20, className : "Main", methodName : "new"});
	openfl_external_ExternalInterface.call("alert","foobarstring");
};

Minified:

var le=function() {
	Lb.call(this);
	nc.trace("foobar",{fileName:"Main.hx",lineNumber:20,className:"Main",methodName:"new"})
};
q.Main=le;
le.__name__=["Main"];
le.__super__=Lb;

Is there another way to call external JS method?

Interesting, thanks for the sample. This sounds like its worth looking into.

In the meantime, you could do something like the following:

#if html5
untyped __js__ ("alert ('foobarstring')");
#end

…or

#if html5
import js.Browser;
#end

...

#if html5
Browser.alert ("Hello!");
#end

…or

stage.window.alert ("Hello!");

There’s a lot of ways to access and write JavaScript directly when you’re publishing to the HTML5 target :slight_smile:

EDIT: …and in this exact case, the Lime window.alert API should also work cross-target, so it shows an alert pop-up on the desktop as well (no support for mobile yet)

Thanks!
‘alert’ was only for example, i call my own custom functions and untyped, i think, will be good for me :slight_smile:

You can even do something like:

package my.custom.code;


@:native("MyCustomCode")
extern class MyCode {
    
    public static function alert (message:String):Void;
    public static function otherMethod ():Void;

}

then

import my.custom.code.MyCode;

...

MyCode.alert ("Hello");

turns into

MyCode.alert ("Hello");

in the final output. The @:native meta is optional – use it if your package + class name is different than what the JavaScript object reference is. This is a type-safe way of using JavaScript. For example, we do extern classes for using Howler.js in Lime:

Yes, it works perfectly! Thanks)