karakum-team / karakum Goto Github PK
View Code? Open in Web Editor NEWConverter of TypeScript declaration files to Kotlin declarations
License: Apache License 2.0
Converter of TypeScript declaration files to Kotlin declarations
License: Apache License 2.0
Since Karakum's config is likely to be checked in version control, it would be best if it supported Linux-style relative paths even on Windows.
At the moment, the Gradle plugin seems to concatenate Windows absolute paths with the Linux relative path from the config (e.g. when substituting <nodeModules>
). This happens for everything except the output
property, which is correctly converted.
Example input config <project>/karakum.config.json
:
{
"input": "<nodeModules>/@blueprintjs/icons/lib/esm/index.d.ts",
"output": "src/jsMain/generated",
"libraryName": "blueprintjs-icons"
}
Output config in <project>/build/karakum/karakum.config.json
:
{
"input" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\node_modules/@blueprintjs/icons/lib/esm/index.d.ts",
"output" : "C:\\Projects\\kotlin-blueprintjs\\kotlin-blueprintjs-icons\\src\\jsMain\\generated",
"libraryName" : "blueprintjs-icons",
"plugins" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/plugins/*.js",
"annotations" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/annotations/*.js",
"nameResolvers" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/nameResolvers/*.js",
"inheritanceModifiers" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/inheritanceModifiers/*.js"
}
When this happens, the input file is actually not found and nothing is generated. This happens silently by the way, no error in the build log, and the build doesn't fail:
> Task :kotlin-blueprintjs-icons:generateKarakumExternals
Source files root: /
Source files count: 0
Covered nodes: 0
Uncovered nodes: 0
BUILD SUCCESSFUL in 941ms
Are there any plans for jsWasm support?
In the current latest version of this tool, when I'm trying to follow the Basic Usage guide, I can observe that the generated file has an empty JsModule
name:
If I remove the moduleNameMapper
section from karakum.config.json
, the JsModule name seems to be generated fine i.e. @file:JsModule("js-file-download/js-file-download")
โ
NPM modules have a typings
(or types
) property in their package.json
to define a path to the .d.ts
file describing the types for the module:
https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#including-declarations-in-your-npm-package
It would be nice if Karakum could figure out the path on its own based on that, so the config could just be the name of a package instead of the users having to inspect the package.json
of each module on their own and update the path to the typings file in case it changes in the original package.
For instance, a basic Karakum config could simply be:
{
"inputModule": "@blueprintjs/icons",
"output": "generated"
}
And both the input
path and the libraryName
could be derived from the inputModule
.
Of course, we could offer optional configuration to override the path to the node_modules
, or override the input path to the declarations if need be. But as a default that would be great.
Example input config:
{
"input" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\node_modules\\@blueprintjs\\icons\\lib\\esm\\index.d.ts",
"output" : "C:\\Projects\\kotlin-blueprintjs\\kotlin-blueprintjs-icons\\src\\jsMain\\generated",
"libraryName" : "blueprintjs-icons",
"plugins" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/plugins/*.js",
"annotations" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/annotations/*.js",
"nameResolvers" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/nameResolvers/*.js",
"inheritanceModifiers" : "C:\\Projects\\kotlin-blueprintjs\\build\\js\\packages\\kotlin-blueprintjs-kotlin-blueprintjs-icons\\karakum/inheritanceModifiers/*.js"
}
The resulting files are generated under:
src/jsMain/generated/blueprintjsiconsCProjectskotlinblueprintjsbuildjsnode_modulesblueprintjsiconslibesm
I'm assuming we're taking the last path element somewhere by relying on /
separator, and on Windows this means the last segment is everything.
Hi, this library is great, it would be really helpful if you could create a JSON Schema file for it, so that we can get autocompletion from any IDE by just using the schema file.
Hi there
I am trying to use this plugin to generate Kotlin declarations for https://www.npmjs.com/package/@jitsi/react-sdk
I followed the readme and this is how my karakum.config.json
file looks like:
{
"input": "node_modules/@jitsi/react-sdk/lib/components/JitsiMeeting.d.ts",
"output": "generated",
"libraryName": "@jitsi/react-sdk"
}
But when I try running the following command: npx karakum --config karakum.config.json
, I get the following error stack:
shubhamsingh@Shubhams-MacBook-Pro test_react % npx karakum --config karakum.config.json
Plugin file: /Users/shubhamsingh/IdeaProjects/test_react/karakum/plugins/convertNamespace.js
Source files root: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@jitsi/react-sdk/lib/components/
Source files count: 64
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/react/global.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/react/global.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/csstype/index.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/csstype/index.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/prop-types/index.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/propTypes/index.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/scheduler/tracing.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/scheduler/tracing.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/react/index.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/react/index.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@jitsi/react-sdk/lib/types/JitsiMeetExternalApi.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@jitsi/reactSdk/lib/types/JitsiMeetExternalApi.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/node/assert.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/node/assert.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/node/assert/strict.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/node/assert/strict.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/node/globals.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/node/globals.kt
Source file: /Users/shubhamsingh/IdeaProjects/test_react/node_modules/@types/node/async_hooks.d.ts
Target file: /Users/shubhamsingh/IdeaProjects/testReact/nodeModules/@types/node/asyncHooks.kt
Error: Unhandled vararg declaration for args parameter
at convertParameterDeclarationWithFixedType (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/convertParameterDeclaration.js:87:19)
at Object.render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/convertParameterDeclaration.js:15:12)
at render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/render.js:13:35)
at CommentsPlugin.render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/CommentsPlugin.js:29:43)
at render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/render.js:13:35)
at /Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/convertFunctionType.js:21:110
at Array.map (<anonymous>)
at Object.render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/convertFunctionType.js:21:93)
at render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/render.js:13:35)
at CommentsPlugin.render (/Users/shubhamsingh/IdeaProjects/test_react/node_modules/karakum/build/converter/plugins/CommentsPlugin.js:29:43)
I'm not sure how to proceed forward. Any help would be appreciated โญ
P.S. this his how my convertNameSpace.js
file looks like:
const ts = require("typescript");
module.exports = (node, context, render) => {
if (ts.isModuleDeclaration(node) && ts.isModuleBlock(node.body)) {
return node.body.statements
.map(statement => render(statement))
.join("\n")
}
return null
}
From what I could see, customizations of Karakum are currently done by writing plugins in TS.
The target audience for this tool seems to be people using Kotlin primarily, and who are trying to wrap typescript libraries into Kotlin externals so they can use it from Kotlin/JS. I guess it's fair to assume that those people want to write Kotlin code.
Are there any plans to add the possibility to write plugins for Karakum in Kotlin?
It seems that the Karakum Gradle plugin assumes the Kotlin multiplatform plugin is applied. In my project where I apply kotlin("js")
, I'm getting the following:
org.gradle.api.UnknownDomainObjectException: Extension of type 'KotlinMultiplatformExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, LibrariesForLibs, VersionCatalogsExtension, KotlinJsProjectExtension, KotlinTestsRegistry, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaToolchainService, JavaPluginExtension, PublishingExtension, GithubInfoExtension, SigningExtension]
at org.gradle.internal.extensibility.ExtensionsStorage.getHolderByType(ExtensionsStorage.java:88)
at org.gradle.internal.extensibility.ExtensionsStorage.getByType(ExtensionsStorage.java:74)
at org.gradle.internal.extensibility.DefaultConvention.getByType(DefaultConvention.java:160)
at io.github.sgrishchenko.karakum.gradle.plugin.tasks.TasksKt.getKotlinJsCompilation(Tasks.kt:26)
at io.github.sgrishchenko.karakum.gradle.plugin.tasks.KarakumGenerate.<init>(KarakumGenerate.kt:29)
at io.github.sgrishchenko.karakum.gradle.plugin.tasks.KarakumGenerate_Decorated.<init>(Unknown Source)
...
This is due to this line:
https://github.com/karakum-team/karakum/blob/master/gradle-plugin/karakum-gradle-plugin/src/main/kotlin/io/github/sgrishchenko/karakum/gradle/plugin/tasks/Tasks.kt#L14
I think it should be modified to get the Kotlin JS compilation from either the MPP plugin or the K/JS plugin depending on which one is present.
Also, note that this is called during the constructor of the KarakumGenerate
task, which might cause problems with plugin application ordering (this forces users to apply the Kotlin MPP plugin before the Karakum plugin). It would be best to configure the Karakum plugin using conventions and plugin application "reaction" blocks like withPlugin
.
Hello, I'm trying to convert PIXI.js
v8 with the following configuration :
{
"input": "node_modules/pixi.js/lib/**/*.d.ts",
"output": "dist",
"libraryName": "pixi.js"
}
But I'm getting the following error :
file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17
return commonPrefix(common, ...rest);
^
RangeError: Maximum call stack size exceeded
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
at commonPrefix (file:///C:/Users/MyUser/IdeaProjects/random/karakum-test/node_modules/karakum/build/utils/fileName.js:17:12)
Here is my package.json
:
{
"name": "karakum-test",
"version": "1.0.0",
"scripts": {
"start": "karakum --config karakum.config.json"
},
"dependencies": {
"pixi.js": "^8.0.0-beta.6"
},
"devDependencies": {
"karakum": "^1.0.0-alpha.25",
"typescript": "^5.2.2"
}
}
I'm trying to generate typings for @blueprintjs/icons
(I'm starting with this simple one as a PoC, and then I intend to move on to more complicated modules of course). See https://github.com/joffrey-bion/kotlin-blueprintjs for my existing declarations.
In this @blueprintjs/icons
NPM module, the package.json
's typings
property points to this lib/esm/index.d.ts
definition file:
export { IconSvgPaths16, IconSvgPaths20, iconNameToPathsRecordKey } from "./iconSvgPaths";
export { getIconContentString, IconCodepoints } from "./iconCodepoints";
export { IconName, IconNames } from "./iconNames";
But when I generate external from it using Karakum, I only get this empty-ish index.kt
file:
@file:JsModule("blueprintjs-icons/index")
@file:Suppress(
"NON_EXTERNAL_DECLARATION_IN_INAPPROPRIATE_FILE",
)
package blueprintjs.icons
/* export { IconSvgPaths16, IconSvgPaths20, iconNameToPathsRecordKey } from "./iconSvgPaths"; */
/* export { getIconContentString, IconCodepoints } from "./iconCodepoints"; */
/* export { IconName, IconNames } from "./iconNames"; */
Here is my config for the Karakum Gradle plugin (in case it's useful):
{
"input": "<nodeModules>/@blueprintjs/icons/lib/esm/index.d.ts",
"output": "src/jsMain/generated",
"libraryName": "blueprintjs-icons"
}
So I have a couple questions:
Why is the JsModule
called blueprintjs-icons/index
? Is this because it's <libraryName>/<originalFileName>
? Could this cause a problem when importing these declarations in Kotlin? Or will this work out of the box?
Why is everything commented?
The README.md file of this repository shows that there's a Kotlin Gradle plugin available i.e.
plugins {
id("io.github.sgrishchenko.karakum") version "1.0.0-alpha.14"
}
but there is no guide that shows how to use it in a project. Could you please at least add an example on how to use it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.