4

We are using wro4j with Google Closure and Maven to minify our JS. By default it does not suport strict mode in the JS ("use strict";).. it just strips it out. Is there any configuration I can do in pom.xml or somewhere else to get it to leave use strict in there?

This is the configuration for google closure complier to do it:

--language_in=ECMASCRIPT5_STRICT

Not sure how to plug that in to Wro4j. Any ideas?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
bshack
  • 1,891
  • 1
  • 21
  • 27
  • Can you provide an example of the script you are trying to minimize. Also what is the expected and actual output? This may be rather a problem of Google Closure... and not a wro4j one. – Alex Objelean Dec 15 '12 at 11:20

2 Answers2

1

Create a custom implementation of the manager factory which adds ECMAScript5:

public class MyCustomWroManagerFactory
extends DefaultStandaloneContextAwareManagerFactory
  {
  @Override 
    protected ProcessorsFactory newProcessorsFactory() 
      {
      final SimpleProcessorsFactory factory = new SimpleProcessorsFactory(); 

      factory.addPreProcessor(
           new GoogleClosureCompressorProcessor(
             CompilerOptions.LanguageMode.ECMASCRIPT5_STRICT
                                               )
                        ); 

      return factory;
      }
  }

Reference it in the pom.xml as the value of the wroManagerFactory node:

<configuration>
  <wroManagerFactory>com.mycompany.MyCustomWroManagerFactory</wroManagerFactory>
</configuration>

According to John Lenz from the Closure Compiler project, if you are using the Compiler API directly, you should specify a CodingConvention.

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • 2
    GoogleClosureCompressorProcessor does not seem to take a LanguageMode as a param to it's constructor. In fact as far as I can tell GoogleClosureCompressorProcessor no longer has any way to set CompilerOptions. – Josh Jun 16 '17 at 05:20
  • You now have to subclass GoogleClosureCompressorProcessor and override newCompilerOptions() to set your custom options. – Nicolai Ehemann Nov 29 '17 at 09:05
1

It's a bit more complicated in wro4j-maven-plugin 1.8, but not that bad.

You need to add two Java classes. First override newCompilerOptions of GoogleClosureCompressorProcessor like so:

package com.example.package.wro;

import com.google.javascript.jscomp.CheckLevel;
import com.google.javascript.jscomp.ClosureCodingConvention;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.DiagnosticGroups;
import java.nio.charset.Charset;
import org.apache.commons.lang3.CharEncoding;
import ro.isdc.wro.extensions.processor.js.GoogleClosureCompressorProcessor;

/**
 * Custom processor overriding `newCompilerOptions` to add custom compiler options.
 *
 * Original author: Alex Objelean.
 */
public class CustomGoogleClosureCompressorProcessor extends GoogleClosureCompressorProcessor {

    /**
     * Encoding to use.
     */
    public static final String ENCODING = CharEncoding.UTF_8;

    @Override
    protected CompilerOptions newCompilerOptions() {
        final CompilerOptions options = new CompilerOptions();

        // Set the language_in option on the Google Closure Compiler to prevent errors like:
        // "JSC_TRAILING_COMMA. Parse error. IE8 (and below)"
        options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5);

        /**
         * According to John Lenz from the Closure Compiler project, if you are using the Compiler API directly, you should
         * specify a CodingConvention. {@link http://code.google.com/p/wro4j/issues/detail?id=155}
         */
        options.setCodingConvention(new ClosureCodingConvention());
        // use the wro4j encoding by default
        //options.setOutputCharset(Charset.forName(getEncoding()));
        setEncoding(ENCODING);
        options.setOutputCharset(Charset.forName(ENCODING));
        // set it to warning, otherwise compiler will fail
        options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
        return options;
    }
}

You'll notice I've commented out the line getEncoding. This is because it's private. I also added setEncoding just in case.

Then we need the Custom manger:

package com.example.package.wro;

import ro.isdc.wro.manager.factory.standalone.DefaultStandaloneContextAwareManagerFactory;
import ro.isdc.wro.model.resource.processor.factory.ProcessorsFactory;
import ro.isdc.wro.model.resource.processor.factory.SimpleProcessorsFactory;

/**
 * Custom manger adding custom processor.
 */
public class CustomWroManagerFactory extends DefaultStandaloneContextAwareManagerFactory {

    @Override
    protected ProcessorsFactory newProcessorsFactory() {
        final SimpleProcessorsFactory factory = new SimpleProcessorsFactory();

        factory.addPreProcessor(
            new CustomGoogleClosureCompressorProcessor()
        );

        return factory;
    }
}

And then use it in your pom.xml in wroManagerFactory. Something like so:

        <plugin>
            <groupId>ro.isdc.wro4j</groupId>
            <artifactId>wro4j-maven-plugin</artifactId>
            <version>1.8.0</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <!-- Google Closure Compiler -->
            <!-- http://www.gzfs020.com/using-google-closure-compiler-with-wro4j-maven-plugin.html -->
            <configuration>
                <contextFolder>${basedir}/src/main</contextFolder>
                <wroFile>${basedir}/src/main/config/wro.xml</wroFile>
                <destinationFolder>${project.build.directory}/${project.build.finalName}/min</destinationFolder>
                <!--
                <wroManagerFactory>ro.isdc.wro.extensions.manager.standalone.GoogleStandaloneManagerFactory</wroManagerFactory>
                -->
                <wroManagerFactory>com.example.package.wro.CustomWroManagerFactory</wroManagerFactory>
            </configuration>
        </plugin>
Nux
  • 9,276
  • 5
  • 59
  • 72