简体   繁体   English

是否可以为 Java 和 JavaScript/TypeScript 创建库

[英]Is it possible to create library for Java & JavaScript/TypeScript

Im angular developer.我是 angular 开发人员。

In our front & back exists some magic calculation methods.在我们的前后存在一些神奇的计算方法。

Classes same, but when anyone find bug in calculation need to fix it in two different projects.类相同,但是当有人发现计算中的错误时需要在两个不同的项目中修复它。 maybe there is a way to create a generic codebase (maby function) that can be converted to js(or ts) & java and update two libraries based on the two results obtained也许有一种方法可以创建可以转换为 js(或 ts)和 java 的通用代码库(maby 函数),并根据获得的两个结果更新两个库

You could try to use kotlin.您可以尝试使用 kotlin。

Kotlin transpiles to JavaScript and also compiles to java bytecode . Kotlin 转译为 JavaScript编译为 java 字节码

However, you can only access kotlin utilities and neither access java or JS/TS types if you want to use the code in both java and ts/js code but you can use the kotlin stdlib . However, you can only access kotlin utilities and neither access java or JS/TS types if you want to use the code in both java and ts/js code but you can use the kotlin stdlib .

But if it really is just a calculation, you may not need java/js specific classes/functions.但如果它真的只是一个计算,你可能不需要 java/js 特定的类/函数。

However, as VLAZ mentioned in the comments, you should consider doing the calculation only once in the backend.但是,正如评论中提到的VLAZ ,您应该考虑只在后端进行一次计算。

Setting this up in IntelliJ在 IntelliJ 中进行设置

You can create such a project in IntelliJ by sekecting Kotlin in the New Project Window and using the project template Library .您可以通过在New Project Kotlin中选择 Kotlin 并使用项目模板Library在 IntelliJ 中创建这样的项目。

在此处输入图像描述

Make sure you have the targets common , jvm and js .确保你有目标commonjvmjs Since you didn't say you would do native stuff, you don't need the native target.因为你没有说你会做原生的东西,所以你不需要native目标。

在此处输入图像描述

You can then use the kotlin library in both JavaScript (eg Angular) and Java projects as a dependency.然后,您可以在 JavaScript(例如 Angular)和 Java 项目中使用 kotlin 库作为依赖项。

From a Java project, you can reference KOTLIN_PROJECT/build/classes/kotlin/jvm/main (this directory contains compiled Java classes).从 Java 项目中,您可以参考KOTLIN_PROJECT/build/classes/kotlin/jvm/main (此目录包含已编译的 Java 类)。

From a JavaScript (eg Angular) project, you can reference KOTLIN_PROJECT/build/js/packages/kotlinToJavaAndJS .从 JavaScript(例如 Angular)项目中,您可以参考KOTLIN_PROJECT/build/js/packages/kotlinToJavaAndJS

As a proof of concept, I have made this repository on GitHub .作为概念证明,我在 GitHub 上创建了这个存储库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM