在IntelliJ Idea中使用Gradle包含本地JavaScript模块依赖

huangapple go评论70阅读模式
英文:

Including a local Javascript module dependency in Intellij Idea using Gradle

问题

我有一个IntelliJ Idea项目,其中包含两个模块,一个模块是Kotlin/JS模块(模块A),另一个是Javascript/node.js模块(模块B)。模块A可以使用名为browserProductionWebpack(或browserDevelopmentWebpack)的Gradle任务创建一个打包的Javascript文件。最佳设置方法是让模块B将模块A的打包Javascript文件作为依赖项。我不确定应该使用什么标准方法来实现这一点。

英文:

I have an Intellij Idea project that has two modules, one module is a Kotlin/JS module (module A), the other a Javascript/node.js module (module B). Module A can create a bundled Javascript file using a gradle task called browserProductionWebpack (or browserDevelopmentWebpack). What is the best way to set it up so that the Module B has the Module A bundled javascript file as a dependency? I'm not sure what the canonical way to do this would be.

答案1

得分: 1

有两种主要方法可以实现这个。

第一种方法 - binaries.library()

您可以在Gradle的JS定义中使用binaries.library()标志,这个库本身将可被Webpack使用。要做到这一点,您需要在package.json中包含文件的直接路径。

build.gradle.kts

kotlin {
  js(IR) {
    moduleName = "moduleA"
    binaries.library()
    
    compilations["main"].packageJson {
      customField("name", "myJsModule")
    }
  }
}

package.json:

{
  "name": "web",
  "version": "1.0.0",
  "private": "true",
  "dependencies": {
    "myJsModule": "file:../../moduleA/build/production"
  },
  "scripts": {
    "preinstall": "gradle :myProjectName:moduleA: browserProductionWebpack"
  }
}

此文件中有两个关键部分。

首先,“myProjectName”定义被硬编码为包含“binaries.library()`”输出的文件夹。

其次,“preinstall”脚本定义将在运行“npm install”时作为gradle任务的一部分运行。这是一种快捷方式,但不是必需的。

第二种方法 - 用于NPM打包的Gradle插件

有多个可用于通过Gradle执行此工作并打包一个可从标准JS项目中使用的NPM包的插件。这可以在本地“托管”,并且我们可以使用上面提到的相同基于路径的结构,或者您可以将其推送到NPM并使用NPM来处理注册。

对于这种方法,一个可能的插件是:
npm-publish

它们在其README中提供了基于NPM的说明,这是另一种使用Gradle构建输出的方法。

英文:

There are two main ways to do this.

First - binaries.library()

You can use the binaries.library() flag in your gradle JS definition, and this library itself will be consumable by Webpack. To do this you would include a direct path to the file in the package.json

build.gradle.kts

kotlin {
  js(IR) {
    moduleName = "moduleA"
    binaries.library()
    
    compilations["main"].packageJson {
      customField("name", "myJsModule")
    }
  }
}

package.json:

{
  "name": "web",
  "version": "1.0.0",
  "private": "true",
  "dependencies": {
    "myJsModule": "file:../../moduleA/build/production"
  },
  "scripts": {
    "preinstall": "gradle :myProjectName:moduleA: browserProductionWebpack"
  }
}

There are 2 keys within this file.

First the "myProjectName" definition is hard coded to the folder containing a binaries.library() output.

Second the script definition of preinstall will run the gradle task as part of running the npm install. This is a shortcut, but not necessary.

Second - Gradle Plugin for NPM packaging

There are multiple plugins available to do this work via Gradle, and then package an NPM package that is usable from a standard JS project. This can be "hosted" locally, and we would do the same path based structure mentioned above, or you can push it to NPM and have NPM help with that registration.

For this method, one possible plugin is:
npm-publish

They have the NPM based instructions in their README, and this is another method to consume the output of your gradle build.

huangapple
  • 本文由 发表于 2023年6月22日 02:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定