Skip to content

Commit

Permalink
Enable embedding sources inside the sourcemaps json as sourcesContent
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Mar 13, 2024
1 parent 7f76f0f commit ddf1364
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 279 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/full-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ jobs:
- name: Checkout GWT tools into a sibling directory
uses: actions/checkout@v2
with:
repository: 'gwtproject/tools'
repository: 'vegegoku/tools'
ref: 'source-maps'
path: 'tools'
- name: Set up JDK ${{ matrix.java-version }}
# GWT presently requires Java8 to build just the SDK and some tests, or 11 to build everything, but can run on newer Java versions
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/quick-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ jobs:
- name: Checkout GWT tools into a sibling directory
uses: actions/checkout@v2
with:
repository: 'gwtproject/tools'
repository: 'vegegoku/tools'
ref: 'source-maps'
path: 'tools'
- name: Set up JDK ${{ matrix.java-version }}
# GWT presently requires Java8 to build just the SDK and some tests, or 11+ to build everything, and can run on newer Java versions
Expand Down
18 changes: 11 additions & 7 deletions dev/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@
<include name="objectweb/asm-9.2/asm-9.2.jar"/>
<include name="objectweb/asm-9.2/asm-commons-9.2.jar"/>
<include name="objectweb/asm-9.2/asm-util-9.2.jar"/>
<include name="guava/guava-19.0/guava-19.0-rebased.jar"/>
<include name="guava/guava-33.0/guava-33.0.0-jre-rebased.jar"/>
<include name="guava/guava-33.0/failureaccess-1.0.2-rebased.jar"/>
<include name="icu4j/63.1/icu4j.jar"/>
<include name="jetty/jetty-9.4.44.v20210927/jetty-all-9.4.44.v20210927.jar"/>
<include name="gson/gson-2.6.2.jar"/>
<include name="jscomp/20160315/sourcemap-rebased.jar"/>
<include name="jscomp/20231112/sourcemap-rebased.jar"/>
<include name="jsr305/jsr305.jar"/>
<include name="protobuf/protobuf-2.5.0/protobuf-java-rebased-2.5.0.jar"/>
<!-- dependencies needed for JSP support in DevMode: BEGIN -->
Expand Down Expand Up @@ -142,8 +143,9 @@
<zipfileset
src="${gwt.tools.lib}/eclipse/org.eclipse.jdt.core_3.17.0.v20190306-2240.jar"/>
<zipfileset
src="${gwt.tools.lib}/eclipse/jdtCompilerAdapter_3.17.0.v20190306-2240.jar"/>
<zipfileset src="${gwt.tools.lib}/guava/guava-19.0/guava-19.0-rebased.jar"/>
src="${gwt.tools.lib}/eclipse/jdtCompilerAdapter_3.32.0.v20221108-1853.jar"/>
<zipfileset src="${gwt.tools.lib}/guava/guava-33.0/guava-33.0.0-jre-rebased.jar"/>
<zipfileset src="${gwt.tools.lib}/guava/guava-33.0/failureaccess-1.0.2-rebased.jar"/>
<zipfileset src="${gwt.tools.lib}/icu4j/63.1/icu4j.jar"/>
<zipfileset
src="${gwt.tools.lib}/jetty/jetty-9.4.44.v20210927/jetty-all-9.4.44.v20210927.jar">
Expand All @@ -154,7 +156,7 @@
<exclude name="META-INF/services/org.eclipse.jetty.security.Authenticator$Factory"/>
</zipfileset>
<zipfileset src="${gwt.tools.lib}/gson/gson-2.6.2.jar"/>
<zipfileset src="${gwt.tools.lib}/jscomp/20160315/sourcemap-rebased.jar"/>
<zipfileset src="${gwt.tools.lib}/jscomp/20231112/sourcemap-rebased.jar"/>
<zipfileset src="${gwt.tools.lib}/jsr305/jsr305.jar"/>
<zipfileset
src="${gwt.tools.lib}/protobuf/protobuf-2.5.0/protobuf-java-rebased-2.5.0.jar"/>
Expand Down Expand Up @@ -246,9 +248,11 @@
<pathelement
location="${gwt.tools.lib}/eclipse/jdtCompilerAdapter_3.17.0.v20190306-2240.jar"/>
<pathelement
location="${gwt.tools.lib}/guava/guava-19.0/guava-19.0-rebased.jar"/>
location="${gwt.tools.lib}/guava/guava-33.0/guava-33.0.0-jre-rebased.jar"/>
<pathelement
location="${gwt.tools.lib}/guava/guava-33.0/failureaccess-1.0.2-rebased.jar"/>
<pathelement location="${gwt.tools.lib}/gson/gson-2.6.2.jar"/>
<pathelement location="${gwt.tools.lib}/jscomp/20160315/sourcemap-rebased.jar"/>
<pathelement location="${gwt.tools.lib}/jscomp/20231112/sourcemap-rebased.jar"/>
<pathelement location="${gwt.tools.lib}/json/android-sdk-19.1/json-android-rebased.jar"/>
<pathelement location="${gwt.tools.lib}/jsr305/jsr305.jar"/>
<pathelement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public int compare(SelectionProperty o1, SelectionProperty o2) {
private ResourceOracle publicResourceOracle;

private final SortedSet<SelectionProperty> selectionProperties;
private ModuleDef module;

public StandardLinkerContext(TreeLogger logger, ModuleDef module,
ResourceOracle publicResourceOracle, JsOutputOption outputOption)
Expand All @@ -143,6 +144,7 @@ public StandardLinkerContext(TreeLogger logger, ModuleDef module,
this.moduleLastModified = module.lastModified();
this.publicResourceOracle = publicResourceOracle;
this.outputOption = outputOption;
this.module = module;

// Sort the linkers into the order they should actually run.
linkerClasses = new ArrayList<Class<? extends Linker>>();
Expand Down Expand Up @@ -537,6 +539,10 @@ public void produceOutput(TreeLogger logger, ArtifactSet artifacts,
}
}

public ModuleDef getModule() {
return module;
}

/**
* (Re)instantiate all linkers.
*/
Expand Down
80 changes: 72 additions & 8 deletions dev/core/src/com/google/gwt/core/linker/SymbolMapsLinker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.google.gwt.core.linker;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

import com.google.gwt.core.ext.LinkerContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
Expand All @@ -31,18 +34,27 @@
import com.google.gwt.core.ext.linker.SoftPermutation;
import com.google.gwt.core.ext.linker.SymbolData;
import com.google.gwt.core.ext.linker.SyntheticArtifact;
import com.google.gwt.core.ext.linker.impl.StandardLinkerContext;
import com.google.gwt.dev.cfg.ResourceLoader;
import com.google.gwt.dev.cfg.ResourceLoaders;
import com.google.gwt.dev.util.Util;
import com.google.gwt.dev.util.collect.HashMap;
import com.google.gwt.dev.util.log.speedtracer.CompilerEventType;
import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger;
import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
import com.google.gwt.thirdparty.debugging.sourcemap.SourceMapConsumerV3;
import com.google.gwt.thirdparty.debugging.sourcemap.SourceMapGeneratorV3;
import com.google.gwt.thirdparty.debugging.sourcemap.SourceMapGeneratorV3.ExtensionMergeAction;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -162,7 +174,8 @@ public static class SourceMapArtifact extends SyntheticArtifact {
private final String sourceRoot;

public SourceMapArtifact(int permutationId, int fragment, byte[] js, String sourceRoot) {
super(SymbolMapsLinker.class, permutationId + '/' + sourceMapFilenameForFragment(fragment), js);
super(SymbolMapsLinker.class, permutationId + '/' +
sourceMapFilenameForFragment(fragment), js);
this.permutationId = permutationId;
this.fragment = fragment;
this.js = js;
Expand Down Expand Up @@ -313,13 +326,46 @@ public ArtifactSet link(TreeLogger logger, LinkerContext context,
}
}
// TODO(cromwellian): apply insert and remove edits
sourceMapGenerator.mergeMapSection(totalPrefixLines, 0, sourceMapString,
new ExtensionMergeAction() {
@Override
public Object merge(String extKey, Object oldVal, Object newVal) {
return newVal;
}
sourceMapGenerator.setStartingPosition(totalPrefixLines, 0);
SourceMapConsumerV3 section = new SourceMapConsumerV3();
section.parse(sourceMapString);
section.visitMappings(sourceMapGenerator::addMapping);

Iterator extensions = section.getExtensions().entrySet().iterator();

while (extensions.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry) extensions.next();
String extensionKey = (String) entry.getKey();
sourceMapGenerator.addExtension(extensionKey, entry.getValue());
}

ResourceLoader resourceLoader = ResourceLoaders.fromContextClassLoader();

Map<String, EmittedArtifact> generatedSources = new java.util.HashMap<>();
artifacts.find(EmittedArtifact.class)
.forEach(emittedArtifact -> {
if (Visibility.Source == emittedArtifact.getVisibility()) {
generatedSources.put(emittedArtifact.getPartialPath(), emittedArtifact);
}
});

StandardLinkerContext stdContext = (StandardLinkerContext) context;
for (String sourceFileName : section.getOriginalSources()) {
String content;
try {
InputStream cis = loadSource(logger, sourceFileName, generatedSources,
resourceLoader);
if (isNull(cis)) {
cis = stdContext.getModule().findSourceFile(sourceFileName).openContents();
}
content = Util.readStreamAsString(cis);
sourceMapGenerator.addSourcesContent(sourceFileName, content);
} catch (UnableToCompleteException | URISyntaxException | IOException e) {
logger.log(TreeLogger.Type.WARN, "Can't write source map " +
partialPath, e);
}
}

StringWriter stringWriter = new StringWriter();
sourceMapGenerator.appendTo(stringWriter, "sourceMap");
emArt = emitSourceMapString(logger, stringWriter.toString(), partialPath);
Expand All @@ -335,6 +381,24 @@ public Object merge(String extKey, Object oldVal, Object newVal) {
return artifacts;
}

private static InputStream loadSource(TreeLogger logger, String sourceFileName,
Map<String, EmittedArtifact> generatedSources,
ResourceLoader resourceLoader)
throws UnableToCompleteException, URISyntaxException, IOException {
if (generatedSources.containsKey(sourceFileName)) {
return generatedSources.get(sourceFileName).getContents(logger);
} else {
// ask the resourceOracle for the file contents and add it
URL resource = resourceLoader.getResource(sourceFileName);
if (nonNull(resource)) {
URI uri = resource.toURI();
// Use Files.readAllBytes and Paths.get for Java 8 compatibility
return resource.openStream();
}
}
return null;
}

/**
* Override to change the manner in which the symbol map is emitted.
*/
Expand Down
4 changes: 0 additions & 4 deletions dev/core/src/com/google/gwt/dev/CompilePerms.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,6 @@ private boolean precompileAndCompile(TreeLogger logger, String moduleName,
if (precompilation == null) {
return false;
}
// TODO: move to precompile() after params are refactored
if (!options.shouldSaveSource()) {
precompilation.removeSourceArtifacts(logger);
}

// Choose which permutations go with this precompilation
Permutation[] subPerms = selectPermutationsForPrecompilation(
Expand Down
4 changes: 0 additions & 4 deletions dev/core/src/com/google/gwt/dev/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ public static boolean compile(
if (precompilation == null) {
return false;
}
// TODO: move to precompile() after params are refactored
if (!options.shouldSaveSource()) {
precompilation.removeSourceArtifacts(branch);
}

Event compilePermutationsEvent =
SpeedTracerLogger.start(CompilerEventType.COMPILE_PERMUTATIONS);
Expand Down
1 change: 0 additions & 1 deletion dev/core/src/com/google/gwt/dev/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ private static void doProduceOutput(TreeLogger logger, ArtifactSet artifacts,
// Assume that all source code is available in the compiler's classpath.
// (This will have to be adjusted to work with Super Dev Mode.)
ResourceLoader loader = ResourceLoaders.fromContextClassLoader();
SourceSaver.save(logger, artifacts, loader, options, destPrefix, extraFileSet);
}

outFileSet.close();
Expand Down
5 changes: 1 addition & 4 deletions dev/core/src/com/google/gwt/dev/Precompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,7 @@ public boolean run(TreeLogger logger) throws UnableToCompleteException {
branch.log(TreeLogger.ERROR, "Precompilation failed");
return false;
}
// TODO: move to precompile() after params are refactored
if (!options.shouldSaveSource()) {
precompilation.removeSourceArtifacts(logger);
}

Util.writeObjectAsFile(logger, precompilationFile, precompilation);

int permsPrecompiled = precompilation.getPermutations().length;
Expand Down
5 changes: 0 additions & 5 deletions dev/core/src/com/google/gwt/dev/PrecompileOnePerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,6 @@ private boolean precompilePermutation(TreeLogger logger,
return false;
}

// TODO: precompile should do this after we get the parameter passing refactored.
if (!options.shouldSaveSource()) {
precompilation.removeSourceArtifacts(logger);
}

File precompilationFile = new File(compilerWorkDir,
getPrecompileFilename(permId));
Util.writeObjectAsFile(logger, precompilationFile, precompilation);
Expand Down

0 comments on commit ddf1364

Please sign in to comment.