Paolo Banfi
08/09/2024, 12:34 PMRoot project 'kjs'
\--- Project ':web'
in the web project I have configured the kotlin multiplatform plugin as shown here, in the tutorial on kotlin doc portal about kotlinjs + react.
my problem is that when I run the app I get an error in the browser console about a missing dependency on react-dom/client
``````Paolo Banfi
08/09/2024, 12:35 PMplugins {
kotlin("multiplatform") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
}
kotlin {
js(IR) {
browser {
commonWebpackConfig {
cssSupport {
enabled.set(true)
}
}
}
binaries.executable()
}
sourceSets {
val jsMain by getting {
dependencies {
implementation(enforcedPlatform("org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom:1.0.0-pre.787"))
implementation("org.jetbrains.kotlin-wrappers:kotlin-react")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom")
implementation("org.jetbrains.kotlin-wrappers:kotlin-emotion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
}
}
}
}
in the root project I'm just setting up the repos for all the subprojects:
group = "org.xaos.supply"
version = "1.0-SNAPSHOT"
subprojects{
repositories {
mavenCentral()
}
}
Edoardo Luppi
08/09/2024, 1:07 PMkjs.js
file?
Could you also paste the content of your index.html
file?Edoardo Luppi
08/09/2024, 1:07 PMPaolo Banfi
08/09/2024, 1:22 PMkjs-web.js
file, the file generated relative to my javascript module in the project hierarchyPaolo Banfi
08/09/2024, 1:23 PMRoot project 'kjs' <- no source code
\--- Project ':web' <- multiplatform plugin is applied here, actual kotlin source code is here
Edoardo Luppi
08/09/2024, 1:28 PMPaolo Banfi
08/09/2024, 1:29 PMEdoardo Luppi
08/09/2024, 1:35 PMEdoardo Luppi
08/09/2024, 1:43 PMjsBrowserDevelopmentRun
Paolo Banfi
08/09/2024, 1:46 PMEdoardo Luppi
08/09/2024, 1:49 PMPaolo Banfi
08/09/2024, 1:49 PMEdoardo Luppi
08/09/2024, 1:50 PM<script>
src
value?Paolo Banfi
08/09/2024, 1:50 PMimport react.FC
import react.Fragment
import react.Props
import react.create
import react.dom.client.createRoot
import react.dom.html.ReactHTML.h1
import react.dom.html.ReactHTML.h3
import react.dom.html.ReactHTML.div
import react.dom.html.ReactHTML.p
import react.dom.html.ReactHTML.img
import web.dom.document
fun main() {
val container = document.getElementById("root") ?: error("Couldn't find root container!")
createRoot(container).render(Fragment.create {
h1 {
+"Hello, React+Kotlin/JS!"
}
})
}
Paolo Banfi
08/09/2024, 1:50 PM<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello, Kotlin/JS!</title>
</head>
<body>
<div id="root"></div>
<script src="kjs-web.js"></script>
</body>
</html>
Edoardo Luppi
08/09/2024, 1:51 PMPaolo Banfi
08/09/2024, 1:51 PMEdoardo Luppi
08/09/2024, 1:51 PMPaolo Banfi
08/09/2024, 1:52 PMEdoardo Luppi
08/09/2024, 1:52 PMkotlinx-coroutines-core
, but I suppose for this sample you might just remove it.Paolo Banfi
08/09/2024, 1:53 PMPaolo Banfi
08/09/2024, 1:55 PMPaolo Banfi
08/09/2024, 1:55 PMPaolo Banfi
08/09/2024, 1:55 PMEdoardo Luppi
08/09/2024, 1:56 PMPaolo Banfi
08/09/2024, 1:56 PMPaolo Banfi
08/09/2024, 1:56 PM(function (factory) {
if (typeof define === 'function' && define.amd)
define(['exports', 'react-dom/client', 'react', './kotlin-kotlin-stdlib.js', './kotlin-react.js', './kotlin-react-dom.js', './kotlin-react-core.js'], factory);
else if (typeof exports === 'object')
factory(module.exports, require('react-dom/client'), require('react'), require('./kotlin-kotlin-stdlib.js'), require('./kotlin-react.js'), require('./kotlin-react-dom.js'), require('./kotlin-react-core.js'));
else {
if (typeof globalThis['react-dom/client'] === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'react-dom/client' was not found. Please, check whether 'react-dom/client' is loaded prior to 'kjs:web'.");
}
if (typeof react === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'react' was not found. Please, check whether 'react' is loaded prior to 'kjs:web'.");
}
if (typeof globalThis['kotlin-kotlin-stdlib'] === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'kotlin-kotlin-stdlib' was not found. Please, check whether 'kotlin-kotlin-stdlib' is loaded prior to 'kjs:web'.");
}
if (typeof globalThis['kotlin-react'] === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'kotlin-react' was not found. Please, check whether 'kotlin-react' is loaded prior to 'kjs:web'.");
}
if (typeof globalThis['kotlin-react-dom'] === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'kotlin-react-dom' was not found. Please, check whether 'kotlin-react-dom' is loaded prior to 'kjs:web'.");
}
if (typeof globalThis['kotlin-react-core'] === 'undefined') {
throw new Error("Error loading module 'kjs:web'. Its dependency 'kotlin-react-core' was not found. Please, check whether 'kotlin-react-core' is loaded prior to 'kjs:web'.");
}
globalThis['kjs:web'] = factory(typeof globalThis['kjs:web'] === 'undefined' ? {} : globalThis['kjs:web'], globalThis['react-dom/client'], react, globalThis['kotlin-kotlin-stdlib'], globalThis['kotlin-react'], globalThis['kotlin-react-dom'], globalThis['kotlin-react-core']);
}
}(function (_, $module$react_dom_client_y5z5eu, $module$react, kotlin_kotlin, kotlin_org_jetbrains_kotlin_wrappers_kotlin_react, kotlin_org_jetbrains_kotlin_wrappers_kotlin_react_dom, kotlin_org_jetbrains_kotlin_wrappers_kotlin_react_core) {
'use strict';
//region block: imports
var createRoot = $module$react_dom_client_y5z5eu.createRoot;
var Fragment = $module$react.Fragment;
var toString = kotlin_kotlin.$_$.f3;
var IllegalStateException_init_$Create$ = kotlin_kotlin.$_$.p;
var create = kotlin_org_jetbrains_kotlin_wrappers_kotlin_react.$_$.a;
var ReactHTML_getInstance = kotlin_org_jetbrains_kotlin_wrappers_kotlin_react_dom.$_$.a;
var addChild = kotlin_org_jetbrains_kotlin_wrappers_kotlin_react_core.$_$.b;
var Unit_getInstance = kotlin_kotlin.$_$.g1;
var addChildNode = kotlin_org_jetbrains_kotlin_wrappers_kotlin_react_core.$_$.a;
//endregion
//region block: pre-declaration
//endregion
function main() {
var tmp0_elvis_lhs = document.getElementById('root');
var tmp;
if (tmp0_elvis_lhs == null) {
var message = "Couldn't find root container!";
throw IllegalStateException_init_$Create$(toString(message));
} else {
tmp = tmp0_elvis_lhs;
}
var container = tmp;
var tmp_0 = createRoot(container);
var tmp_1 = Fragment;
tmp_0.render(create(tmp_1, main$lambda));
}
function main$lambda($this$create) {
// Inline function 'react.ChildrenBuilder.invoke' call
// Inline function 'react.dom.html.ReactHTML.h1' call
ReactHTML_getInstance();
// Inline function 'react.IntrinsicType' call
// Inline function 'kotlin.js.unsafeCast' call
// Inline function 'kotlin.js.asDynamic' call
var block = main$lambda$lambda;
addChild($this$create, 'h1', block);
return Unit_getInstance();
}
function main$lambda$lambda($this$h1) {
// Inline function 'react.ChildrenBuilder.unaryPlus' call
// Inline function 'react.ReactNode' call
// Inline function 'kotlin.js.unsafeCast' call
// Inline function 'kotlin.js.asDynamic' call
var tmp$ret$2 = 'Hello, React+Kotlin/JS!';
addChildNode($this$h1, tmp$ret$2);
return Unit_getInstance();
}
function mainWrapper() {
main();
}
mainWrapper();
return _;
}));
//# sourceMappingURL=kjs-web.js.map
Paolo Banfi
08/09/2024, 1:56 PMEdoardo Luppi
08/09/2024, 1:57 PMrequire
-ing other JS files.Edoardo Luppi
08/09/2024, 2:00 PMuseEsModules()
under js { ... }
?Paolo Banfi
08/09/2024, 2:03 PMPaolo Banfi
08/09/2024, 2:04 PMPaolo Banfi
08/09/2024, 2:05 PMEdoardo Luppi
08/09/2024, 2:05 PMmjs
is an extension used to identify JS file which use the ES2015 module system.Paolo Banfi
08/09/2024, 2:06 PMEdoardo Luppi
08/09/2024, 2:06 PMPaolo Banfi
08/09/2024, 2:06 PMEdoardo Luppi
08/09/2024, 2:06 PMPaolo Banfi
08/09/2024, 2:11 PMPaolo Banfi
08/09/2024, 2:12 PMEdoardo Luppi
08/09/2024, 2:23 PMPaolo Banfi
08/09/2024, 2:23 PMEdoardo Luppi
08/09/2024, 2:25 PMEdoardo Luppi
08/09/2024, 2:29 PM<script src="web.js"></script>
Instead. There you goPaolo Banfi
08/09/2024, 2:32 PMPaolo Banfi
08/09/2024, 2:33 PMPaolo Banfi
08/09/2024, 2:33 PMEdoardo Luppi
08/09/2024, 2:35 PMbrowser {
webpackTask {
mainOutputFileName = "custom-name.js"
}
}
Paolo Banfi
08/09/2024, 2:35 PMEdoardo Luppi
08/09/2024, 2:36 PMPaolo Banfi
08/09/2024, 2:36 PMmoduleName
property but it actually doesn't workPaolo Banfi
08/09/2024, 2:37 PMEdoardo Luppi
08/09/2024, 2:39 PMmoduleName
is used when compiling the various modules to their respective JS files.
It's not used to produce the minified file via Webpack.Paolo Banfi
08/09/2024, 2:40 PMjsBrowserDevelopmentRun
task the minified version of the jaavascript file is run
• the reference file is the one I find in the build/dist directory under the gradle subprojectEdoardo Luppi
08/09/2024, 2:44 PMthe reference file under build/distTo distribute it (e.g., to a webserver) it's correct IIRC. The Webpack tasks will also output the bundled/minified files under
build/kotlin-webpack
Paolo Banfi
08/09/2024, 2:44 PMEdoardo Luppi
08/09/2024, 2:45 PMbrowser {
distribution { ... }
}
DSL in case.Edoardo Luppi
08/09/2024, 2:45 PMPaolo Banfi
08/09/2024, 2:46 PMEdoardo Luppi
08/09/2024, 2:46 PM