<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-webresources</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.javascript</groupId>
<artifactId>closure-compiler</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.javascript</groupId>
<artifactId>closure-compiler</artifactId>
<version>v20151216</version>
</dependency>
@Contribute(ResourceMinimizer.class)
@Primary
public static void overrideJavaScriptMinimizer(final
MappedConfiguration<String, ResourceMinimizer> configuration) {
configuration.overrideInstance("text/javascript",
MyGoogleClosureMinimizer.class);
}
package com.jacilla.core.services;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.*;
import org.apache.commons.io.IOUtils;
import org.apache.tapestry5.internal.webresources.GoogleClosureMinimizer;
import org.apache.tapestry5.ioc.OperationTracker;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
import org.apache.tapestry5.services.assets.StreamableResource;
import org.apache.tapestry5.webresources.WebResourcesSymbols;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
public class MyGoogleClosureMinimizer extends GoogleClosureMinimizer {
private final static String OUTPUT_CHARSET = "utf-8";
private final List<SourceFile> EXTERNS = Collections.emptyList();
private final CompilationLevel compilationLevel;
public MyGoogleClosureMinimizer(Logger logger, OperationTracker tracker,
AssetChecksumGenerator checksumGenerator, Request request,
@Symbol(WebResourcesSymbols.COMPILATION_LEVEL) CompilationLevel
compilationLevel) {
super(logger, tracker, checksumGenerator, request, compilationLevel);
this.compilationLevel = compilationLevel;
}
@Override
protected InputStream doMinimize(StreamableResource resource) throws
IOException {
CompilerOptions options = new CompilerOptions();
compilationLevel.setOptionsForCompilationLevel(options);
options.setLanguage(CompilerOptions.LanguageMode.ECMASCRIPT6);
options.setCodingConvention(new ClosureCodingConvention());
options.setOutputCharset(OUTPUT_CHARSET);
options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES,
CheckLevel.WARNING);
com.google.javascript.jscomp.Compiler compiler = new Compiler();
compiler.disableThreads();
SourceFile input = SourceFile.fromInputStream(resource.toString(),
resource.openStream(), Charset.defaultCharset());
List<SourceFile> inputs = Collections.singletonList(input);
Result result = compiler.compile(EXTERNS, inputs, options);
if (result.success) {
return IOUtils.toInputStream(compiler.toSource(), OUTPUT_CHARSET);
}
throw new RuntimeException(String.format("Compilation failed: %s.",
InternalUtils.join(CollectionFactory.newList(result.errors),
";")));
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2015",