I have a Kotlin JS project that I like to export so that it can be used in a non-Kotlin React app.
Things I tried (let's say the module is called exportedlib):
- Export it as CommonJS module, compiling it with
gradlew compileKotlinJs.
I then copied build/js/packages/exportedlib/kotlin/exportedlib.js to the React app and imported it with import exportedlib from './exportedlib' in App.js.
When compiling with npm start I then get this error message: Module not found: Can't resolve 'kotlin'
- I then also imported kotlin.js from
build/js/packages_imported/kotlin/1.3.72/kotlin.jsinto the React app.
Then I get the error message:
./src/kotlin.js
Line 2:39: 'define' is not defined no-undef
- As above didn't work I also added the browser target in build.gradle and exported it with
gradlew browserDistribution.
Then I get these error messages from 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
Can anybody help me to export a Kotlin JS lib so that it can be used in a React app?
Here's my 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"
Update
vanyochek's answer works for me when exported with ./gradlew compileProductionExecutableKotlinJs, but only works for Kotlin 1.4 M2 with experimental IR backend.
Any solution for Kotlin 1.3 would be appreciated.