use "sourceRoot" in map instead to concatenate it to every source file name

This commit is contained in:
Volker Berlin 2019-12-31 14:06:30 +01:00
parent 7fa099298f
commit 515123d4e1
3 changed files with 25 additions and 9 deletions

View File

@ -63,8 +63,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private final boolean createSourceMap; private final boolean createSourceMap;
private final String javaSourceMapBase;
private WasmOutputStream codeStream = new WasmOutputStream(); private WasmOutputStream codeStream = new WasmOutputStream();
private List<TypeEntry> functionTypes = new ArrayList<>(); private List<TypeEntry> functionTypes = new ArrayList<>();
@ -104,7 +102,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
this.target = target; this.target = target;
// for now we build the source map together with debug names // for now we build the source map together with debug names
createSourceMap = options.debugNames(); createSourceMap = options.debugNames();
javaSourceMapBase = options.getSourceMapBase();
} }
/** /**
@ -294,7 +291,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
} }
wasm.writeSection( SectionType.Code, stream ); wasm.writeSection( SectionType.Code, stream );
SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter() : null; SourceMapWriter sourceMap = createSourceMap ? new SourceMapWriter( options.getSourceMapBase() ) : null;
if( sourceMap != null ) { if( sourceMap != null ) {
int offset = wasm.size() - start - stream.size(); int offset = wasm.size() - start - stream.size();
for( Function func : functions.values() ) { 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 { protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
if( createSourceMap ) { if( createSourceMap ) {
int idx = name.className.lastIndexOf( '/' ); 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(); codeStream.reset();
} }

View File

@ -21,6 +21,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.annotation.Nullable;
/** /**
* Generates Source Map version 3. * Generates Source Map version 3.
* *
@ -28,12 +30,24 @@ import java.util.Map.Entry;
*/ */
public class SourceMapWriter { 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 final LinkedHashMap<String, Integer> sourceFileNames = new LinkedHashMap<String, Integer>();
private int nextSourceFileNameIndex; 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. * Adds a mapping for the given node. Mappings must be added in order.
* *
@ -61,6 +75,11 @@ public class SourceMapWriter {
out.append( "{\n" ); out.append( "{\n" );
appendJsonField( out, "version", "3" ); appendJsonField( out, "version", "3" );
// sourceRoot
if( sourceRoot != null && !sourceRoot.isEmpty() ) {
appendJsonField( out, "sourceRoot", sourceRoot );
}
// the source file names // the source file names
out.append( ",\n" ); out.append( ",\n" );
appendJsonField( out, "sources", "[" ); appendJsonField( out, "sources", "[" );

View File

@ -10,7 +10,7 @@ public class SourceMapWriterTest {
@Test @Test
public void simple() throws IOException { 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( 0, 1, "Test1.java" ) );
map.addMapping( new SourceMapping( 5, 2, "Test1.java" ) ); map.addMapping( new SourceMapping( 5, 2, "Test1.java" ) );