Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for external js/css sources file + closure language_in compiler option #24

Merged
merged 1 commit into from
Sep 5, 2013

Conversation

ssaarela
Copy link
Contributor

New configuration options:

jsSourcesFile: a file containing new-line separated list of JS source files. Alternative for jsSourceFiles.

cssSourcesFile: a file containing new-line separated list of CSS source files. Alternative for cssSourceFiles.

closureLanguageIn: Google Closure language_in compiler option (of type enum LanguageMode).

External source files helps streamlining development: same list of files can be used to render individual <script> -tags for scripts so that there's no need to recompile the bundle each time something is changed - browser's reload is enough.

@ssaarela
Copy link
Contributor Author

Argh! I have no idea what happend with the diff! Locally it is just fine - i.e. showing only actually changed lines.

@ssaarela
Copy link
Contributor Author

This is how the diff should look like:

diff --git src/main/java/com/samaxes/maven/minify/plugin/MinifyMojo.java src/main/java/com/samaxes/maven/minify/plugin/MinifyMojo.java
index b6fc1a4..356e6ce 100644
--- src/main/java/com/samaxes/maven/minify/plugin/MinifyMojo.java
+++ src/main/java/com/samaxes/maven/minify/plugin/MinifyMojo.java
@@ -20,6 +20,9 @@
  */
 package com.samaxes.maven.minify.plugin;

+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -28,11 +31,20 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;

+import javax.annotation.Nullable;
+
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;

