如何使用kotlin-frontend-plugin设置package.json配置

时间:2017-12-21 14:04:09

标签: gradle npm webpack kotlin

我希望以指向Kotlin生成的JavaScript文件的方式配置自动生成的package.json。这是我当前的gradle配置:

apply plugin: 'kotlin-platform-js'
apply from: "$project.rootDir/gradle/deploy.gradle"


dependencies {
               expectedBy project(":")

               // Compile/implementation dependencies
               implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}

apply plugin: 'org.jetbrains.kotlin.frontend'

compileKotlin2Js {
                    kotlinOptions.metaInfo = true
                    kotlinOptions.sourceMap = true
                    kotlinOptions.moduleKind = 'commonjs'
}

kotlinFrontend {
                    sourceMaps = true

                    npm {
                           dependency("kotlin")
                           replaceVersion("kotlin-js-library", "1.1.0")
                    }

                    define "PRODUCTION", true

                    webpackBundle {
                                      bundleName = "${project.name}"
                                      sourceMapEnabled = true
                    }
}

这将输出位于$ project.builDir.path / classes / nain中的myProject.js文件,这是我希望在生成的package.json文件中反映出来的。但是输出的package.json文件(位于项目的构建目录中)是这样的:

{
    "name": "myProject",
    "version": "1.2.0-10-SNAPSHOT",
    "description": "simple description",
    "main": "myProject",
    "dependencies": {
         "kotlin": "*"
    },
    "devDependencies": {
        "webpack": "*",
        "webpack-dev-server": "*",
        "source-map-loader": "*",
        "karma": "*",
        "qunitjs": "1.23.1",
        "karma-qunit": "*",
        "karma-sourcemap-loader": "*",
        "karma-phantomjs-launcher": "*",
        "phantomjs-prebuilt": "*",
        "karma-webpack": "*"
    }
}

问题是这样,package.json中的“main”属性不指向classes / main / myProject.js中的bundle文件。我试着查看文档,找到如何将main属性设置为特定目录和js文件,但找不到它。我只能通过在gradle文件的webpackBundle部分中设置bundleName属性来更改名称。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以我找到了一个能够使用gradle发布到npm的工作。我在代码中添加了以下内容:

apply plugin: "com.moowork.node"

//Must copy the output javascript file
//to the location that the package.json expects it to be
//This is semi-workaround since I have not found a way to
//configure the outputted package.json file
task copyJSFile(type: Copy){

    from "$project.buildDir.path/classes/main/"
    into "${project.buildDir}"
    include "*.js"
}

//We publish our project to npm
task npmPublish(dependsOn: copyJSFile, type: NpmTask) {
    description = "publishes the project to npm"
    workingDir = file("${project.buildDir}")
    args = ['publish']
}

这样做是将输出的JavaScript生成的文件复制到调用npmPublish gradle任务时配置package.json的位置。