Skip to content

Commit

Permalink
Lazy. Update lazy item calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
turansky committed Sep 16, 2024
1 parent 9be8ea1 commit 58320e6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package seskar.gradle.plugin

data class LazyItem(
val imports: String? = null,
val body: String
val body: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package seskar.gradle.plugin

interface LazyItemFactory {
fun create(
source: String,
export: String,
): LazyItem?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seskar.gradle.plugin

import seskar.gradle.plugin.Modules.ORIGINAL_MODULE_SUFFIX
import java.io.FilterReader
import java.io.Reader
import java.io.StringReader
Expand All @@ -19,32 +18,33 @@ private fun lazyComponentTransformer(
val componentProvider = getComponentProvider(writer.toString())
?: return StringReader("export {}")

val componentName = componentProvider
.removePrefix("get_")
.substringBefore("__react__component")
val lazyItems = listOf(componentProvider)
.map(::createLazyItem)

val originalComponentPath = "./$componentName$ORIGINAL_MODULE_SUFFIX"
val imports = lazyItems.asSequence()
.mapNotNull { it.imports }
.distinct()
.joinToString("\n")

// language=javascript
val proxyBody = """
import { lazy } from "react"
const component = lazy(() =>
import("$originalComponentPath")
.then(module => module.${componentProvider}())
.then(component => ({ default: component }))
)
const provider = () => component
export {
provider as $componentProvider,
}
""".trimIndent()
val proxyBody = sequenceOf(imports)
.plus(lazyItems.map { it.body })
.joinToString("\n\n")

return StringReader(proxyBody)
}

private val LAZY_ITEM_FACTORIES = setOf(
ReactLazyComponentFactory(),
)

private fun createLazyItem(
export: String,
): LazyItem =
LAZY_ITEM_FACTORIES.asSequence()
.mapNotNull { it.create(export) }
.firstOrNull()
?: error("Unable to transform export '$export' to lazy item.")

private fun getComponentProvider(
content: String,
): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seskar.gradle.plugin

import seskar.gradle.plugin.Modules.ORIGINAL_MODULE_SUFFIX

class ReactLazyComponentFactory :
LazyItemFactory {
override fun create(
export: String,
): LazyItem {
val fileName = export
.removePrefix("get_")
.substringBefore("__react__component")

val originalComponentPath = "./$fileName$ORIGINAL_MODULE_SUFFIX"

val componentName = "$fileName\$\$__react__lazy__component"

// language=javascript
val body = """
const $componentName = lazy(() =>
import("$originalComponentPath")
.then(module => module.${export}())
.then(component => ({ default: component }))
)
export const $export = () => $componentName
""".trimIndent()

return LazyItem(
imports = """import { lazy } from "react"""",
body = body,
)
}
}

0 comments on commit 58320e6

Please sign in to comment.