[JS CLI] Support sourcemap generation for IR BE in CLI

#KT-46551 In Progress
This commit is contained in:
Zalim Bashorov
2021-07-01 21:54:46 +03:00
committed by teamcityserver
parent 5a3efc1a98
commit 2460f5f9ae
6 changed files with 90 additions and 24 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -516,7 +516,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
}
@NotNull
private static String calculateSourceMapSourceRoot(
static String calculateSourceMapSourceRoot(
@NotNull MessageCollector messageCollector,
@NotNull K2JSCompilerArguments arguments
) {
@@ -284,10 +284,10 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
),
)
val jsCode = if (arguments.irDce && !arguments.irDceDriven) compiledModule.outputsAfterDce!! else compiledModule.outputs!!
outputFile.writeText(jsCode.jsCode)
jsCode.dependencies.forEach { (name, content) ->
outputFile.resolveSibling("$name.js").writeText(content)
val outputs = if (arguments.irDce && !arguments.irDceDriven) compiledModule.outputsAfterDce!! else compiledModule.outputs!!
outputFile.write(outputs)
outputs.dependencies.forEach { (name, content) ->
outputFile.resolveSibling("$name.js").write(content)
}
if (arguments.generateDts) {
val dtsFile = outputFile.withReplacedExtensionOrNull(outputFile.extension, "d.ts")!!
@@ -298,6 +298,15 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
return OK
}
private fun File.write(outputs: CompilationOutputs) {
writeText(outputs.jsCode)
outputs.sourceMap?.let {
val mapFile = resolveSibling("$name.map")
appendText("\n//# sourceMappingURL=${mapFile.name}")
mapFile.writeText(it)
}
}
override fun setupPlatformSpecificArgumentsAndServices(
configuration: CompilerConfiguration,
arguments: K2JSCompilerArguments,
@@ -310,9 +319,22 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.defaultVersion())
// TODO: Support source maps
if (arguments.sourceMap) {
messageCollector.report(WARNING, "source-map argument is not supported yet", null)
configuration.put(JSConfigurationKeys.SOURCE_MAP, true)
if (arguments.sourceMapPrefix != null) {
configuration.put(JSConfigurationKeys.SOURCE_MAP_PREFIX, arguments.sourceMapPrefix!!)
}
var sourceMapSourceRoots = arguments.sourceMapBaseDirs
if (sourceMapSourceRoots == null && StringUtil.isNotEmpty(arguments.sourceMapPrefix)) {
sourceMapSourceRoots = K2JSCompiler.calculateSourceMapSourceRoot(messageCollector, arguments)
}
if (sourceMapSourceRoots != null) {
val sourceMapSourceRootList = StringUtil.split(sourceMapSourceRoots, File.pathSeparator)
configuration.put(JSConfigurationKeys.SOURCE_MAP_SOURCE_ROOTS, sourceMapSourceRootList)
}
} else {
if (arguments.sourceMapPrefix != null) {
messageCollector.report(WARNING, "source-map-prefix argument has no effect without source map", null)