Skip to content

Commit

Permalink
Lazy. Calculate data from export
Browse files Browse the repository at this point in the history
  • Loading branch information
turansky committed Sep 16, 2024
1 parent 58320e6 commit 41227b5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seskar.gradle.plugin

import seskar.gradle.plugin.Modules.ORIGINAL_MODULE_SUFFIX

data class LazyItemData(
val name: String,
val originalFilePath: String,
val type: LazyItemType,
val export: String,
)

fun LazyItemData(
export: String,
): LazyItemData {
val fileName = export
.removePrefix("get_")
.substringBefore("__react__component")

return LazyItemData(
name = fileName,
originalFilePath = "./$fileName$ORIGINAL_MODULE_SUFFIX",
type = LazyItemType.LAZY_REACT_COMPONENT,
export = export,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package seskar.gradle.plugin

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

enum class LazyItemType {
LAZY_FUNCTION,
LAZY_REACT_COMPONENT,

;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seskar.gradle.plugin

import seskar.gradle.plugin.LazyItemType.LAZY_REACT_COMPONENT
import java.io.FilterReader
import java.io.Reader
import java.io.StringReader
Expand Down Expand Up @@ -33,17 +34,17 @@ private fun lazyComponentTransformer(
return StringReader(proxyBody)
}

private val LAZY_ITEM_FACTORIES = setOf(
ReactLazyComponentFactory(),
private val LAZY_ITEM_FACTORY_MAP = mapOf(
LAZY_REACT_COMPONENT to 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.")
): LazyItem {
val data = LazyItemData(export)
val factory = LAZY_ITEM_FACTORY_MAP.getValue(data.type)
return factory.create(data)
}

private fun getComponentProvider(
content: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package seskar.gradle.plugin

import seskar.gradle.plugin.Modules.ORIGINAL_MODULE_SUFFIX
// language=javascript
private val IMPORTS = """
import { lazy } from "react"
""".trimIndent()

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

val originalComponentPath = "./$fileName$ORIGINAL_MODULE_SUFFIX"

val componentName = "$fileName\$\$__react__lazy__component"
val componentName = "${data.name}\$\$__react__lazy__component"

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

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

0 comments on commit 41227b5

Please sign in to comment.