+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.io.Files;
+import com.google.javascript.jscomp.CompilerOptions.LanguageMode;

 /**
  * Goal for combining and minifying CSS and JavaScript files.
@@ -42,6 +54,24 @@ import com.google.common.base.Strings;
  */
 public class MinifyMojo extends AbstractMojo {

+    private static final Function<String, String> TRIM_AND_REMOVE_COMMENTS = new Function<String, String>() {
+        @Override
+        public String apply(@Nullable String input) {
+            if (input == null) {
+                return null;
+            } else {
+                String result = input.trim();
+                if (result.startsWith("#")) {
+                    return null;
+                } else {
+                    return Strings.emptyToNull(result);
+                }
+            }
+        }
+    };
+
+    private static final Predicate<String> NOT_NULL = Predicates.<String>not(Predicates.<String>isNull());
+
     /**
      * Webapp source directory.
      *
@@ -78,6 +108,13 @@ public class MinifyMojo extends AbstractMojo {
     private ArrayList<String> cssSourceFiles;

     /**
+     *  A file containing a new-line separated list of CSS source file names.
+     *
+     * @parameter expression="${cssSourcesFile}" alias="csssFiles"
+     */
+    private String cssSourcesFile;
+
+    /**
      * JavaScript source filenames list.
      *
      * @parameter expression="${jsSourceFiles}" alias="jsFiles"
@@ -85,6 +122,13 @@ public class MinifyMojo extends AbstractMojo {
     private ArrayList<String> jsSourceFiles;

     /**
+     * A file containing a new-line separated list of JavaScript source file names.
+     *
+     * @parameter expression="${jsSourcesFile}" alias="jssFile"
+     */
+    private String jsSourcesFile;
+
+    /**
      * CSS files to include. Specified as fileset patterns which are relative to the CSS source directory.
      *
      * @parameter expression="${cssSourceIncludes}" alias="cssIncludes"
@@ -277,6 +321,13 @@ public class MinifyMojo extends AbstractMojo {
     private String jsEngine;

     /**
+     * Closure language-in compiler option
+     *
+     * @parameter expression="${closureLanguageIn}"
+     */
+    private LanguageMode closureLanguageIn;
+
+    /**
      * Executed when the goal is invoked, it will first invoke a parallel lifecycle, ending at the given phase.
      */
     @Override
@@ -291,6 +342,12 @@ public class MinifyMojo extends AbstractMojo {
         if (Strings.isNullOrEmpty(jsTargetDir)) {
             jsTargetDir = jsSourceDir;
         }
+        if (!Strings.isNullOrEmpty(jsSourcesFile)) {
+            jsSourceFiles = getFiles(jsSourcesFile);
+        }
+        if (!Strings.isNullOrEmpty(cssSourcesFile)) {
+            cssSourceFiles = getFiles(cssSourcesFile);
+        }

         Collection<ProcessFilesTask> processFilesTasks = new ArrayList<ProcessFilesTask>();
         processFilesTasks.add(new ProcessCSSFilesTask(getLog(), bufferSize, debug, skipMerge, skipMinify,
@@ -299,7 +356,7 @@ public class MinifyMojo extends AbstractMojo {
         processFilesTasks.add(new ProcessJSFilesTask(getLog(), bufferSize, debug, skipMerge, skipMinify,
                 webappSourceDir, webappTargetDir, jsSourceDir, jsSourceFiles, jsSourceIncludes, jsSourceExcludes,
                 jsTargetDir, jsFinalFile, suffix, nosuffix, charset, linebreak, jsEngine, !nomunge, verbose,
-                preserveAllSemiColons, disableOptimizations));
+                preserveAllSemiColons, disableOptimizations, closureLanguageIn));

         ExecutorService executor = Executors.newFixedThreadPool(2);
         try {
@@ -317,4 +374,15 @@ public class MinifyMojo extends AbstractMojo {
             throw new MojoFailureException(e.getMessage(), e);
         }
     }
+
+    private ArrayList<String> getFiles(String bundleDescriptor) throws MojoFailureException {
+        try {
+            Iterable<String> files = Files.readLines(new File(bundleDescriptor), Charset.forName(charset));
+            files = Iterables.transform(files, TRIM_AND_REMOVE_COMMENTS);
+            files = Iterables.filter(files, NOT_NULL);
+            return Lists.newArrayList(files);
+        } catch (IOException e) {
+            throw new MojoFailureException(e.getMessage(), e);
+        }
+   }
 }
diff --git src/main/java/com/samaxes/maven/minify/plugin/ProcessJSFilesTask.java src/main/java/com/samaxes/maven/minify/plugin/ProcessJSFilesTask.java
index 1d68c01..410ff4c 100644
--- src/main/java/com/samaxes/maven/minify/plugin/ProcessJSFilesTask.java
+++ src/main/java/com/samaxes/maven/minify/plugin/ProcessJSFilesTask.java
@@ -38,6 +38,7 @@ import com.google.javascript.jscomp.CompilationLevel;
 import com.google.javascript.jscomp.Compiler;
 import com.google.javascript.jscomp.CompilerOptions;
 import com.google.javascript.jscomp.SourceFile;
+import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
 import com.samaxes.maven.minify.common.JavaScriptErrorReporter;
 import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

@@ -56,6 +57,8 @@ public class ProcessJSFilesTask extends ProcessFilesTask {

     private final boolean disableOptimizations;

+    private final LanguageMode closureLanguageIn;
+
     /**
      * Task constructor.
      *
@@ -87,7 +90,8 @@ public class ProcessJSFilesTask extends ProcessFilesTask {
             String webappSourceDir, String webappTargetDir, String inputDir, List<String> sourceFiles,
             List<String> sourceIncludes, List<String> sourceExcludes, String outputDir, String outputFilename,
             String suffix, boolean nosuffix, String charset, int linebreak, String jsEngine, boolean munge,
-            boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations) {
+            boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations,
+            LanguageMode closureLanguageIn) {
         super(log, bufferSize, debug, skipMerge, skipMinify, webappSourceDir, webappTargetDir, inputDir, sourceFiles,
                 sourceIncludes, sourceExcludes, outputDir, outputFilename, suffix, nosuffix, charset, linebreak);

@@ -96,6 +100,7 @@ public class ProcessJSFilesTask extends ProcessFilesTask {
         this.verbose = verbose;
         this.preserveAllSemiColons = preserveAllSemiColons;
         this.disableOptimizations = disableOptimizations;
+        this.closureLanguageIn = closureLanguageIn;
     }

     /**
@@ -120,6 +125,9 @@ public class ProcessJSFilesTask extends ProcessFilesTask {
                 CompilerOptions options = new CompilerOptions();
                 CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
                 options.setOutputCharset(charset);
+                if (closureLanguageIn != null) {
+                    options.setLanguageIn(closureLanguageIn);
+                }

                 SourceFile input = SourceFile.fromInputStream(mergedFile.getName(), in);
                 List<SourceFile> externs = Collections.emptyList();

@samaxes
Copy link
Owner

samaxes commented Jun 4, 2013

I fail to see the benefits of the new options cssSourcesFile and jsSourcesFile. Am I missing something here?
However, allowing the user to set the LanguageMode is indeed useful.

@dms-it
Copy link
Contributor

dms-it commented Aug 12, 2013

Maybe better idea is to have a separate model file in XML format. Like wro4j does. They put resources in groups.

@ghost ghost assigned samaxes Aug 22, 2013
@samaxes samaxes merged commit 14ced0b into samaxes:master Sep 5, 2013
@Halayem
Copy link

Halayem commented Apr 26, 2016

i can't find in the maven repository the version of minify-maven-plugin that include the 'jsSourcesFile' capability, can you help me please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants