Skip to content

Commit

Permalink
Refactor SourceMap creation #512
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Bardin committed Jun 4, 2015
1 parent a7ad9df commit 7fc5b4b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Compiles and minifies JavaScript files.
Expand Down Expand Up @@ -215,11 +217,6 @@ private void compile(Aggregation aggregation) throws WatchingException {
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 @@ -240,7 +237,7 @@ private void compile(Aggregation aggregation) throws WatchingException {
}
}

//Create the source map
//Create the source map file
createSourceMapFile(output,compiler.getSourceMap());
}

Expand Down Expand Up @@ -392,23 +389,13 @@ private void compile(File base) throws WatchingException {
List<SourceFile> inputs = new ArrayList<>();
List<SourceFile> externs = new ArrayList<>();

List<SourceMap.LocationMapping> mappingList = new ArrayList<>(files.size());

for (File file : files) {
if (file.isFile() && isNotMinified(file) && isNotInLibs(file)) {
store.add(file);
inputs.add(SourceFile.fromFile(file));
mappingList.add(new SourceMap.LocationMapping(getMinifiedFile(file).getParentFile().getPath(),
getMinifiedFile(file).getParentFile().getPath()));
}
}

if (googleClosureMap) {
options.setSourceMapFormat(SourceMap.Format.DEFAULT);
options.setSourceMapOutputPath(buildDirectory.getPath());
options.setSourceMapLocationMappings(mappingList);
}

compiler.initOptions(options);
final Result result = compiler.compile(externs, inputs, options);
listErrors(result);
Expand Down Expand Up @@ -454,6 +441,12 @@ protected CompilerOptions newCompilerOptions() {
options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES,
CheckLevel.WARNING);

//Initialise source map options
if (googleClosureMap) {
options.setSourceMapFormat(SourceMap.Format.DEFAULT);
options.setSourceMapOutputPath(buildDirectory.getPath());
}

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.io.IOException;

import static java.util.Collections.singletonList;
import static org.apache.commons.io.FileUtils.lineIterator;
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 @@ -119,16 +119,16 @@ public void testGetDefaultOutputFile() {
}

@Test
public void testAggregatedSourceMapFileIsCreated() {
public void testAggregatedSourceMapIsCreated() throws IOException, MojoExecutionException, MojoFailureException {
JavaScriptCompilerMojo mojo = new JavaScriptCompilerMojo();
mojo.googleClosureMinifierSuffix = "-min";
mojo.basedir = new File("target/junk/root");
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;
mojo.googleClosureCompilationLevel = CompilationLevel.ADVANCED_OPTIMIZATIONS;
mojo.googleClosureMap = true;

Aggregation aggregation = new Aggregation();
aggregation.setMinification(true);
Expand All @@ -137,22 +137,34 @@ public void testAggregatedSourceMapFileIsCreated() {
javascript.setAggregations(singletonList(new Aggregation()));
mojo.javascript = javascript;

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

File map = new File(mojo.getDefaultOutputFile(aggregation).getParentFile(),"my-artifact-min.js.map");
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);
}
assertThat(lineIterator(map)).contains("\"file\":\"my-artifact-min.js\",");
}

@Test
public void testSourceMapIsCreatedForInternal() throws IOException, MojoExecutionException, MojoFailureException {
JavaScriptCompilerMojo mojo = new JavaScriptCompilerMojo();
mojo.googleClosureMinifierSuffix = "-min";
mojo.basedir = new File("target/test-classes/fake-project");
mojo.buildDirectory = new File(mojo.basedir, "target");
mojo.getInternalAssetOutputDirectory().mkdirs();
mojo.googleClosureMap = true;

FileUtils.copyFileToDirectory(new File(mojo.getExternalAssetsDirectory(), "street.js"),
mojo.getInternalAssetOutputDirectory());
mojo.googleClosureCompilationLevel = CompilationLevel.ADVANCED_OPTIMIZATIONS;

mojo.execute();

File min = new File(mojo.getInternalAssetOutputDirectory(), "street-min.js");
File map = new File(mojo.getInternalAssetOutputDirectory(), "street-min.js.map");

assertThat(lineIterator(min)).endsWith("//# sourceMappingURL=street-min.js.map");
assertThat(map).exists();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var msg = "Here comes a new challenger";
console.log(msg);

0 comments on commit 7fc5b4b

Please sign in to comment.