Allow to embed source files into JS source maps
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.parser.sourcemaps
|
||||
|
||||
class SourceMap {
|
||||
import java.io.Reader
|
||||
|
||||
class SourceMap(val sourceContentResolver: (String) -> Reader?) {
|
||||
val groups = mutableListOf<SourceMapGroup>()
|
||||
}
|
||||
|
||||
|
||||
+3
-5
@@ -16,10 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.parser.sourcemaps
|
||||
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsLocation
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsNode
|
||||
import org.jetbrains.kotlin.js.backend.ast.RecursiveJsVisitor
|
||||
import org.jetbrains.kotlin.js.backend.ast.SourceInfoAwareJsNode
|
||||
import org.jetbrains.kotlin.js.backend.ast.*
|
||||
|
||||
class SourceMapLocationRemapper(val sourceMaps: Map<String, SourceMap>) {
|
||||
fun remap(node: JsNode) {
|
||||
@@ -62,7 +59,8 @@ class SourceMapLocationRemapper(val sourceMaps: Map<String, SourceMap>) {
|
||||
}
|
||||
|
||||
val segment = group.segments[lastSegmentIndex]
|
||||
node.source = JsLocation(segment.sourceFileName, segment.sourceLineNumber, segment.sourceColumnNumber)
|
||||
val location = JsLocation(segment.sourceFileName, segment.sourceLineNumber, segment.sourceColumnNumber)
|
||||
node.source = JsLocationWithEmbeddedSource(location, sourceMap) { sourceMap.sourceContentResolver(segment.sourceFileName) }
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
import java.io.IOException
|
||||
import java.io.Reader
|
||||
import java.io.StringReader
|
||||
|
||||
object SourceMapParser {
|
||||
@Throws(IOException::class)
|
||||
@@ -72,6 +73,20 @@ object SourceMapParser {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val sourcesContent: List<String?> = if (jsonObject.has("sourcesContent")) {
|
||||
val sourcesContentProperty = jsonObject.get("sourcesContent") as? JSONArray ?:
|
||||
return SourceMapError("'sourcesContent' property is not of array type")
|
||||
if (sourcesContentProperty.any { it != JSONObject.NULL && it !is String? }) {
|
||||
return SourceMapError("'sources' array must contain strings")
|
||||
}
|
||||
sourcesContentProperty.map { if (it == JSONObject.NULL) null else it as String? }
|
||||
}
|
||||
else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
val sourcePathToContent = sources.zip(sourcesContent).associate { it }
|
||||
|
||||
if (!jsonObject.has("mappings")) return SourceMapError("'mappings' property not found")
|
||||
val mappings = jsonObject.get("mappings") as? String ?: return SourceMapError("'mappings' property is not of string type")
|
||||
|
||||
@@ -80,7 +95,7 @@ object SourceMapParser {
|
||||
var sourceColumn = 0
|
||||
var sourceIndex = 0
|
||||
val stream = MappingStream(mappings)
|
||||
val sourceMap = SourceMap()
|
||||
val sourceMap = SourceMap { sourcePathToContent[it]?.let { StringReader(it) } }
|
||||
var currentGroup = SourceMapGroup().also { sourceMap.groups += it }
|
||||
|
||||
while (!stream.isEof) {
|
||||
|
||||
Reference in New Issue
Block a user