[JS] Factor out source map path rewriting
This commit is contained in:
@@ -124,37 +124,29 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun mapSourcePaths(inputFile: File, targetFile: File): Boolean {
|
private fun mapSourcePaths(inputFile: File, targetFile: File): Boolean {
|
||||||
val json = try {
|
val jsonOut = StringWriter()
|
||||||
parseJson(inputFile)
|
|
||||||
} catch (e: JsonSyntaxException) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val sourcesArray = (json as? JsonObject)?.properties?.get("sources") as? JsonArray ?: return false
|
|
||||||
val sources = sourcesArray.elements.map {
|
|
||||||
(it as? JsonString)?.value ?: return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val pathCalculator = RelativePathCalculator(targetFile.parentFile)
|
val pathCalculator = RelativePathCalculator(targetFile.parentFile)
|
||||||
val mappedSources = sources.map {
|
val sourceMapChanged = try {
|
||||||
val result = pathCalculator.calculateRelativePathTo(File(inputFile.parentFile, it))
|
SourceMap.mapSources(inputFile.readText(), jsonOut) {
|
||||||
if (result != null) {
|
val result = pathCalculator.calculateRelativePathTo(File(inputFile.parentFile, it))
|
||||||
if (File(targetFile.parentFile, result).exists()) {
|
if (result != null) {
|
||||||
result
|
if (File(targetFile.parentFile, result).exists()) {
|
||||||
|
result
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
it
|
it
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
it
|
|
||||||
}
|
}
|
||||||
|
} catch (e: SourceMapSourceReplacementException) {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedSources == sources) return false
|
if (!sourceMapChanged) return false
|
||||||
|
|
||||||
json.properties["sources"] = JsonArray(*mappedSources.map { JsonString(it) }.toTypedArray())
|
|
||||||
|
|
||||||
targetFile.parentFile.mkdirs()
|
targetFile.parentFile.mkdirs()
|
||||||
OutputStreamWriter(FileOutputStream(targetFile), "UTF-8").use { it.write(json.toString()) }
|
OutputStreamWriter(FileOutputStream(targetFile), "UTF-8").use { it.write(jsonOut.toString()) }
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.js.parser.sourcemaps
|
package org.jetbrains.kotlin.js.parser.sourcemaps
|
||||||
|
|
||||||
import java.io.File
|
import java.io.*
|
||||||
import java.io.PrintStream
|
|
||||||
import java.io.Reader
|
|
||||||
|
|
||||||
class SourceMap(val sourceContentResolver: (String) -> Reader?) {
|
class SourceMap(val sourceContentResolver: (String) -> Reader?) {
|
||||||
val groups = mutableListOf<SourceMapGroup>()
|
val groups = mutableListOf<SourceMapGroup>()
|
||||||
@@ -49,8 +47,48 @@ class SourceMap(val sourceContentResolver: (String) -> Reader?) {
|
|||||||
writer.println()
|
writer.println()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@Throws(IOException::class, SourceMapSourceReplacementException::class)
|
||||||
|
fun replaceSources(sourceMapFile: File, mapping: (String) -> String): Boolean {
|
||||||
|
val content = sourceMapFile.readText()
|
||||||
|
return sourceMapFile.writer().buffered().use {
|
||||||
|
mapSources(content, it, mapping)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class, SourceMapSourceReplacementException::class)
|
||||||
|
fun mapSources(content: String, output: Writer, mapping: (String) -> String): Boolean {
|
||||||
|
val json = try {
|
||||||
|
parseJson(content)
|
||||||
|
} catch (e: JsonSyntaxException) {
|
||||||
|
throw SourceMapSourceReplacementException(cause = e)
|
||||||
|
}
|
||||||
|
val jsonObject = json as? JsonObject ?: throw SourceMapSourceReplacementException("Top-level object expected")
|
||||||
|
val sources = jsonObject.properties["sources"]
|
||||||
|
if (sources != null) {
|
||||||
|
val sourcesArray =
|
||||||
|
sources as? JsonArray ?: throw SourceMapSourceReplacementException("'sources' property is not of array type")
|
||||||
|
var changed = false
|
||||||
|
val fixedSources = sourcesArray.elements.mapTo(mutableListOf<JsonNode>()) {
|
||||||
|
val sourcePath = it as? JsonString ?: throw SourceMapSourceReplacementException("'sources' array must contain strings")
|
||||||
|
val replacedPath = mapping(sourcePath.value)
|
||||||
|
if (!changed && replacedPath != sourcePath.value) {
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
JsonString(replacedPath)
|
||||||
|
}
|
||||||
|
if (!changed) return false
|
||||||
|
jsonObject.properties["sources"] = JsonArray(fixedSources)
|
||||||
|
}
|
||||||
|
jsonObject.write(output)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SourceMapSourceReplacementException(message: String? = null, cause: Throwable? = null) : Exception(message, cause)
|
||||||
|
|
||||||
data class SourceMapSegment(
|
data class SourceMapSegment(
|
||||||
val generatedColumnNumber: Int,
|
val generatedColumnNumber: Int,
|
||||||
val sourceFileName: String?,
|
val sourceFileName: String?,
|
||||||
|
|||||||
Reference in New Issue
Block a user