gradle-node-plugin icon indicating copy to clipboard operation
gradle-node-plugin copied to clipboard

Allow to set default NODE_PATH in extension for all NodeTasks

Open lamba92 opened this issue 4 years ago • 3 comments

Use case: when building a Kotlin/JS project, the Gradle plugin will create in rootProject build/js a yarn environment with all required modules in all subprojects (which is very handy). When running NodeTasks, it would be reasonable to set once NODE_PATH to said directory in NodeExtension and always rely on the Kotlin/JS dependency resolution.

And/or provide an extension like:

fun NodeTask.setNodeModulesPath(path: String) =
    environment.put("NODE_PATH", path)

fun NodeTask.setNodeModulesPath(folder: File) =
    environment.put("NODE_PATH", path.normalize().absolutePath)

lamba92 avatar Feb 28 '21 11:02 lamba92

I am not sure I understand correctly how the Kotlin/JS plugin works. It downloads all the npm dependencies of all the subprojects thanks to yarn in the $rootProject/build/js, right? Does it also generate a package.json file? Which Node.js interpreter does it use?

bsautel avatar Mar 05 '21 18:03 bsautel

Exactly. A uber yarn workspace is created there with all the dependencies of all subprojects of the build and you can use that environment to do whatever you want. A packgage.json is created for each subproject with the Kotlin/JS plugin based on what dependencies you have imported and from where (a .klib with a Kotlin/JS compilation from Maven or an NPM module from, well, NPM), and recursively for each Kotlin/JS library imported.

I believe that the Kotlin/JS plugin redownlaods NodeJS somewhere the same way the gradle-node-plugin does, but gives no access to its tasks so a third party solution is needed to tinker with the output of the build like I need.

In my use case I need to webpack into a single file my application for NodeJS. I thought that NodeExtension.nodeProjectDir would be used as default NODE_PATH for all NodeTasks but it isn't!

If you want to see for yourself how the build process works for Kotlin/JS + Webpack, try running :examples:js-bot:productionWebpackExecutable or :examples:js-bot:developmentWebpackExecutable of kotlingram.

lamba92 avatar Mar 05 '21 19:03 lamba92

I think this might be possible to do already with execOverrides

But if we're going to start exposing common settings (like --registry and --frozen-lockfile) I think this should also get first-class support

deepy avatar Oct 11 '21 08:10 deepy