What if you have a bunch of code that is only used for debugging your application and it is making your compiled swf a little heavy. You don't really want this code in your production environment and you would like a way to compile different versions of your swf based on some kind of conditional to define your swfs environment.
The MXMLC has a built in way to handle conditional compiling! It is done by defining constants for the compiler and then referencing these in your code. It has a couple of odd things you have to take into account but it works quite well.
The first thing that you have to do to set this up is define your constants for the compiler to evaluate against. These can be set in a number of ways.
Setting up your constants
Command line argument.
-define=CONFIG::DEBUG,true
From within the flex-config.xml file
<compiler>
<define>
<name>CONFIG::DEBUG</name>
<value>true</value>
</define>
</compiler>
Or from a Flex ANT task
<mxmlc ... >
<define name="CONFIG::DEBUG" value="true"/>
</mxmlc>
All of these are the same thing written in different environments.
Using these constants to exclude include or exclude parts of your code
package {
public class Logger extends Sprite
{
public function Logger() {
super();
}
public static function log(...message):void {
CONFIG::DEBUG{
trace.apply(null,message);
}
}
}
}
As you can see pretty simple, if CONFIG::DEBUG equals false this trace command would end up being striped out of your compiled swf and no trace would be made.
These constants can also be other values like Strings or Numbers but you are not able to do the more complicated conditional statements within your code. For instance if you have a three environment variables debug, staging, and production, you could not use operators like &&, ||, >= to determined anything. These values are evaluated by the compiler first and then simply placed into your code. If a block is defined like above and the value is false it will strip out the contents of this code from the compiled swf.
If you do need to evaluate things based on different constants you can define this up front like this.
-define+=CONFIG::myConst,"1 > 2"
or even using other constants within these equations.
-define+=CONFIG::bool2,false -define+=CONFIG::and1,"CONFIG::bool2 && false"
If you are looking for more information on this subject you can see the Adobe docs here livedocs.adobe.com/flex/3/html/help.html
+