From bc88cc0f0854b13b065df9a25af22877f3938777 Mon Sep 17 00:00:00 2001 From: Dmitrii Tikhomirov Date: Sun, 4 Feb 2024 23:32:01 -0800 Subject: [PATCH] move incremental source maps processsing to ClosureBundleTask --- .../build/provided/ClosureBundleTask.java | 40 +++++++++++-------- .../j2cl/build/provided/J2clTask.java | 29 -------------- 2 files changed, 23 insertions(+), 46 deletions(-) diff --git a/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/ClosureBundleTask.java b/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/ClosureBundleTask.java index b5e211af..e6a879c4 100644 --- a/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/ClosureBundleTask.java +++ b/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/ClosureBundleTask.java @@ -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(); @@ -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())); @@ -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) ); } } @@ -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)); } } } @@ -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()) { @@ -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"); } @@ -241,6 +244,16 @@ 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; } @@ -248,11 +261,8 @@ 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; } @@ -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 provides; -// private List requires; //skipping requires as it isnt used by the dep sorter + // private List requires; //skipping requires as it isnt used by the dep sorter private List requiredSymbols; private List typeRequires; private Map loadFlags; diff --git a/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/J2clTask.java b/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/J2clTask.java index 00ca46de..0018e5af 100644 --- a/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/J2clTask.java +++ b/j2cl-tasks/src/main/java/com/vertispan/j2cl/build/provided/J2clTask.java @@ -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; @@ -58,10 +53,6 @@ public Task resolve(Project project, Config config) { File bootstrapClasspath = config.getBootstrapClasspath(); List 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 @@ -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); - } - } }