Allow to embed source files into JS source maps

This commit is contained in:
Alexey Andreev
2017-06-14 14:03:37 +03:00
parent 73c37ecd25
commit a0e1bde594
40 changed files with 491 additions and 81 deletions
@@ -196,4 +196,22 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
assertTrue("Source map should contain reference to $sourceFilePath") { map.contains("\"$sourceFilePath\"") }
}
}
@Test
fun testKotlinJsSourceMapInline() {
val project = Project("kotlin2JsProjectWithSourceMapInline", "2.10")
project.build("build") {
assertSuccessful()
val mapFilePath = "app/build/classes/main/app_main.js.map"
assertFileExists(mapFilePath)
val map = fileInWorkingDir(mapFilePath).readText()
assertTrue("Source map should contain reference to main.kt") { map.contains("\"main.kt\"") }
assertTrue("Source map should contain reference to foo.kt") { map.contains("\"foo.kt\"") }
assertTrue("Source map should contain source of main.kt") { map.contains("\"fun main(args: Array<String>) {\\n") }
assertTrue("Source map should contain source of foo.kt") { map.contains("\"inline fun foo(): String {\\n") }
}
}
}
@@ -0,0 +1,36 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
apply plugin: 'kotlin2js'
repositories {
mavenLocal()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/kotlin-dev/" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
compileKotlin2Js {
kotlinOptions.freeCompilerArgs = [ "-Xskip-metadata-version-check" ]
kotlinOptions.sourceMap = true
kotlinOptions.sourceMapEmbedSources = "always"
}
}
project("app") {
dependencies {
compile project(":lib")
}
}
@@ -48,6 +48,13 @@ interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
*/
var sourceMap: kotlin.Boolean
/**
* Embed source files into source map
* Possible values: "never", "always", "inlining"
* Default value: "inlining"
*/
var sourceMapEmbedSources: kotlin.String
/**
* Prefix for paths in a source map
* Default value: null
@@ -59,6 +59,11 @@ internal abstract class KotlinJsOptionsBase : org.jetbrains.kotlin.gradle.dsl.Ko
get() = sourceMapField ?: false
set(value) { sourceMapField = value }
private var sourceMapEmbedSourcesField: kotlin.String? = null
override var sourceMapEmbedSources: kotlin.String
get() = sourceMapEmbedSourcesField ?: "inlining"
set(value) { sourceMapEmbedSourcesField = value }
private var sourceMapPrefixField: kotlin.String?? = null
override var sourceMapPrefix: kotlin.String?
get() = sourceMapPrefixField ?: null
@@ -86,6 +91,7 @@ internal abstract class KotlinJsOptionsBase : org.jetbrains.kotlin.gradle.dsl.Ko
noStdlibField?.let { args.noStdlib = it }
outputFileField?.let { args.outputFile = it }
sourceMapField?.let { args.sourceMap = it }
sourceMapEmbedSourcesField?.let { args.sourceMapEmbedSources = it }
sourceMapPrefixField?.let { args.sourceMapPrefix = it }
targetField?.let { args.target = it }
typedArraysField?.let { args.typedArrays = it }
@@ -104,6 +110,7 @@ internal fun org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments.fil
noStdlib = true
outputFile = null
sourceMap = false
sourceMapEmbedSources = "inlining"
sourceMapPrefix = null
target = "v5"
typedArrays = false
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-js-sourceMapEmbedSources</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-js</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>js</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceMap>true</sourceMap>
<sourceMapEmbedSources>always</sourceMapEmbedSources>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,3 @@
package org.jetbrains
fun bar() = "OK"
@@ -0,0 +1,4 @@
source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath());
assertFileContains("target/js/test-js-sourceMapEmbedSources.js.map", "\"org/jetbrains/HelloWorld.kt\"");
assertFileContains("target/js/test-js-sourceMapEmbedSources.js.map", "\"package org.jetbrains\\n\\nfun bar() = \\\"OK\\\"");
@@ -70,6 +70,9 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
@Parameter
private String sourceMapPrefix;
@Parameter(defaultValue = "inlining")
private String sourceMapEmbedSources;
/**
* Main invocation behaviour. Possible values are <b>call</b> and <b>noCall</b>.
*/
@@ -110,6 +113,7 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
arguments.sourceMap = sourceMap;
arguments.sourceMapPrefix = sourceMapPrefix;
arguments.sourceMapEmbedSources = sourceMapEmbedSources;
if (outputFile != null) {
ConcurrentMap<String, List<String>> collector = getOutputDirectoriesCollector();