使用wro4j和Google Closure Compiler的ECMASCRIPT 5

4
我们正在使用wro4j、Google Closure和Maven来压缩我们的JS。默认情况下,它不支持JS中的严格模式("use strict";)...它只是将其删除。是否有任何配置可以在pom.xml或其他地方进行,以便让它保留"use strict"?
以下是Google Closure编译器的配置:
--language_in=ECMASCRIPT5_STRICT

不确定如何将其插入到Wro4j中。 有什么建议吗?


你能提供一下你试图最小化的脚本示例吗?另外,期望的和实际的输出是什么?这可能更多是Google Closure的问题,而不是wro4j的问题。 - Alex Objelean
2个回答

1

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

然后在您的pom.xml中使用它在 wroManagerFactory 中。像这样:

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

1
创建一个自定义的管理器工厂实现,其中添加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>

根据Closure Compiler项目的John Lenz所说,如果您直接使用编译器API,应该指定一种编码约定(CodingConvention)。
参考资料:
- GoogleClosureCompressorProcessor.java - setCompilerOptions方法:{{link1}} - GoogleClosureCompressorProcessor.java - optionsPool方法:{{link2}} - Closure Compiler服务API参考 - 语言 | Closure Compiler | Google Developers:{{link3}}

2
GoogleClosureCompressorProcessor似乎没有将LanguageMode作为其构造函数的参数。实际上,据我所知,GoogleClosureCompressorProcessor不再具有设置CompilerOptions的任何方式。 - Josh
现在您需要子类化GoogleClosureCompressorProcessor并重写newCompilerOptions()方法来设置您的自定义选项。 - Nicolai Ehemann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接