From 515123d4e1e75c5e868d7cc2efce16f8d12ce51c Mon Sep 17 00:00:00 2001 From: Volker Berlin <volker.berlin@googlemail.com> Date: Tue, 31 Dec 2019 14:06:30 +0100 Subject: [PATCH] use "sourceRoot" in map instead to concatenate it to every source file name --- .../binary/BinaryModuleWriter.java | 7 ++---- .../sourcemap/SourceMapWriter.java | 25 ++++++++++++++++--- .../sourcemap/SourceMapWriterTest.java | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index bba690d..ad9012c 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -63,8 +63,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod private final boolean createSourceMap; - private final String javaSourceMapBase; - private WasmOutputStream codeStream = new WasmOutputStream(); private List<TypeEntry> functionTypes = new ArrayList<>(); @@ -104,7 +102,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod this.target = target; // for now we build the source map together with debug names createSourceMap = options.debugNames(); - javaSourceMapBase = options.getSourceMapBase(); } /** @@ -294,7 +291,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod } wasm.writeSection( SectionType.Code, stream ); - SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null; + SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter( options.getSourceMapBase() ) : null; if( sourceMap != null ) { int offset = wasm.size() - start - stream.size(); for( Function func : functions.values() ) { @@ -563,7 +560,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { if( createSourceMap ) { int idx = name.className.lastIndexOf( '/' ); - this.javaSourceFile = javaSourceMapBase + name.className.substring( 0, idx + 1 ) + sourceFile; + this.javaSourceFile = name.className.substring( 0, idx + 1 ) + sourceFile; } codeStream.reset(); } diff --git a/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriter.java b/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriter.java index ac78845..2f6c1db 100644 --- a/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriter.java +++ b/src/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriter.java @@ -21,6 +21,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map.Entry; +import javax.annotation.Nullable; + /** * Generates Source Map version 3. * @@ -28,11 +30,23 @@ import java.util.Map.Entry; */ public class SourceMapWriter { - private List<SourceMapping> mappings = new ArrayList<>(); + private final String sourceRoot; - private LinkedHashMap<String, Integer> sourceFileNames = new LinkedHashMap<String, Integer>(); + private final List<SourceMapping> mappings = new ArrayList<>(); - private int nextSourceFileNameIndex; + private final LinkedHashMap<String, Integer> sourceFileNames = new LinkedHashMap<String, Integer>(); + + private int nextSourceFileNameIndex; + + /** + * Create a new instance of the writer for a single map file. + * + * @param sourceRoot + * optional absolute or relative path to the sources + */ + public SourceMapWriter( @Nullable String sourceRoot ) { + this.sourceRoot = sourceRoot; + } /** * Adds a mapping for the given node. Mappings must be added in order. @@ -61,6 +75,11 @@ public class SourceMapWriter { out.append( "{\n" ); appendJsonField( out, "version", "3" ); + // sourceRoot + if( sourceRoot != null && !sourceRoot.isEmpty() ) { + appendJsonField( out, "sourceRoot", sourceRoot ); + } + // the source file names out.append( ",\n" ); appendJsonField( out, "sources", "[" ); diff --git a/test/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriterTest.java b/test/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriterTest.java index 82d76fa..aa2765e 100644 --- a/test/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriterTest.java +++ b/test/de/inetsoftware/jwebassembly/sourcemap/SourceMapWriterTest.java @@ -10,7 +10,7 @@ public class SourceMapWriterTest { @Test public void simple() throws IOException { - SourceMapWriter map = new SourceMapWriter(); + SourceMapWriter map = new SourceMapWriter( null ); map.addMapping( new SourceMapping( 0, 1, "Test1.java" ) ); map.addMapping( new SourceMapping( 5, 2, "Test1.java" ) );