我将Kotlin编译为JS,并且效果很好,但是浏览器始终显示错误:
在控制台日志中。
这是我的 build.gradle 文件:
group 'org.example'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/web/output.js"
kotlinOptions.moduleKind = "plain"
kotlinOptions.sourceMap = true
kotlinOptions.sourceMapEmbedSources = "always"
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web/js"
dependsOn classes
}
task copyHTMLFolder(type: Copy) {
dependsOn assembleWeb
from file("src/html/")
into file("${projectDir}/web/")
}
task openUrlInBrowser {
dependsOn copyHTMLFolder
doLast {
java.awt.Desktop.desktop.browse "http://localhost:63342/engineEmi/web/index.html".toURI()
}
}
assemble.dependsOn assembleWeb
sourceSets {
main.kotlin.srcDirs += 'src/'
main.java.srcDirs += 'src/'
}
我怎样才能让gradle生成kotlin.ja.map文件?
亲切的问候
最佳答案
傻我文件已创建,但未从META-INF复制,因为仅复制了.js文件。
这是assembleWeb Task的工作实现(请参阅带有path.endsWith的条件
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
(path.endsWith(".js") || path.endsWith(".map")) && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
})
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/web/output.js"
kotlinOptions.moduleKind = "plain"
kotlinOptions.sourceMap = true
kotlinOptions.sourceMapEmbedSources = "always"
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web/js"
dependsOn classes
}