Ch8n
07/05/2024, 1:38 PMjsMain
module, which will be called from a front-end written in React with Vite.
I used the KMP Wizard to generate the Ktor server and added the jsMain
module manually. In the composeApp
package's build.gradle.kts
, I added this block to configure JavaScript:
js {
useCommonJs()
moduleName = "kotlinJs"
browser {
testTask {
enabled = false
}
webpackTask {
outputFileName = "kotlin.js"
output.libraryTarget = COMMONJS2
}
@OptIn(ExperimentalDistributionDsl::class)
distribution {
outputDirectory.set(projectDir.resolve("output"))
}
}
binaries.executable()
}
In the shared
module's build.gradle.kts
, I only added:
js {
browser()
binaries.executable()
}
When I run jsBrowserProductionWebpack
, it creates the kotlin.js
bundle. However, I’m having trouble importing this JS file into my React project.
Any help would be greatly appreciated!Ch8n
07/05/2024, 1:41 PMimport { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import commonjs from 'vite-plugin-commonjs'
// <https://vitejs.dev/config/>
export default defineConfig({
plugins: [
react(),
commonjs()
],
})
React app components is
import * as kotlin from "./kotlin"
function App() {
console.log("==== start =====");
console.log(kotlin);
return (
<>
<div className="text-xl">Hello Ch8n!</div>
</>
)
}
export default App
Artem Kobzar
07/05/2024, 2:49 PMkotlin
namespace?
And could you describe the problem itself? Is it a runtime error or you just don't see the expected declarations in JS?Ch8n
07/05/2024, 4:00 PMUncaught SyntaxError: The requested module '/src/kotlin.js' does not provide an export named 'composeApp' (at App.jsx:1:10)
inspecting source kotlin.js file shows this errorArtem Kobzar
07/05/2024, 4:28 PMCh8n
07/05/2024, 4:31 PMjsBrowserProductionWebpack
task, then copy paste generated js and mapping file into src directoryturansky
07/05/2024, 4:53 PMturansky
07/05/2024, 4:54 PMCh8n
07/05/2024, 5:58 PMturansky
07/05/2024, 6:37 PMkotlin.js.ir.output.granularity=whole-program
in gradle.properties
2. target="es2015"
in compilation task
3. Use result of ...ProductionExecutable
taskCh8n
07/06/2024, 7:40 AMkotlin.js.ir.output.granularity=whole-program
in gradle.properties
2. added below to compilation task
tasks.withType<KotlinJsCompile>().configureEach {
compilerOptions {
target = "es2015"
}
}
do i need to update or remove
useCommonJs()
...
webpackTask {
outputFileName = "kotlin.js"
output.libraryTarget = COMMONJS2
}
...
turansky
07/06/2024, 12:00 PMwebpackTask {
enabled = false
}
Ch8n
07/07/2024, 10:41 AMcompileProductionExecutableKotlinJs
but there is no .js file in any build folders of composeAppModule or in shared module. there is a sikko.js file in root level build folder but it doesn't have my code generated from my file.
I have created a repository if you can check my configuration,it would be a great help https://github.com/ch8n/kmm-full-stackturansky
07/07/2024, 10:48 AMtasks.compileProductionExecutableKotlinJs {
doLast {
println(this.destinationDir)
}
}
turansky
07/07/2024, 10:50 AM%subproject%/build/compileProduction...
directoryturansky
07/07/2024, 11:16 AM%root%/build/js/packages/composeApp
?Ch8n
07/07/2024, 11:23 AMturansky
07/07/2024, 11:26 AMwhich I exposed using Json exportDo you want to embed JSON inside Kotlin/JS distribution?
Ch8n
07/07/2024, 11:27 AMturansky
07/07/2024, 11:28 AM@JsExport
?Ch8n
07/07/2024, 11:31 AM@OptIn(ExperimentalJsExport::class)
@JsExport
object Injector : KoinComponent {
fun test(): String = "Hello from Kotlin Singleton!"
}
generated sikko.js or KotlinProject-shared.js both don't have this Injector
object or functionturansky
07/07/2024, 11:33 AM./gradlew clean
./gradlew composeApp:compileProductionExecutableKotlinJs
turansky
07/07/2024, 11:33 AMcomposeApp/build
and build/js/packages/composeApp
turansky
07/07/2024, 11:35 AMCh8n
07/07/2024, 11:36 AMturansky
07/07/2024, 11:37 AMmoduleName in js config block to kotlinJSSorry, but it’s very bad name from debugging perspective
turansky
07/07/2024, 11:37 AMkotlin.js
- default name of stdlib fileCh8n
07/07/2024, 11:37 AMturansky
07/07/2024, 11:38 AMcomposeApp.mjs
- is what you needCh8n
07/07/2024, 11:40 AMKotlinProject-composeApp.mjs
turansky
07/07/2024, 11:40 AMturansky
07/07/2024, 11:41 AMmoduleName
is preferredCh8n
07/07/2024, 11:41 AMturansky
07/07/2024, 11:46 AMMETA-INF
and index.html
)turansky
07/07/2024, 11:46 AMCh8n
07/07/2024, 11:47 AMCh8n
07/07/2024, 2:34 PMKotlinProject-composeApp.mjs
directly I'm able to module object in logturansky
07/07/2024, 2:36 PMkotlin.js
)Ch8n
07/07/2024, 2:38 PMCh8n
07/07/2024, 3:45 PMCh8n
07/07/2024, 3:55 PMturansky
07/07/2024, 3:56 PMCh8n
07/07/2024, 3:58 PM_1
will remain same or can change?turansky
07/07/2024, 4:07 PMCh8n
07/07/2024, 4:12 PM