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 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 mappings = new ArrayList<>(); + private final String sourceRoot; - private LinkedHashMap sourceFileNames = new LinkedHashMap(); + private final List mappings = new ArrayList<>(); - private int nextSourceFileNameIndex; + private final LinkedHashMap sourceFileNames = new LinkedHashMap(); + + 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" ) );