compiler-jx: minor refactor to call a function for each compilation unit instead of putting everything directly in the loop

This function will eventually be used for a file system watcher that recompiles only changed compilation units
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/JSConfiguration.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/JSConfiguration.java
index 874fdea..94f9e17 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/JSConfiguration.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/JSConfiguration.java
@@ -28,6 +28,7 @@
 
 import org.apache.royale.compiler.clients.MXMLJSC.JSTargetType;
 import org.apache.royale.compiler.config.Configuration;
+import org.apache.royale.compiler.config.ConfigurationBuffer;
 import org.apache.royale.compiler.config.ConfigurationValue;
 import org.apache.royale.compiler.exceptions.ConfigurationException;
 import org.apache.royale.compiler.exceptions.ConfigurationException.CannotOpen;
@@ -114,6 +115,25 @@
     }
 
     //
+    // 'watch'
+    //
+
+    private boolean watch = false;
+
+    public boolean getWatch()
+    {
+        return watch;
+    }
+
+    @Config
+    @Mapping("watch")
+    public void setWatch(ConfigurationValue cv, boolean value)
+            throws ConfigurationException
+    {
+        watch = value;
+    }
+
+    //
     // 'source-map'
     //
 
@@ -633,4 +653,16 @@
         jsxFactory = value;
     }
     
+    @Override
+    public void validate(ConfigurationBuffer configurationBuffer) throws ConfigurationException
+    {
+        super.validate(configurationBuffer);
+
+        if (getWatch() && !debug())
+        {
+            List<ConfigurationValue> values = configurationBuffer.getVar("watch");
+            throw new ConfigurationException.VariableMissingRequirement("debug=true", values.get(0).getVar(), values.get(0).getSource(),
+                    values.get(0).getLine());
+        }
+    }
 }
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSC.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSC.java
index 1b86589..33cfb43 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSC.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSC.java
@@ -554,73 +554,7 @@
 	                ((RoyaleJSTarget)target).collectRemoteClassMetaData(project.remoteClassAliasMap, reachableCompilationUnits);
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
-	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
-	
-	                    if (cuType == ICompilationUnit.UnitType.AS_UNIT
-	                            || cuType == ICompilationUnit.UnitType.MXML_UNIT)
-	                    {
-	                        final File outputClassFile = getOutputClassFile(
-	                                cu.getQualifiedNames().get(0), outputFolder);
-	
-                            if (config.isVerbose())
-                            {
-                                System.out.println("Compiling file: " + outputClassFile);
-                            }
-
-	                        ICompilationUnit unit = cu;
-	
-	                        IJSWriter writer;
-	                        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createWriter(project,
-	                                    errors, unit, false);
-	                        }
-	                        else
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createMXMLWriter(
-	                                    project, errors, unit, false);
-	                        }
-	
-	                        BufferedOutputStream out = new BufferedOutputStream(
-	                                new FileOutputStream(outputClassFile));
-	
-                            BufferedOutputStream sourceMapOut = null;
-	                        File outputSourceMapFile = null;
-	                        if (project.config.getSourceMap())
-	                        {
-	                            outputSourceMapFile = getOutputSourceMapFile(
-	                                    cu.getQualifiedNames().get(0), outputFolder);
-                                sourceMapOut = new BufferedOutputStream(
-                                        new FileOutputStream(outputSourceMapFile));
-	                        }
-	                        
-	                        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
-	                        out.flush();
-                            out.close();
-                            if (sourceMapOut != null)
-                            {
-                                sourceMapOut.flush();
-                                sourceMapOut.close();
-                            }
-	                        writer.close();
-	                        long fileDate = 0;
-	                    	String metadataDate = targetSettings.getSWFMetadataDate();
-	                    	if (metadataDate != null)
-	                    	{
-	                    		String metadataFormat = targetSettings.getSWFMetadataDateFormat();
-	                    		try {
-	                    			SimpleDateFormat sdf = new SimpleDateFormat(metadataFormat);
-                                    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
-                                    fileDate = sdf.parse(metadataDate).getTime();
-	                    		} catch (ParseException e) {
-	                				// TODO Auto-generated catch block
-	                				e.printStackTrace();
-	                			} catch (IllegalArgumentException e1) {
-	                				e1.printStackTrace();
-	                			}
-	                    		outputClassFile.setLastModified(fileDate);
-	                    	}
-	                    }
+                        writeCompilationUnit(cu, outputFolder);
                         ClosureUtils.collectPropertyNamesToKeep(cu, project, closurePropNamesToKeep);
                         ClosureUtils.collectSymbolNamesToExport(cu, project, closureSymbolNamesToExport);
 	                }
