If you’re a Flex developer, SWC’s are nothing new to you. But for Flash dev’s, SWC files are one of those ‘yea I’ve seen those, but I don’t totally understand how, what, or why I’m supposed to use them’ things in Flash development. They’re incredibly useful – if you know exactly how to use them. Especially in CS3 (CS4 made it a little less painless with the explicit abiliy to specify SWC files as internal or external libraries). Unfortunately, generating them can be even more confusing. there just isn’t much documentation, and if you use the flex sdk and/or FlashDevelop, the compc.exe command line options are daunting.
Luckily, it isn’t as hard as it seems. For developers, one of the best uses for SWC (Flash builds require a matching SWF file as well) files in flash AS3 dev is packing up pre compiled libraries for use at runtime ( you can also package assets, but that isn’t what we are here to talk about). It’s pretty straightforward using the export SWC option under publish settings. But what if you need to export only a portion of the package as a SWC/SWF library?
I did some searching, and the few topics I found that mentioned this had replies that questioned the merits of the idea instead of proposing how to go about it. In my case, for Fosfr, I was looking to produce an external library file, but needed to include about 13kb of the entire 50kb package in the core SWF file to properly launch the loading process of the framework, which would then go and get the rest of the library before running any code that needed the full framework library. So there are legitimate cases where you may want to do this. And actually, it can be done.
So, how to go about it? The problem lies in that generating SWC files pulls all classes referenced from your chosen classes along with them into the SWC. so, say you are using TweenLite in your package that you wish to turn into a SWC. Check the SWC afterwards and you’ll see that the TweenLite package got sucked in there too. So how can this be avoided? To do it you’ll need to use one of the powers of SWC’s themselves -compile time class referencing – and one of their usage nuisances – priority of classpaths over SWCs in Flash.
Here’s the steps to generate a partial package SWC:
1 – Create an FLA to make a SWC and generate a full SWC of the entire package you wish to generate as a SWC. Dont worry about file size or excess files, this file is for reference.
2 – Take the SWC and place it next to that same FLA (CS3) or in publish setting specify the SWC as an external library (CS4). Now, go back to your class package. Save a backup copy first, then in the package folder, erase all of the classes you’d like to EXCLUDE from the SWC library.
3 – Generate another SWC (give it a different name). If you did this right, this new SWC will only contain the classes you have left in the package folder. Since the Flash compiler prioritizes classpaths over SWC files, the compiler found the source for those files in the right place, so it placed them in the new SWC. When it got to one it couldnt find, it then searched your external library SWC’s, in this case the one holding the whole package and said ‘yes, here it is, it compiles and I’m cool with it’. Since it is an external library though, it doesn’t pull the class into the new SWC. The result? a partial package SWC and SWF library, containing only the .as files you explicitly wanted contained inside.
This leaves two open ends.
First, you must be positive that you aren’t referencing any of the classes you left out before you load in the full library for use (remember proper loader context, ApplicationDomain.currentDomain, to access those classes at runtime).
Second, what about that full library? now you have a full library that has files you’ve since duplicated in the new smaller SWC. Well, you can, if you wish, reverse the process and go back and use this new smaller SWC as an external lib and remove the classes contained inside from your package, and recompile. What you’ll wind up with is a full package split cleanly between two SWC files. Pretty nifty. It’s a fair amount of work, and it should be easier, and maybe it is – if someone has a better solution, please share!