kotlin-frontend-plugin icon indicating copy to clipboard operation
kotlin-frontend-plugin copied to clipboard

node_modules_imported is incomplete

Open Polyana-Fontes opened this issue 7 years ago • 3 comments

I have a highly modular project and I'm trying to build a Kotlin/JS application to run on NodeJS. Since we need to write lots of expect stuff I'm creating a gradle module for each NPM and there I write the expect stuff alongside with some extra extensions and helper functions and classes (to use suspend instead of callbacks for example).

So I have a build.gradle like this:

dependencies {
    compile project(':environment:js:headers:helmet-npm')
    compile project(':environment:js:headers:node-npm')
    compile project(':environment:js:headers:windows-cpu-npm')
    compile project(':environment:js:headers:minimist-npm')
    compile project(':environment:js:headers:source-map-support-npm')
}

kotlinFrontend {
    npm {
        dependency "windows-cpu"
        dependency "mime-lookup"
        dependency "express"
        dependency "helmet"
        dependency "minimist"
        dependency "source-map-support"
    }

    webpackBundle {
        bundleName = "main"
        sourceMapEnabled = true
    }
}

Note: Notice that I added a "-npm" suffix to the gradle project's name, that was needed because it was overriding the actual NPM modules as they were getting the same JS name.

When I run:

./gradlew clean
./gradlew build

It fills node_modules_imported only with some dependencies, not all: image

If I try to run my main function using the npm run configuration and pointing to the produced js file (build\classes\kotlin\main\node-manager.js in my case), this happens: image

But if after that I run a clean build targeting the sub-module:

./gradlew clean
./gradlew build
./gradlew :network:node-manager:clean
./gradlew :network:node-manager:build

The node_modules_imported directory gets fully built: image

And the same npm run configuration works as expected (also supporting sourceMap).

So the issue is that the gradle plugin isn't waiting all dependencies be ready before building the node_modules_imported directory.

Polyana-Fontes avatar Aug 31 '18 22:08 Polyana-Fontes

I also noticed the example that I provided is also incomplete.

If I reference my dependencies directly like I did in my previous message, the JS module will not be linked to the node_modules_imported directory and NPM will not be able to run it!

To get everything working I had to create an other module which is neighbor to the actual module (resides on the same parent directory) and apply the actual dependencies to it and finally link that module as dependency to my actual module.

apply plugin: 'kotlin-platform-js'
//apply plugin: 'kotlin-dce-js'
apply plugin: 'org.jetbrains.kotlin.frontend'

def indirect = project.path+'-indirect'
project(indirect) {
    apply plugin: 'kotlin-platform-js'
    dependencies {
        compile project(':environment:js:headers:helmet-npm')
        compile project(':environment:js:headers:node-npm')
        compile project(':environment:js:headers:windows-cpu-npm')
        compile project(':environment:js:headers:minimist-npm')
        compile project(':environment:js:headers:source-map-support-npm')
    }
}

dependencies {
    compile project(indirect)
}

kotlinFrontend {
    npm {
        dependency "windows-cpu"
        dependency "mime-lookup"
        dependency "express"
        dependency "helmet"
        dependency "minimist"
        dependency "source-map-support"
    }
}

Now I will be able to get all modules in the node_modules_imported and node_modules directory doing these commands:

./gradlew clean
./gradlew build
./gradlew :network:node-manager:clean
./gradlew :network:node-manager:build

Polyana-Fontes avatar Sep 01 '18 22:09 Polyana-Fontes

I've uploaded a sample project showing this issue.

It has a module named 'sample-module' with the main code and a bunch of submodules inside of environment:

There's a bat file to clean, build and run the project.

The "should-work" branch shows how I was trying to organize the project originally, directly referencing my submodules as dependencies and trying to build it fully with gradlew clean build. https://github.com/joserobjr/kotlin-frontend-plugin-issue-104/tree/should-work

Dependencies of :sample-module
runtimeClasspath - Runtime classpath of source set 'main'.
+--- project :environment:js:headers:minimist-npm
|    \--- project :environment:js
|         +--- project :environment:core
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|         |    \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.25.0
|         |         +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|         |         \--- org.jetbrains.kotlinx:atomicfu-common:0.11.3
|         |              \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|         +--- org.jetbrains.kotlinx:kotlinx-coroutines-core-js:0.25.0
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib-js:1.2.61
|         |    |    \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|         |    +--- org.jetbrains.kotlinx:atomicfu-js:0.11.3
|         |    \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.25.0 (*)
|         \--- org.jetbrains.kotlin:kotlin-stdlib-js:1.2.61 (*)
+--- project :environment:js:headers:source-map-support-npm
|    \--- project :environment:js (*)
\--- project :environment:js (*)

The module "workaround" shows what I had to do to make it work, which is adding an indirect modules to indirectly add dependencies to what I need and adding two extra commands for each Kotlin/JS project that I need to run the main function to the batch.

https://github.com/joserobjr/kotlin-frontend-plugin-issue-104/tree/workaround

Dependencies of :sample-module
runtimeClasspath - Runtime classpath of source set 'main'.
+--- project :sample-module-indirect
|    +--- project :environment:js:headers:minimist-npm
|    |    \--- project :environment:js:indirect
|    |         \--- project :environment:js
|    |              +--- project :environment:core
|    |              |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|    |              |    \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.25.0
|    |              |         +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|    |              |         \--- org.jetbrains.kotlinx:atomicfu-common:0.11.3
|    |              |              \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|    |              +--- org.jetbrains.kotlinx:kotlinx-coroutines-core-js:0.25.0
|    |              |    +--- org.jetbrains.kotlin:kotlin-stdlib-js:1.2.61
|    |              |    |    \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.61
|    |              |    +--- org.jetbrains.kotlinx:atomicfu-js:0.11.3
|    |              |    \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-common:0.25.0 (*)
|    |              \--- org.jetbrains.kotlin:kotlin-stdlib-js:1.2.61 (*)
|    +--- project :environment:js:headers:source-map-support-npm
|    |    \--- project :environment:js:indirect (*)
|    \--- project :environment:js:indirect (*)
\--- project :environment:js:indirect (*)

Polyana-Fontes avatar Sep 02 '18 00:09 Polyana-Fontes

I personally ran into the same issue and worked around it the same way.

UnknownJoe796 avatar Feb 27 '19 08:02 UnknownJoe796