Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.petuska.npm.publish.extension.domain.json

import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

/**
* [exports field](https://nodejs.org/api/packages.html#main-entry-point-export)
*/
public abstract class Exports : GenericJsonObject() {

internal abstract val paths: MapProperty<String, ExportedPath>

override fun finalise(): MutableMap<String, Any> = super.finalise().apply {
paths.get().forEach { (path, info) ->
put(path, info.finalise())
}
}
}

/**
* A path that will be exported in [Exports]
*/
public abstract class ExportedPath : GenericJsonObject() {

/**
* If set, this path will be set in "default"
*/
@get:Input
@get:Optional
public abstract val default: Property<String>

/**
* If set, this path will be set in "types"
*/
@get:Input
@get:Optional
public abstract val types: Property<String>

/**
* If set, this path will be set in "import"
*/
@get:Input
@get:Optional
public abstract val import: Property<String>

/**
* If set, this path will be set in "require"
*/
@get:Input
@get:Optional
public abstract val require: Property<String>

override fun finalise(): MutableMap<String, Any> = super.finalise().apply {
types.finalOrNull?.let {
put("types", it)
}
default.finalOrNull?.let {
put("default", it)
}
require.finalOrNull?.let {
put("require", it)
}
import.finalOrNull?.let {
put("import", it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.petuska.npm.publish.extension.domain.json

import dev.petuska.npm.publish.util.configure
import org.gradle.api.Action
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -163,6 +164,13 @@ public abstract class PackageJson : GenericJsonObject() {
@get:Optional
public abstract val bundledDependencies: SetProperty<String>

/**
* [exports](https://docs.npmjs.com/files/package.json#bundleddependencies)
*/
@get:Input
@get:Optional
public abstract val exports: Property<Exports>

// region DSL

/**
Expand Down Expand Up @@ -286,6 +294,45 @@ public abstract class PackageJson : GenericJsonObject() {
optionalDependencies.configure(action)
}

/**
* Set exports to a single path that will map "."
* @see [exports]
*/
public infix fun Property<Exports>.by(path: String) {
configure({ exports ->
exports.paths.configure { paths ->
paths["."] = instance(ExportedPath::class).apply {
default.set(path)
}
}
})
}

/**
* Set exports to a single path that will map ".", this path may have several export
* @see [exports]
*/
public infix fun Property<Exports>.by(pathAction: Action<ExportedPath>) {
this by mapOf("." to pathAction)
}

/**
* Configure exports to a map of configurable paths
* @see [exports]
*/
public infix fun Property<Exports>.by(paths: Map<String, Action<ExportedPath>>) {
exports.set(
instance(Exports::class).also {
it.paths.putAll(
paths.mapValues { (_, action) -> ExportedPath(action) }
)
}
)
}

private fun ExportedPath(action: Action<ExportedPath>): ExportedPath =
instance(ExportedPath::class).apply(action::execute)

// endregion

@Suppress("ComplexMethod")
Expand Down Expand Up @@ -319,5 +366,6 @@ public abstract class PackageJson : GenericJsonObject() {
peerDependencies.finalOrNull?.let { put("peerDependencies", it.finalise()) }
optionalDependencies.finalOrNull?.let { put("optionalDependencies", it.finalise()) }
bundledDependencies.final?.let { put("bundledDependencies", it) }
exports.finalOrNull?.let { put("exports", it) }
}
}
9 changes: 9 additions & 0 deletions samples/local-ts-consumer/kt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dev.petuska.npm.publish.extension.domain.json.ExportedPath

plugins {
kotlin("multiplatform")
id("dev.petuska.npm.publish")
Expand All @@ -20,4 +22,11 @@ kotlin {

npmPublish {
organization.set("mpetuska")
packages {
named("js") {
packageJson {
exports by "index.mjs"
}
}
}
}