【发布时间】:2020-06-14 11:52:12
【问题描述】:
我有一个想要导出的 Kotlin JS 项目,以便在非 Kotlin React 应用程序中使用它。
我尝试过的事情(假设模块名为exportedlib):
- 将其导出为 CommonJS 模块,使用
gradlew compileKotlinJs编译它。
然后我将 build/js/packages/exportedlib/kotlin/exportedlib.js 复制到 React 应用程序并在 App.js 中使用 import exportedlib from './exportedlib' 导入它。
当使用 npm start 编译时,我会收到以下错误消息:Module not found: Can't resolve 'kotlin'
- 然后我还将 kotlin.js 从
build/js/packages_imported/kotlin/1.3.72/kotlin.js导入到 React 应用程序中。
然后我收到错误消息:
./src/kotlin.js
Line 2:39: 'define' is not defined no-undef
- 如上所述,我还在 build.gradle 中添加了浏览器目标,并将其导出为
gradlew browserDistribution。
然后我从 npm 收到这些错误消息:
./src/exportedlib.js
Line 1:1: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 1:112: 'define' is not defined no-undef
Line 1:123: 'define' is not defined no-undef
Line 1:500: Expected an assignment or function call and instead saw an expression no-unused-expressions
// ... a lot of other "Expected an assignment or function call and instead saw an expression" error messages
谁能帮我导出一个 Kotlin JS 库,以便在 React 应用程序中使用它?
这是我的 build.gradle:
plugins {
id 'org.jetbrains.kotlin.js' version '1.3.72'
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js"
testImplementation "org.jetbrains.kotlin:kotlin-test-js"
}
kotlin.target.nodejs { }
// added as above didn't work
kotlin.target.browser { }
compileKotlinJs.kotlinOptions.moduleKind = "commonjs"
更新
vanyochek 的答案在使用 ./gradlew compileProductionExecutableKotlinJs 导出时适用于我,但仅适用于带有实验性 IR 后端的 Kotlin 1.4 M2。
我们将不胜感激 Kotlin 1.3 的任何解决方案。
【问题讨论】:
-
嗨,我也有同样的问题。你有没有设法让它工作?