Skip to content

Commit

Permalink
move incremental source maps processsing to ClosureBundleTask
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel committed Feb 5, 2024
1 parent da2ac7c commit 36d3c3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void finish(TaskContext taskContext) throws IOException {
}

// Copy sources to the output directory, if sourcemaps are enabled and incremental source maps is not.
if(sourcemapsEnabled) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ 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();
boolean sourcemapsEnabled = config.getSourcemapsEnabled() && config.getEnableIncrementalSourcemaps();
Path sourceMapsFolder = sourcemapsEnabled ? prepareSourcesFolder(config) : null;

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

Expand Down Expand Up @@ -133,7 +134,6 @@ 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 @@ -153,7 +153,7 @@ public Task resolve(Project project, Config config) {
input.setCompiler(jsCompiler);
depInfoMap.put(
change.getSourcePath().toString(),
new DependencyInfoAndSource(input, fileNameKey, input::getCode)
new DependencyInfoAndSource(input, input::getCode)
);
}
}
Expand All @@ -172,7 +172,8 @@ public Task resolve(Project project, Config config) {
.withOriginalPath(path.getSourcePath().toString())
.build());
input.setCompiler(jsCompiler);
dependencyInfos.add(new DependencyInfoAndSource(input, fileNameKey, input::getCode));

dependencyInfos.add(new DependencyInfoAndSource(input, input::getCode));
}
}
}
Expand All @@ -198,12 +199,15 @@ public Task resolve(Project project, Config config) {
""
)).useEval(true);

if (sourcemapsEnabled) {
FileUtils.copyDirectory(context.outputPath().resolve("sources").toFile(), sourceMapsFolder.toFile());
}

try (OutputStream outputStream = Files.newOutputStream(Paths.get(outputFile));
BufferedWriter bundleOut = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
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 @@ -212,8 +216,7 @@ public Task resolve(Project project, Config config) {

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

}
Expand Down Expand Up @@ -241,18 +244,25 @@ public Task resolve(Project project, Config config) {
};
}

private Path prepareSourcesFolder(Config config) {
try {
Path initialScriptFile = config.getWebappDirectory().resolve(config.getInitialScriptFilename());
Path destSourcesDir = Files.createDirectories(initialScriptFile.getParent()).resolve(Closure.SOURCES_DIRECTORY_NAME);
return Files.createDirectories(destSourcesDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public interface SourceSupplier {
String get() throws IOException;
}
public static class DependencyInfoAndSource implements DependencyInfo {
private final DependencyInfo delegate;
private final SourceSupplier sourceSupplier;

private final String project;

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

Expand Down Expand Up @@ -315,17 +325,13 @@ public boolean getHasExternsAnnotation() {
public boolean getHasNoCompileAnnotation() {
return delegate.getHasNoCompileAnnotation();
}

public String getProject() {
return project;
}
}

public static class DependencyInfoFormat implements DependencyInfo {
private String name;
// private String pathRelativeToClosureBase = name;
// private String pathRelativeToClosureBase = name;
private List<String> provides;
// private List<RequireFormat> requires; //skipping requires as it isnt used by the dep sorter
// private List<RequireFormat> requires; //skipping requires as it isnt used by the dep sorter
private List<String> requiredSymbols;
private List<String> typeRequires;
private Map<String, String> loadFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

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 @@ -58,10 +53,6 @@ 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 @@ -93,26 +84,6 @@ 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 36d3c3a

Please sign in to comment.