Setting output path for Kotlin/JS distribution / webpack output

Viewed 408

When building my project using the browserDistribution Gradle task, Kotlin/JS puts the output which has been processed by webpack into /build/distributions/myProject.js.

Instead, I would like my output to go into a folder called /output in my project's root directory.

How can I change the path where my distributions are put after running through webpack?

I'm using the Gradle Kotlin DSL if that helps.

2 Answers

The Kotlin/JS Gradle plugin provides a distribution API which can be used to adjust the output directory of a project. To put the final distribution output of a project into a folder called output in the project root, one can use the following snippet (it works in both Gradle Kotlin DSL and Groovy Gradle):

browser {
    distribution {
        directory = file("$projectDir/output/")
    }
}

The output path can be set via the outputPath property in the commonWebpackConfig or the destinationDirectory property in the webpackTask block , e.g.:

// kotlin dsl
kotlin {
    js {
        binaries.executable()
        browser {
            // either configure here
            commonWebpackConfig {
                outputPath = file("$projectDir/build/distributions/assets/js")
            }

            // or here
            webpackTask {
                destinationDirectory = file("$projectDir/build/distributions/assets/js")
            }
        }
    }
}

Related