diff --git a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaScriptCompilerMojo.java b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaScriptCompilerMojo.java index 7a7b4fea3..2d80a4c73 100644 --- a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaScriptCompilerMojo.java +++ b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaScriptCompilerMojo.java @@ -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. @@ -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); @@ -240,7 +237,7 @@ private void compile(Aggregation aggregation) throws WatchingException { } } - //Create the source map + //Create the source map file createSourceMapFile(output,compiler.getSourceMap()); } @@ -392,23 +389,13 @@ private void compile(File base) throws WatchingException { List inputs = new ArrayList<>(); List externs = new ArrayList<>(); - List 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); @@ -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; } diff --git a/core/wisdom-maven-plugin/src/test/java/org/wisdom/maven/mojos/JavaScriptCompilerMojoTest.java b/core/wisdom-maven-plugin/src/test/java/org/wisdom/maven/mojos/JavaScriptCompilerMojoTest.java index cf1c9a844..04de12802 100644 --- a/core/wisdom-maven-plugin/src/test/java/org/wisdom/maven/mojos/JavaScriptCompilerMojoTest.java +++ b/core/wisdom-maven-plugin/src/test/java/org/wisdom/maven/mojos/JavaScriptCompilerMojoTest.java @@ -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; @@ -119,7 +119,7 @@ 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"); @@ -127,8 +127,8 @@ public void testAggregatedSourceMapFileIsCreated() { 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); @@ -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(); + } } diff --git a/core/wisdom-maven-plugin/src/test/resources/fake-project/src/main/assets/street.js b/core/wisdom-maven-plugin/src/test/resources/fake-project/src/main/assets/street.js new file mode 100644 index 000000000..8bc276e5c --- /dev/null +++ b/core/wisdom-maven-plugin/src/test/resources/fake-project/src/main/assets/street.js @@ -0,0 +1,2 @@ +var msg = "Here comes a new challenger"; +console.log(msg); \ No newline at end of file