The following steps describe how to create a new module in Flex Builder. After you create a new module, you can compile it.
Create modules in Flex Builder
Select Do not Optimize to include all classes in the module, whether or not they are defined in the main application. This can improve the performance of the incremental compilation. In addition, you can load the module into any application, not just the application that you select here, because it has all of its dependencies compiled into it.
The following example shows the default contents of this new application:
<?xml version="1.0" encoding="utf-8"?> <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300"> </mx:Module>
In Flex Builder, you can either run the module as if it were an application or you can build the module's project. If the modules are in the same project as your Flex application, then when you run your application, Flex Builder compiles the modules' SWF files for you. The SWF files are then loaded into the application at run time.
You cannot run the module-based SWF file as a stand-alone Flash application or load it into a browser window. It must be loaded by an application as a module. If you run the module in Flex Builder to compile it, you should close Adobe Flash Player or browser window and ignore any errors. Modules should not be requested by the Player or through a browser directly.
The module SWF files and main application SWF file are typically in the same directory, although Flex Builder compiles the modules at the same time as your application, regardless of their location (they can be in the same directory as the application or in subdirectories).
You can also create a separate Flex or ActionScript project for each module or for groups of modules. This gives you greater control over how modules are compiled because each project can have different compiler options than the application or other modules. It also lets you compile the module's project or projects without compiling the application. However, this approach requires that you manually compile each module before compiling the application, unless you compile all open projects in Flex Builder at one time.
If you compile modules separately from the main application, you must be sure to include or exclude debugging information, based on whether you want to debug your application and modules. For more information, see Debugging modules in Flex Builder.
The Flex Builder workflow is designed around associating modules with a single application. If you want to use modules across multiple applications, consider encapsulating the code in a library component or class and including that in a simple module for each application. Modules are not intended to be used for cross-application code reuse; that is for libraries.
When you set up your project's architecture, you can decide to include modules in your application's project, create a separate project for each module, or create a separate project for all modules.
Using one project for each module has the following benefits:
Using one project for each module has the following drawbacks:
A related approach is to use a single project for all modules, while keeping the application in its own separate project. This has some of the drawbacks of using a single project for both the application and the modules, but it has many of the same benefits as using a separate project for each module.
Using one project for all modules has the following benefits:
Using one module project for all modules has the following drawbacks:
When creating a separate project for modules, you change the module project's output folder to a directory that is used by the application. You also suppress the generation of wrapper files.
Create a separate project for modules in Flex Builder
${DOCUMENTS}\MainProject\assets
This redirects the output of your module's compilation to your application project's (MainProject) assets directory. In your main application, you can point the ModuleLoader url property to the SWF files in the assets directory. The value of this property is relative to the output folder.
Compiling multiple projects in Flex Builder is a common operation. First you choose the order in which you want the projects to be compiled and then you compile all projects at the same time.
Compile all projects at the same time in Flex Builder
From the main menu, select Project > Build All.
Flex builds all projects in the workspace. The application files are added to each project's output folder. If you haven't already chosen to save files automatically before a build begins, you are prompted to save the files.
If you want to change the build order, you use the Build Order dialog box. This is not always necessary. Projects that use modules need to be compiled only by the time the main project application runs, not as it is compiled. In most cases, the default build order is adequate.
However, if you want to eliminate overlapping dependencies, you might need to change the build order so that the main application is compiled first. At that time, you use the link-report compiler option to generate the linker report. When you compile the modules, you use the load-externs compiler option to use the linker report that was just generated by the shell application. For more information on reducing module size, see Optimizing modules in Flex Builder.
Change the build order of the projects
The Build Order dialog box appears.
If you create dependencies between separate projects in the workspace, the compiler automatically determines the order in which the projects are built, so these dependencies are resolved properly.
When you use a separate project for each module, you can compile a single module at a time. This can save time over compiling all projects at once, or over compiling a single project that contains all module and application files.
Compile a single module's project