Hey! I'm trying to create a kotlin wrapper library...
# javascript
j
Hey! I'm trying to create a kotlin wrapper library for Quill.js. I used Dukat for the base and then fixed some minor conflicts myself. I use multiplatform/js(IR) 1.9.20 as compiler. But when I use the library in my main project (again multiplatform/js(IR) 1.9.20) it keeps generating broken imports. In my research I stumbled over https://youtrack.jetbrains.com/issue/KT-39272, but it seems to be fixed since 1.5.X. Changing the
@JsName
to "Quill" or leaving it out doesn't help. Imports generated by compiler:
Copy code
function (_, $module$parchment, $module$quill, kotlin_kotlin) {
  'use strict';
  //region block: imports
  var default_0 = $module$parchment.default;
  var default_1 = $module$quill.default;
//snip
}
My wrapper class:
Copy code
@JsName("default")
external open class Quill : EventEmitter {
    constructor(container: String)

    //snip

    companion object {
        //snip
        fun import(path: String): Any
    }
}
quill.js
Copy code
import Quill from './core';

//snip

export default Quill;
/core/quill.js
Copy code
//snip

class Quill {

  //snip  

  static import(name) {
    if (this.imports[name] == null) {
      debug.error(`Cannot import ${name}. Are you sure it was registered?`);
    }
    return this.imports[name];
  }

  //snip

  constructor(container, options = {}) {
    //snip
  }

  //snip

}

export { expandConfig, overload, Quill as default };
a
When you say that your imports are broken, what do you mean? What is the expected result with this code?
j
Copy code
var default_1 = $module$quill.default;
Quill.js exposes the "Quill" class/constructor as default export for the Quill module. So when I use the constructor in kotlin, it tries to access that default using the above import.
$module$quill
is the actual module/constructor/class, the
default
property is undefined. I expect kotlin to point to the actual module (so
$module$quill
) instead of the undefined "default" object. According to prior advice given here and on the internet
JsName("default")
is supposed to achieve that. If I use no JsName, it generates
$module$quill.<Name>
instead, which is equally unhelpful. The file containing the wrapper class on kotlin was marked with the following by dukat:
Copy code
@file:JsModule("quill")
@file:JsNonModule
t
1. For start you can check module in console:
Copy code
@JsModule("quill")
external object quill

fun main() {
    console.log("Quill module", quill)
    console.log("Quill module default", quill.asDynamic().default) 
}
2. Use
commonjs
module kind and repeat step 1
j
Thank you for your reply. I tested the object declaration in both projects, main and library. I put the logging into the main project. The output was always: > Quill module function Quill(container) > Quill module default undefined I tried
output.libraryTarget = "commonjs"
for both - library and main project - as well as
useCommonJs()
for the library project. None of those combinations changed anything. Setting
useCommonJs()
for the main project caused issues when loading the kotlin std lib. Also apart from when I set
useCommonJs()
for the main project, I had to use
@JsNonModule
or I would get the following compilation error: > When accessing module declarations from UMD, they must be marked by both @JsModule and @JsNonModule
t
Quill module function Quill(container)
Looks like class, which you need
j
It is. But the generated code uses
Copy code
var default_1 = $module$quill.default;
default_1
as constructor, which is undefined. I can't access the
$module$quill
t
Copy code
@JsModule("quill")
external class Quill
?
j
Wouldn't that require that I remove
@file:JsModule("quill")
from the file? If I understood correctly from the prior discussion that I linked,
@JsName("default")
should mark the default export, which is exactly what I want.
t
There is no
default
inside module according your test results
j
Thank you. I will have a look at this
Annotating the class itself instead of the file did the trick for now, thank you.
😉 1