Change the way kotlin.js source map paths are postprocessed

The idea is that we do no longer use `sourceMapBaseDirs` parameter
of `compileKotlin2Js` task to remove prefix from source paths,
but instead preserve the full paths relative to the output directory.
This allows us to avoid duplicate file names and to identify
source files more reliably.
Then, after sources have been embedded in the source map, we remove
several relative prefixes from source paths.
This commit is contained in:
Ilya Gorbunov
2021-04-17 10:34:52 +03:00
parent 29d2a6acee
commit b59f668a50
+18 -19
View File
@@ -20,12 +20,9 @@ configurations {
def builtinsSrcDir = "${buildDir}/builtin-sources"
def builtinsSrcDir2 = "${buildDir}/builtin-sources-for-builtins"
def commonSrcDir = "${projectDir}/../src/kotlin"
def commonSrcDir2 = "${projectDir}/../common/src"
def jsCommonDir = "${projectDir}/../js"
def builtinsDir = "${rootDir}/core/builtins"
def unsignedCommonSrcDir = "${rootDir}/libraries/stdlib/unsigned/src"
def jsSrcDir = "src"
def jsCommonSrcDir = "${jsCommonDir}/src"
@@ -142,11 +139,6 @@ compileKotlin2Js {
kotlinOptions {
outputFile = "${buildDir}/classes/main/kotlin.js"
sourceMap = true
sourceMapPrefix = "./"
sourceMapBaseDirs = files(
[builtinsSrcDir, jsSrcDir, jsCommonSrcDir, commonSrcDir, commonSrcDir2, unsignedCommonSrcDir]
.collect { file(it).absoluteFile }
)
freeCompilerArgs += [
"-Xopt-in=kotlin.RequiresOptIn",
"-Xopt-in=kotlin.ExperimentalMultiplatform",
@@ -203,6 +195,7 @@ task compileJs(type: NoDebugJavaExec) {
def sourceMapFile = file(jsOutputMapFileName)
def jsOutputMetaFile = file(jsOutputMetaFileName)
def compileMetaFile = file(compileKotlin2Js.outputFile.path.replaceAll('\\.js$', '.meta.js'))
def mainJsOutputDir = compileKotlin2Js.destinationDirectory
doLast {
ant.replaceregexp(
file: jsOutputFileName,
@@ -222,17 +215,7 @@ task compileJs(type: NoDebugJavaExec) {
def sourceMap = new groovy.json.JsonSlurper().parseText(sourceMapFile.text)
def sourceMapBasePaths = [
"./",
"libraries/stdlib/js-v1/src/js/",
"libraries/stdlib/js-v1/src/",
]
sourceMap.sources = sourceMap.sources.collect { sourcePath ->
def prefixToRemove = sourceMapBasePaths.find { basePath -> sourcePath.startsWith(basePath) }
if (prefixToRemove != null) sourcePath.substring(prefixToRemove.length()) else sourcePath
}
def sourceMapSourcesBaseDirs = ["$projectDir/$jsSrcDir", jsCommonSrcDir, "$projectDir/$jsSrcJsDir", builtinsSrcDir, commonSrcDir, commonSrcDir2, projectDir, unsignedCommonSrcDir]
def sourceMapSourcesBaseDirs = [mainJsOutputDir.get(), "${jsCommonDir}/runtime", projectDir, rootDir]
sourceMap.sourcesContent = sourceMap.sources.collect { sourceName ->
def text = sourceMapSourcesBaseDirs.collect { new File("$it/$sourceName") }.find { it.exists() }?.text
@@ -240,6 +223,22 @@ task compileJs(type: NoDebugJavaExec) {
text
}
def sourceMapBasePaths = [
"../../../../",
"../../../",
"../../",
"./",
"libraries/stdlib/js-v1/src/"
]
def shortPaths = sourceMap.sources.collect { sourcePath ->
def prefixToRemove = sourceMapBasePaths.find { basePath -> sourcePath.startsWith(basePath) }
if (prefixToRemove != null) sourcePath.substring(prefixToRemove.length()) else sourcePath
}
if (shortPaths.size != shortPaths.toUnique().size) {
logger.warn("Duplicate source file names found:\n${sourceMap.sources.toSorted().join("\n")}")
}
sourceMap.sources = shortPaths
sourceMapFile.text = groovy.json.JsonOutput.toJson(sourceMap)
jsOutputMetaFile.text = compileMetaFile.text