I'm attempting to build a React app in KotlinJS an...
# javascript
m
I'm attempting to build a React app in KotlinJS and JS alongside one another. This is related to a similar question someone asked before in this thread and linked to this StackOverflow question. I'm able to make the dependency work as stated in that post, but unfortunately, React has custom DSL and requires compiling with
npm run-script build
(i.e.
react-scripts build
). Does anyone know a way for Gradle to compile the React JavaScript code in my JS module and then use that compiled code as the dependency to include and interop?
t
1 option - Gradle Exec 2 option - Use Node from Kotlin/JS plugin
m
Thanks for the reply! Do you have any further insight on running node from the Kotlin/JS plugin @turansky? It seems Google is short on answers for how to accomplish that
Ideally I'd be able to use the npm dependencies from Kotlin/JS rather than a separate package.json
t
@Sergei Grishchenko Do you have example?
s
I hope it will be helpful
Copy code
val kotlinJsTarget = project.kotlinExtension.targets.firstNotNullOf { it as? KotlinJsTarget }
        val kotlinJsCompilation =
            kotlinJsTarget.compilations.first { it.name == KotlinCompilation.MAIN_COMPILATION_NAME }

        NodeJsExec.create(kotlinJsCompilation, "convert") {
            group = KARAKUM_GRADLE_PLUGIN_GROUP

            dependsOn("unpackEmbeddedJsLibrary")

            inputFileProperty.set(embeddedJsLibrary.file("karakum-converter.js"))

            doLast {
                println("Hello from plugin 'team.karakum.converter'")
            }
        }
It will work approximately this way
Copy code
node karakum-converter.js
All deps will be handle according to npm env of gradle project, you also can dig into
NodeJsExec
task to explore more about custom node tasks
m
Awesome! Thanks for the example I will see if I can make that work