@@ -647,6 +581,78 @@
         return compilationSuccess;
     }
 
+    protected void writeCompilationUnit(ICompilationUnit cu, File outputFolder) throws InterruptedException, IOException
+    {
+        ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+	    if (cuType != ICompilationUnit.UnitType.AS_UNIT
+                && cuType != ICompilationUnit.UnitType.MXML_UNIT)
+        {
+            return;
+        }
+
+        final File outputClassFile = getOutputClassFile(
+                cu.getQualifiedNames().get(0), outputFolder);
+
+        if (config.isVerbose())
+        {
+            System.out.println("Compiling file: " + outputClassFile);
+        }
+
+        ICompilationUnit unit = cu;
+
+        IJSWriter writer;
+        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
+        {
+            writer = (IJSWriter) project.getBackend().createWriter(project,
+                    problems.getProblems(), unit, false);
+        }
+        else
+        {
+            writer = (IJSWriter) project.getBackend().createMXMLWriter(
+                    project, problems.getProblems(), unit, false);
+        }
+
+        BufferedOutputStream out = new BufferedOutputStream(
+                new FileOutputStream(outputClassFile));
+
+        BufferedOutputStream sourceMapOut = null;
+        File outputSourceMapFile = null;
+        if (project.config.getSourceMap())
+        {
+            outputSourceMapFile = getOutputSourceMapFile(
+                    cu.getQualifiedNames().get(0), outputFolder);
+            sourceMapOut = new BufferedOutputStream(
+                    new FileOutputStream(outputSourceMapFile));
+        }
+        
+        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
+        out.flush();
+        out.close();
+        if (sourceMapOut != null)
+        {
+            sourceMapOut.flush();
+            sourceMapOut.close();
+        }
+        writer.close();
+        long fileDate = 0;
+        String metadataDate = targetSettings.getSWFMetadataDate();
+        if (metadataDate != null)
+        {
+            String metadataFormat = targetSettings.getSWFMetadataDateFormat();
+            try {
+                SimpleDateFormat sdf = new SimpleDateFormat(metadataFormat);
+                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
+                fileDate = sdf.parse(metadataDate).getTime();
+            } catch (ParseException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            } catch (IllegalArgumentException e1) {
+                e1.printStackTrace();
+            }
+            outputClassFile.setLastModified(fileDate);
+        }
+    }
+
     /**
      * Build target artifact.
      * 
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNative.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNative.java
index 1a92629..6da3da8 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNative.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNative.java
@@ -337,56 +337,7 @@
 	                ((RoyaleJSTarget)target).collectMixinMetaData(project.mixinClassNames, reachableCompilationUnits);
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
-	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
-	
-	                    if (cuType == ICompilationUnit.UnitType.AS_UNIT
-	                            || cuType == ICompilationUnit.UnitType.MXML_UNIT)
-	                    {
-	                        final File outputClassFile = getOutputClassFile(
-	                                cu.getQualifiedNames().get(0), outputFolder);
-	
-                            if (config.isVerbose())
-                            {
-                                System.out.println("Compiling file: " + outputClassFile);
-                            }
-	
-	                        ICompilationUnit unit = cu;
-	
-	                        IJSWriter writer;
-	                        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createWriter(project,
-	                                    errors, unit, false);
-	                        }
-	                        else
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createMXMLWriter(
-	                                    project, errors, unit, false);
-	                        }
-	
-	                        BufferedOutputStream out = new BufferedOutputStream(
-	                                new FileOutputStream(outputClassFile));
-	
-                            BufferedOutputStream sourceMapOut = null;
-	                        File outputSourceMapFile = null;
-	                        if (project.config.getSourceMap())
-	                        {
-	                            outputSourceMapFile = getOutputSourceMapFile(
-	                                    cu.getQualifiedNames().get(0), outputFolder);
-                                sourceMapOut = new BufferedOutputStream(
-                                        new FileOutputStream(outputSourceMapFile));
-	                        }
-	                        
-	                        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
-	                        out.flush();
-	                        out.close();
-                            if (sourceMapOut != null)
-                            {
-                                sourceMapOut.flush();
-                                sourceMapOut.close();
-                            }
-	                        writer.close();
-	                    }
+	                    writeCompilationUnit(cu, outputFolder);
                         ClosureUtils.collectPropertyNamesToKeep(cu, project, closurePropNamesToKeep);
                         ClosureUtils.collectSymbolNamesToExport(cu, project, closureSymbolNamesToExport);
 	                }
@@ -413,6 +364,61 @@
         return compilationSuccess;
     }
 
+    protected void writeCompilationUnit(ICompilationUnit cu, File outputFolder) throws InterruptedException, FileNotFoundException, IOException
+    {
+        ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+        if (cuType != ICompilationUnit.UnitType.AS_UNIT
+                && cuType != ICompilationUnit.UnitType.MXML_UNIT)
+        {
+            return;
+        }
+
+        final File outputClassFile = getOutputClassFile(
+                cu.getQualifiedNames().get(0), outputFolder);
+
+        if (config.isVerbose())
+        {
+            System.out.println("Compiling file: " + outputClassFile);
+        }
+
+        ICompilationUnit unit = cu;
+
+        IJSWriter writer;
+        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
+        {
+            writer = (IJSWriter) project.getBackend().createWriter(project,
+                    problems.getProblems(), unit, false);
+        }
+        else
+        {
+            writer = (IJSWriter) project.getBackend().createMXMLWriter(
+                    project, problems.getProblems(), unit, false);
+        }
+
+        BufferedOutputStream out = new BufferedOutputStream(
+                new FileOutputStream(outputClassFile));
+
+        BufferedOutputStream sourceMapOut = null;
+        File outputSourceMapFile = null;
+        if (project.config.getSourceMap())
+        {
+            outputSourceMapFile = getOutputSourceMapFile(
+                    cu.getQualifiedNames().get(0), outputFolder);
+            sourceMapOut = new BufferedOutputStream(
+                    new FileOutputStream(outputSourceMapFile));
+        }
+        
+        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
+        out.flush();
+        out.close();
+        if (sourceMapOut != null)
+        {
+            sourceMapOut.flush();
+            sourceMapOut.close();
+        }
+        writer.close();
+    }
+
     /**
      * Build target artifact.
      * 
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNode.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNode.java
index 32132b4..918a3d6 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNode.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCNode.java
@@ -339,56 +339,7 @@
 	                ((RoyaleJSTarget)target).collectMixinMetaData(project.mixinClassNames, reachableCompilationUnits);
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
-	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
-	
-	                    if (cuType == ICompilationUnit.UnitType.AS_UNIT
-	                            || cuType == ICompilationUnit.UnitType.MXML_UNIT)
-	                    {
-	                        final File outputClassFile = getOutputClassFile(
-	                                cu.getQualifiedNames().get(0), outputFolder);
-	
-                            if (config.isVerbose())
-                            {
-                                System.out.println("Compiling file: " + outputClassFile);
-                            }
-	
-	                        ICompilationUnit unit = cu;
-	
-	                        IJSWriter writer;
-	                        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createWriter(project,
-	                                    errors, unit, false);
-	                        }
-	                        else
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createMXMLWriter(
-	                                    project, errors, unit, false);
-	                        }
-	
-	                        BufferedOutputStream out = new BufferedOutputStream(
-	                                new FileOutputStream(outputClassFile));
-	
-                            BufferedOutputStream sourceMapOut = null;
-	                        File outputSourceMapFile = null;
-	                        if (project.config.getSourceMap())
-	                        {
-	                            outputSourceMapFile = getOutputSourceMapFile(
-	                                    cu.getQualifiedNames().get(0), outputFolder);
-                                sourceMapOut = new BufferedOutputStream(
-                                        new FileOutputStream(outputSourceMapFile));
-	                        }
-	                        
-	                        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
-	                        out.flush();
-	                        out.close();
-                            if (sourceMapOut != null)
-                            {
-                                sourceMapOut.flush();
-                                sourceMapOut.close();
-                            }
-	                        writer.close();
-	                    }
+	                    writeCompilationUnit(cu, outputFolder);
                         ClosureUtils.collectPropertyNamesToKeep(cu, project, closurePropNamesToKeep);
                         ClosureUtils.collectSymbolNamesToExport(cu, project, closureSymbolNamesToExport);
 	                }
@@ -415,6 +366,61 @@
         return compilationSuccess;
     }
 
+    protected void writeCompilationUnit(ICompilationUnit cu, File outputFolder) throws InterruptedException, IOException
+    {
+        ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+        if (cuType != ICompilationUnit.UnitType.AS_UNIT
+                && cuType != ICompilationUnit.UnitType.MXML_UNIT)
+        {
+            return;
+        }
+
+        final File outputClassFile = getOutputClassFile(
+                cu.getQualifiedNames().get(0), outputFolder);
+
+        if (config.isVerbose())
+        {
+            System.out.println("Compiling file: " + outputClassFile);
+        }
+
+        ICompilationUnit unit = cu;
+
+        IJSWriter writer;
+        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
+        {
+            writer = (IJSWriter) project.getBackend().createWriter(project,
+                    problems.getProblems(), unit, false);
+        }
+        else
+        {
+            writer = (IJSWriter) project.getBackend().createMXMLWriter(
+                    project, problems.getProblems(), unit, false);
+        }
+
+        BufferedOutputStream out = new BufferedOutputStream(
+                new FileOutputStream(outputClassFile));
+
+        BufferedOutputStream sourceMapOut = null;
+        File outputSourceMapFile = null;
+        if (project.config.getSourceMap())
+        {
+            outputSourceMapFile = getOutputSourceMapFile(
+                    cu.getQualifiedNames().get(0), outputFolder);
+            sourceMapOut = new BufferedOutputStream(
+                    new FileOutputStream(outputSourceMapFile));
+        }
+        
+        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
+        out.flush();
+        out.close();
+        if (sourceMapOut != null)
+        {
+            sourceMapOut.flush();
+            sourceMapOut.close();
+        }
+        writer.close();
+    }
+
     /**
      * Build target artifact.
      * 
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
index 69b3b98..cf1a615 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyale.java
@@ -373,56 +373,7 @@
 	                }
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
-	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
-	
-	                    if (cuType == ICompilationUnit.UnitType.AS_UNIT
-	                            || cuType == ICompilationUnit.UnitType.MXML_UNIT)
-	                    {
-	                        final File outputClassFile = getOutputClassFile(
-	                                cu.getQualifiedNames().get(0), outputFolder);
-    
-                            if (config.isVerbose())
-                            {
-                                System.out.println("Compiling file: " + outputClassFile);
-                            }
-	
-	                        ICompilationUnit unit = cu;
-	
-	                        IJSWriter writer;
-	                        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createWriter(project,
-	                                    problems.getProblems(), unit, false);
-	                        }
-	                        else
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createMXMLWriter(
-	                                    project, problems.getProblems(), unit, false);
-	                        }
-	
-	                        BufferedOutputStream out = new BufferedOutputStream(
-	                                new FileOutputStream(outputClassFile));
-
-                            BufferedOutputStream sourceMapOut = null;
-	                        File outputSourceMapFile = null;
-	                        if (project.config.getSourceMap())
-	                        {
-	                            outputSourceMapFile = getOutputSourceMapFile(
-                                        cu.getQualifiedNames().get(0), outputFolder);
-                                sourceMapOut = new BufferedOutputStream(
-	                                    new FileOutputStream(outputSourceMapFile));
-	                        }
-	                        
-	                        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
-	                        out.flush();
-	                        out.close();
-                            if (sourceMapOut != null)
-                            {
-                                sourceMapOut.flush();
-                                sourceMapOut.close();
-                            }
-	                        writer.close();
-	                    }
+                        writeCompilationUnit(cu, outputFolder);
                         ClosureUtils.collectPropertyNamesToKeep(cu, project, closurePropNamesToKeep);
                         ClosureUtils.collectSymbolNamesToExport(cu, project, closureSymbolNamesToExport);
 	                }
@@ -464,6 +415,61 @@
         return compilationSuccess && (errs.size() == 0);
     }
 
+    protected void writeCompilationUnit(ICompilationUnit cu, File outputFolder) throws InterruptedException, IOException
+    {
+        ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+        if (cuType != ICompilationUnit.UnitType.AS_UNIT
+                && cuType != ICompilationUnit.UnitType.MXML_UNIT)
+        {
+            return;
+        }
+
+        final File outputClassFile = getOutputClassFile(
+                cu.getQualifiedNames().get(0), outputFolder);
+
+        if (config.isVerbose())
+        {
+            System.out.println("Compiling file: " + outputClassFile);
+        }
+
+        ICompilationUnit unit = cu;
+
+        IJSWriter writer;
+        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
+        {
+            writer = (IJSWriter) project.getBackend().createWriter(project,
+                    problems.getProblems(), unit, false);
+        }
+        else
+        {
+            writer = (IJSWriter) project.getBackend().createMXMLWriter(
+                    project, problems.getProblems(), unit, false);
+        }
+
+        BufferedOutputStream out = new BufferedOutputStream(
+                new FileOutputStream(outputClassFile));
+
+        BufferedOutputStream sourceMapOut = null;
+        File outputSourceMapFile = null;
+        if (project.config.getSourceMap())
+        {
+            outputSourceMapFile = getOutputSourceMapFile(
+                    cu.getQualifiedNames().get(0), outputFolder);
+            sourceMapOut = new BufferedOutputStream(
+                    new FileOutputStream(outputSourceMapFile));
+        }
+        
+        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
+        out.flush();
+        out.close();
+        if (sourceMapOut != null)
+        {
+            sourceMapOut.flush();
+            sourceMapOut.close();
+        }
+        writer.close();
+    }
+
     private void generateExternsReport(File externsReportFile,
 			List<ICompilationUnit> reachableCompilationUnits,
 			ProblemQuery problems) {
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyaleCordova.java b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyaleCordova.java
index a508ec2..5014d70 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyaleCordova.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/clients/MXMLJSCRoyaleCordova.java
@@ -342,56 +342,7 @@
 	                ((RoyaleJSTarget)target).collectMixinMetaData(project.mixinClassNames, reachableCompilationUnits);
 	                for (final ICompilationUnit cu : reachableCompilationUnits)
 	                {
-	                    ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
-	
-	                    if (cuType == ICompilationUnit.UnitType.AS_UNIT
-	                            || cuType == ICompilationUnit.UnitType.MXML_UNIT)
-	                    {
-	                        final File outputClassFile = getOutputClassFile(
-	                                cu.getQualifiedNames().get(0), outputFolder);
-	
-                            if (config.isVerbose())
-                            {
-                                System.out.println("Compiling file: " + outputClassFile);
-                            }
-	
-	                        ICompilationUnit unit = cu;
-	
-	                        IJSWriter writer;
-	                        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createWriter(project,
-	                                    errors, unit, false);
-	                        }
-	                        else
-	                        {
-	                            writer = (IJSWriter) project.getBackend().createMXMLWriter(
-	                                    project, errors, unit, false);
-	                        }
-	
-	                        BufferedOutputStream out = new BufferedOutputStream(
-	                                new FileOutputStream(outputClassFile));
-	
-                            BufferedOutputStream sourceMapOut = null;
-	                        File outputSourceMapFile = null;
-	                        if (project.config.getSourceMap())
-	                        {
-	                            outputSourceMapFile = getOutputSourceMapFile(
-	                                    cu.getQualifiedNames().get(0), outputFolder);
-                                sourceMapOut = new BufferedOutputStream(
-                                        new FileOutputStream(outputSourceMapFile));
-	                        }
-	                        
-	                        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
-	                        out.flush();
-	                        out.close();
-                            if (sourceMapOut != null)
-                            {
-                                sourceMapOut.flush();
-                                sourceMapOut.close();
-                            }
-	                        writer.close();
-	                    }
+	                    writeCompilationUnit(cu, outputFolder);
                         ClosureUtils.collectPropertyNamesToKeep(cu, project, closurePropNamesToKeep);
                         ClosureUtils.collectSymbolNamesToExport(cu, project, closureSymbolNamesToExport);
 	                }
@@ -418,6 +369,61 @@
         return compilationSuccess;
     }
 
+    protected void writeCompilationUnit(ICompilationUnit cu, File outputFolder) throws InterruptedException, IOException
+    {
+        ICompilationUnit.UnitType cuType = cu.getCompilationUnitType();
+        if (cuType != ICompilationUnit.UnitType.AS_UNIT
+                && cuType != ICompilationUnit.UnitType.MXML_UNIT)
+        {
+            return;
+        }
+
+        final File outputClassFile = getOutputClassFile(
+                cu.getQualifiedNames().get(0), outputFolder);
+
+        if (config.isVerbose())
+        {
+            System.out.println("Compiling file: " + outputClassFile);
+        }
+
+        ICompilationUnit unit = cu;
+
+        IJSWriter writer;
+        if (cuType == ICompilationUnit.UnitType.AS_UNIT)
+        {
+            writer = (IJSWriter) project.getBackend().createWriter(project,
+                    problems.getProblems(), unit, false);
+        }
+        else
+        {
+            writer = (IJSWriter) project.getBackend().createMXMLWriter(
+                    project, problems.getProblems(), unit, false);
+        }
+
+        BufferedOutputStream out = new BufferedOutputStream(
+                new FileOutputStream(outputClassFile));
+
+        BufferedOutputStream sourceMapOut = null;
+        File outputSourceMapFile = null;
+        if (project.config.getSourceMap())
+        {
+            outputSourceMapFile = getOutputSourceMapFile(
+                    cu.getQualifiedNames().get(0), outputFolder);
+            sourceMapOut = new BufferedOutputStream(
+                    new FileOutputStream(outputSourceMapFile));
+        }
+        
+        writer.writeTo(out, sourceMapOut, outputSourceMapFile);
+        out.flush();
+        out.close();
+        if (sourceMapOut != null)
+        {
+            sourceMapOut.flush();
+            sourceMapOut.close();
+        }
+        writer.close();
+    }
+
     /**
      * Build target artifact.
      *