Skip to content

Commit

Permalink
Add google closure source map support #512
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Bardin committed Jun 3, 2015
1 parent c6b1cbf commit 533719b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.wisdom.maven.mojos;

import com.google.javascript.jscomp.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -32,10 +33,8 @@
import org.wisdom.maven.node.LoggedOutputStream;
import org.wisdom.maven.utils.WatcherUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -92,6 +91,12 @@ public class JavaScriptCompilerMojo extends AbstractWisdomWatcherMojo implements
@Parameter(defaultValue = "// -- Input %num% -- //")
public String googleClosureInputDelimiter;

/**
* Whether or not Google Closure must create the source map file.
*/
@Parameter(defaultValue = "true")
public boolean googleClosureMap;

/**
* The JavaScript configuration.
*/
Expand Down Expand Up @@ -207,12 +212,16 @@ private void compile(Aggregation aggregation) throws WatchingException {
inputs.add(SourceFile.fromFile(file));
}


List<SourceFile> externs = new ArrayList<>();
if (javascript.getExtern() != null) {
externs.add(new SourceFile(javascript.getExtern().getAbsolutePath()));
}

if (googleClosureMap) {
options.setSourceMapFormat(SourceMap.Format.DEFAULT);
options.setSourceMapOutputPath(output.getPath());
}

compiler.initOptions(options);
final Result result = compiler.compile(externs, inputs, options);
listErrors(result);
Expand All @@ -223,6 +232,9 @@ private void compile(Aggregation aggregation) throws WatchingException {

FileUtils.deleteQuietly(output);
String[] outputs = compiler.toSourceArray();

PrintWriter map = null;

for (String source : outputs) {
try {
FileUtils.write(output, source, true);
Expand All @@ -231,6 +243,20 @@ private void compile(Aggregation aggregation) throws WatchingException {
e);
}
}

if (googleClosureMap) {
try {
File mapFile = new File(output.getPath() + ".map");
map = new PrintWriter(mapFile, Charset.defaultCharset().name());
compiler.getSourceMap().appendTo(map, output.getName());
FileUtils.write(output, "\n//# sourceMappingURL=" + mapFile.getName(), true);
} catch (IOException e) {
throw new WatchingException("Cannot create source map file for JavaScript file '" +
output.getAbsolutePath() + "'", e);
} finally {
IOUtils.closeQuietly(map);
}
}
}

private File getOutputFile(Aggregation aggregation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
package org.wisdom.maven.mojos;

import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilationLevel;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -111,4 +118,40 @@ public void testGetDefaultOutputFile() {
"classes/assets/my-artifact-min.js").getAbsolutePath());
}

@Test
public void testSourceMapFileIsCreated() {
JavaScriptCompilerMojo mojo = new JavaScriptCompilerMojo();
mojo.googleClosureMinifierSuffix = "-min";
mojo.buildDirectory = new File(mojo.basedir, "target");
MavenProject project = mock(MavenProject.class);
when(project.getArtifactId()).thenReturn("my-artifact");
mojo.project = project;
mojo.googleClosureCompilationLevel= CompilationLevel.ADVANCED_OPTIMIZATIONS;
mojo.googleClosureMap=true;

Aggregation aggregation = new Aggregation();
aggregation.setMinification(true);

JavaScript javascript = new JavaScript();
javascript.setAggregations(singletonList(new Aggregation()));
mojo.javascript = javascript;

try {
mojo.execute();
} catch (MojoExecutionException | MojoFailureException e) {
fail("Cannot create the map file.",e);
}

File map = new File(mojo.getDefaultOutputFile(aggregation).getParentFile(),"my-artifact-min.js.map");

assertThat(mojo.getDefaultOutputFile(aggregation)).hasContent("\n//# sourceMappingURL=my-artifact-min.js.map");
assertThat(map).exists();

try {
assertThat(FileUtils.lineIterator(map)).contains("\"file\":\"my-artifact-min.js\",");
} catch (IOException e) {
fail("Cannot read map file",e);
}
}

}

0 comments on commit 533719b

Please sign in to comment.