【问题标题】:KotlinJS + Typescript: Atomicfu TraceBase reference not found in d.ts fileKotlinJS + Typescript:在 d.ts 文件中找不到 Atomicfu TraceBase 参考
【发布时间】:2022-01-23 23:20:32
【问题描述】:

使用coroutinektor 依赖项,当KotlinJS npm 库发布时,在生成的打字稿d.ts 文件中有atomicfu 引用

export namespace kotlinx.atomicfu {
    function atomic$ref$<T>(initial: T, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicRef<T>;
    function atomic$boolean$(initial: boolean, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicBoolean;
    function atomic$int$(initial: number, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicInt;
    function atomic$long$(initial: kotlin.Long, trace: kotlinx.atomicfu.TraceBase): kotlinx.atomicfu.AtomicLong;
}

但文件中缺少TraceBase 的引用,并且在编译依赖于上述kotlinJS 库的Typescript 应用程序时,它抱怨缺少TraceBase

前进的唯一方法是在tsconfig 文件中添加skipLibCheck = true。对于图书馆的用户来说,这不是一个理想的解决方案。

有没有办法解决这个问题?

【问题讨论】:

    标签: typescript kotlin kotlin-multiplatform kotlin-js


    【解决方案1】:

    这是一个已知的编译器错误。创建了一个问题来修复它:https://youtrack.jetbrains.com/issue/KT-50464

    暂时您可以使用 Gradle 从生成的 d.ts 文件中删除所有 atomicfu 引用来解决它。

    以下解决方案假设您使用this Gradle plugin NPM package publishing。它提供了一个packJsNpmPublication 任务,用于构建js 库发布代码,并为生成的文件提供特定命名。

    你应该在下面添加两个 gradle 任务

    // This task copies content of "js" build folder into a "temp" folder
    
    tasks.register<Copy>("copyJsBuildContentToTemp") {
        this.from("$buildDir/publications/npm/js/")
        this.into("$buildDir/publications/npm/js/temp/")
    }
    
    // This task goes through your `d.ts` file from `temp` folder,
    // Removes all the lines containing `atomicfu` word and then last line with '}'
    // Then copies the modified file back into `js` folder that replaces the original file
    // At the end, it deletes the `temp` folder
    
    tasks.register<Copy>("removeAtomicFu") {
        var atomicfu = false
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
        from(buildDir.resolve("$buildDir/publications/npm/js/temp/${rootProject.name}-${project.name}.d.ts")) {
            filter {
                when {
                    it.contains("atomicfu") -> {
                        atomicfu = true
                        ""
                    }
                    atomicfu && it == "}" -> {
                        atomicfu = false
                        ""
                    }
                    else -> it
                }
            }
        }
        this.into("$buildDir/publications/npm/js/")
        dependsOn("copyJsBuildContentToTemp")
        doLast {
            delete("$buildDir/publications/npm/js/temp")
        }
    }
    
    // This makes sure that `removeAtomicFu` tasks runs at the end of task 
    // `assembleJsNpmPublication` so when `pack` or `publish` runs and creates 
    // the `tarball` file, it would put update content in the `tgz` file
    project.afterEvaluate {
        tasks.findByName("assembleJsNpmPublication")?.finalizedBy("removeAtomicFu")
    }
    

    现在,如果您运行 ./gradlew packJsNpmPublication,您将获得没有 atomicfu 引用的构建内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-16
      • 2020-02-16
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 2021-04-23
      • 2019-02-28
      • 2019-01-02
      相关资源
      最近更新 更多