带有 wro4j 和 Google Closure 编译器的 ECMASCRIPT 5



我们正在将wro4j与Google Closure和Maven一起使用来缩小我们的JS。默认情况下,它不支持JS中的严格模式("使用strict";).它只是将其剥离。我可以在pom.xml或其他地方进行任何配置以使其use strict留在其中吗?

这是谷歌闭包编译器的配置:

--language_in=ECMASCRIPT5_STRICT

不知道如何将其插入 Wro4j。有什么想法吗?

创建一个添加 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;
      }
  }

在pom中引用它.xml作为wroManagerFactory节点的值:

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

根据闭包编译器项目的 John Lenz 的说法,如果你直接使用 Compiler API,你应该指定一个 CodingConvention。

引用

  • GoogleClosureCompressorProcessor.java - Method setCompilerOptions

  • GoogleClosureCompressorProcessor.java - optionsPool Method

  • 闭包编译器服务 API 参考 - 语言 | 闭包编译器 | 谷歌开发者

wro4j-maven-plugin 1.8 中它有点复杂,但还不错。

您需要添加两个 Java 类。第一次覆盖GoogleClosureCompressorProcessornewCompilerOptions,如下所示:

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;
    }
}

你会注意到我已经注释掉了getEncoding行。这是因为它是私有的。我还添加了setEncoding以防万一。

然后我们需要自定义管理器:

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;
    }
}

然后在wroManagerFactorypom.xml中使用它.像这样:

        <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>

相关内容

  • 没有找到相关文章

最新更新