Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify node-modules loading for wasmJs and Js targets #289

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions core/js/src/node/initializers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/

package kotlinx.io.node

internal actual fun bufferInitializer(): BufferModule? = js("eval('require')('buffer')")
internal actual fun osInitializer(): Os? = js("eval('require')('os')")
internal actual fun fsInitializer(): Fs? = js("eval('require')('fs')")
internal actual fun pathInitializer(): Path? = js("eval('require')('path')")
39 changes: 0 additions & 39 deletions core/js/src/node/nodeModulesJs.kt

This file was deleted.

6 changes: 5 additions & 1 deletion core/nodeFilesystemShared/src/node/buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ internal external interface Buffer {
fun writeInt8(value: Byte, offset: Int)
}

internal expect val buffer: BufferModule
internal val buffer: BufferModule by lazy {
loadModule("buffer", ::bufferInitializer)
}

internal expect fun bufferInitializer(): BufferModule?
6 changes: 5 additions & 1 deletion core/nodeFilesystemShared/src/node/fs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ internal external interface realpathSync {
fun native(path: String): String
}

internal expect val fs: Fs
internal val fs: Fs by lazy {
loadModule("fs", ::fsInitializer)
}

internal expect fun fsInitializer(): Fs?
19 changes: 19 additions & 0 deletions core/nodeFilesystemShared/src/node/modLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/

package kotlinx.io.node

import kotlinx.io.withCaughtException

internal fun <T> loadModule(name: String, initializer: () -> T?): T {
var mod: T? = null
val ex = withCaughtException {
mod = initializer()
}
if (mod == null) {
throw UnsupportedOperationException("Module '$name' could not be loaded", ex)
}
return mod!!
}
6 changes: 5 additions & 1 deletion core/nodeFilesystemShared/src/node/os.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ internal external interface Os {
fun platform(): String
}

internal expect val os: Os
internal val os: Os by lazy {
loadModule("os", ::osInitializer)
}

internal expect fun osInitializer(): Os?
6 changes: 5 additions & 1 deletion core/nodeFilesystemShared/src/node/path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ internal external interface Path {
val sep: String
}

internal expect val path: Path
internal val path: Path by lazy {
loadModule("path", ::pathInitializer)
}

internal expect fun pathInitializer(): Path?
11 changes: 11 additions & 0 deletions core/wasmJs/src/node/initializers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/

package kotlinx.io.node

internal actual fun bufferInitializer(): BufferModule? = js("eval('require')('buffer')")
internal actual fun osInitializer(): Os? = js("eval('require')('os')")
internal actual fun fsInitializer(): Fs? = js("eval('require')('fs')")
internal actual fun pathInitializer(): Path? = js("eval('require')('path')")
47 changes: 0 additions & 47 deletions core/wasmJs/src/node/nodeModulesWasmJs.kt

This file was deleted.