Skip to content

Commit

Permalink
Added ability in watch mode to copy source maps to output folder for …
Browse files Browse the repository at this point in the history
…only changed projects.
  • Loading branch information
treblereel committed Feb 1, 2024
1 parent 320e5ba commit da2ac7c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public boolean getSourcemapsEnabled() {
return Boolean.parseBoolean(getString("enableSourcemaps"));
}

@Override
public boolean getEnableIncrementalSourcemaps() {
return Boolean.parseBoolean(getString("enableIncrementalSourcemaps"));
}

@Override
public String getInitialScriptFilename() {
return getString("initialScriptFilename");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface Config {

boolean getSourcemapsEnabled();

boolean getEnableIncrementalSourcemaps();

String getInitialScriptFilename();

Map<String, String> getDefines();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public class BuildMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "false")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// pre-create the directory so it is easier to find up front, even if it starts off empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public class TestMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "false")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

/**
* Closure flag: "Source of translated messages. Currently only supports XTB."
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public class WatchMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "true")
protected boolean enableSourcemaps;

/**
* True to copy only changed source files to the output directory. This is a performance optimization.
*/
@Parameter(defaultValue = "false")
protected boolean enableIncrementalSourcemaps;

/**
* @deprecated Will be removed in 0.21
*/
Expand Down
2 changes: 1 addition & 1 deletion j2cl-tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public Task resolve(Project project, Config config) {
}

File initialScriptFile = config.getWebappDirectory().resolve(config.getInitialScriptFilename()).toFile();
boolean sourcemapsEnabled = config.getSourcemapsEnabled() && !config.getEnableIncrementalSourcemaps();

Map<String, Object> defines = new LinkedHashMap<>(config.getDefines());

List<Input> outputToCopy = Stream.concat(
Expand Down Expand Up @@ -113,11 +115,14 @@ public void finish(TaskContext taskContext) throws IOException {
Files.copy(bundle.getAbsolutePath(), targetFile, StandardCopyOption.REPLACE_EXISTING);
}

File destSourcesDir = outputDir.toPath().resolve(Closure.SOURCES_DIRECTORY_NAME).toFile();
destSourcesDir.mkdirs();
for (Path dir : jsSources.stream().map(Input::getParentPaths).flatMap(Collection::stream).map(p -> p.resolve(Closure
.SOURCES_DIRECTORY_NAME)).collect(Collectors.toSet())) {
FileUtils.copyDirectory(dir.toFile(), destSourcesDir);
// Copy sources to the output directory, if sourcemaps are enabled and incremental source maps is not.
if(sourcemapsEnabled) {
File destSourcesDir = outputDir.toPath().resolve(Closure.SOURCES_DIRECTORY_NAME).toFile();
destSourcesDir.mkdirs();
for (Path dir : jsSources.stream().map(Input::getParentPaths).flatMap(Collection::stream).map(p -> p.resolve(Closure
.SOURCES_DIRECTORY_NAME)).collect(Collectors.toSet())) {
FileUtils.copyDirectory(dir.toFile(), destSourcesDir);
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public Task resolve(Project project, Config config) {

// Consider treating this always as true, since the build doesnt get more costly to be incremental
boolean incrementalEnabled = config.isIncrementalEnabled();
boolean enableIncrementalSourcemaps = config.getEnableIncrementalSourcemaps();

Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();

Expand Down Expand Up @@ -132,6 +133,7 @@ public Task resolve(Project project, Config config) {
depInfoMap = deps.stream()
.map(info -> new DependencyInfoAndSource(
info,
fileNameKey,
() -> Files.readString(lastOutput.resolve(Closure.SOURCES_DIRECTORY_NAME).resolve(info.getName())))
)
.collect(Collectors.toMap(DependencyInfo::getName, Function.identity()));
Expand All @@ -151,7 +153,7 @@ public Task resolve(Project project, Config config) {
input.setCompiler(jsCompiler);
depInfoMap.put(
change.getSourcePath().toString(),
new DependencyInfoAndSource(input, input::getCode)
new DependencyInfoAndSource(input, fileNameKey, input::getCode)
);
}
}
Expand All @@ -170,8 +172,7 @@ public Task resolve(Project project, Config config) {
.withOriginalPath(path.getSourcePath().toString())
.build());
input.setCompiler(jsCompiler);

dependencyInfos.add(new DependencyInfoAndSource(input, input::getCode));
dependencyInfos.add(new DependencyInfoAndSource(input, fileNameKey, input::getCode));
}
}
}
Expand Down Expand Up @@ -202,6 +203,7 @@ public Task resolve(Project project, Config config) {
for (DependencyInfoAndSource info : sorter.getSortedList()) {
String code = info.getSource();
String name = info.getName();
String projectName = info.getProject();

//TODO do we actually need this?
if (Compiler.isFillFileName(name) && code.isEmpty()) {
Expand All @@ -210,7 +212,8 @@ public Task resolve(Project project, Config config) {

// append this file and a comment where it came from
bundleOut.append("//").append(name).append("\n");
bundler.withPath(name).withSourceUrl(Closure.SOURCES_DIRECTORY_NAME + "/" + name).appendTo(bundleOut, info, code);
String sourceMapUrl = Closure.SOURCES_DIRECTORY_NAME + (enableIncrementalSourcemaps ? ("/" + projectName) : "") + "/" + name;
bundler.withPath(name).withSourceUrl(sourceMapUrl).appendTo(bundleOut, info, code);
bundleOut.append("\n");

}
Expand Down Expand Up @@ -245,8 +248,11 @@ public static class DependencyInfoAndSource implements DependencyInfo {
private final DependencyInfo delegate;
private final SourceSupplier sourceSupplier;

public DependencyInfoAndSource(DependencyInfo delegate, SourceSupplier sourceSupplier) {
private final String project;

public DependencyInfoAndSource(DependencyInfo delegate, String project, SourceSupplier sourceSupplier) {
this.delegate = delegate;
this.project = project.replaceAll("\\.", "-");
this.sourceSupplier = sourceSupplier;
}

Expand Down Expand Up @@ -309,6 +315,10 @@ public boolean getHasExternsAnnotation() {
public boolean getHasNoCompileAnnotation() {
return delegate.getHasNoCompileAnnotation();
}

public String getProject() {
return project;
}
}

public static class DependencyInfoFormat implements DependencyInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import com.google.auto.service.AutoService;
import com.google.j2cl.common.SourceUtils;
import com.google.javascript.jscomp.CompilationLevel;
import com.vertispan.j2cl.build.task.*;
import com.vertispan.j2cl.tools.Closure;
import com.vertispan.j2cl.tools.J2cl;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Collections;
Expand Down Expand Up @@ -53,6 +58,10 @@ public Task resolve(Project project, Config config) {

File bootstrapClasspath = config.getBootstrapClasspath();
List<File> extraClasspath = config.getExtraClasspath();

boolean sourcemapsEnabled = config.getSourcemapsEnabled() && config.getEnableIncrementalSourcemaps();
Path sourceMapsFolder = sourcemapsEnabled ? prepareSourcesFolder(config, project) : null;

return context -> {
if (ownJavaSources.getFilesAndHashes().isEmpty()) {
return;// nothing to do
Expand Down Expand Up @@ -84,6 +93,26 @@ public Task resolve(Project project, Config config) {
if (!j2cl.transpile(javaSources, nativeSources)) {
throw new IllegalStateException("Error while running J2CL");
}

if(sourcemapsEnabled) {
if(sourceMapsFolder.toFile().exists()) {
FileUtils.deleteDirectory(sourceMapsFolder.toFile());
}
sourceMapsFolder.toFile().mkdirs();
FileUtils.copyDirectory(context.outputPath().toFile(), sourceMapsFolder.toFile());
}
};
}

private Path prepareSourcesFolder(Config config, Project project) {
try {
Path initialScriptFile = config.getWebappDirectory().resolve(config.getInitialScriptFilename());
Path destSourcesDir = Files.createDirectories(initialScriptFile.getParent()).resolve(Closure.SOURCES_DIRECTORY_NAME);
return Files.createDirectories(destSourcesDir).resolve(project.getKey()
.replaceAll("\\.","-")
.replaceAll(":","-"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit da2ac7c

Please sign in to comment.