Skip to content

Commit d0c59d6

Browse files
fix(svelte): fix new import alias resolution, support custom registry
1 parent 8091d5a commit d0c59d6

File tree

5 files changed

+67
-26
lines changed

5 files changed

+67
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
## [Unreleased]
1414

15+
### Fixed
16+
17+
- [Svelte] Fix invalid imports for added or updated components
18+
- [Svelte] Add support for custom registry
19+
1520
## [0.9.6] - 2025-08-23
1621

1722
### Fixed

src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class Source<C : Config>(val project: Project, private val serializer:
3939
private val log = logger<Source<C>>()
4040
private var config: C? = null
4141

42-
protected val domain: String
42+
protected open val domain: String
4343
get() = URI(getLocalConfig().`$schema`).let { uri ->
4444
"${uri.scheme}://${uri.host}".also {
4545
log.debug("Parsed domain: $it")

src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/config/SvelteConfig.kt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ sealed class SvelteConfig : Config() {
2121
/**
2222
* The schema URL for the file.
2323
*/
24-
override val `$schema`: String = "https://shadcn-svelte.com/schema.json"
24+
override val `$schema` = "https://shadcn-svelte.com/schema.json"
2525

2626
/**
2727
* The Tailwind configuration.
@@ -50,8 +50,38 @@ sealed class SvelteConfig : Config() {
5050
@Serializable
5151
data class Aliases(
5252
override val components: String,
53-
override val utils: String
54-
) : Config.Aliases()
53+
override val utils: String,
54+
val ui: String = $$"$lib/components/ui",
55+
val hooks: String = $$"$lib/hooks",
56+
val lib: String = $$"$lib"
57+
) : Config.Aliases() {
58+
59+
fun fromRaw(raw: String) = when (raw.uppercase()) {
60+
Alias.COMPONENTS.name -> components
61+
Alias.UTILS.name -> utils
62+
Alias.UI.name -> ui
63+
Alias.HOOKS.name -> hooks
64+
Alias.LIB.name -> lib
65+
else -> null
66+
}
67+
68+
enum class Alias {
69+
COMPONENTS,
70+
UI,
71+
HOOKS,
72+
UTILS,
73+
LIB;
74+
75+
val slotName = name.lowercase()
76+
77+
val placeholder = "$$name$"
78+
}
79+
}
80+
81+
/**
82+
* The registry to use for API calls
83+
*/
84+
val registry = "https://shadcn-svelte.com/registry"
5585
}
5686

5787
/**

src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ open class SvelteSource(project: Project) : Source<SvelteConfig>(project, Svelte
2929
private var isJsUnsupportedNotified = false
3030
}
3131

32+
override val domain: String
33+
get() = getLocalConfig().registry
34+
3235
override var framework = "Svelte"
3336

34-
override fun getURLPathForRoot() = "registry/index.json"
37+
override fun getURLPathForRoot() = "/index.json"
3538

36-
override fun getURLPathForComponent(componentName: String) =
37-
"registry/$componentName.json"
39+
override fun getURLPathForComponent(componentName: String) = "/$componentName.json"
3840

3941
override fun usesDirectoriesForComponents() = true
4042

@@ -115,15 +117,20 @@ open class SvelteSource(project: Project) : Source<SvelteConfig>(project, Svelte
115117
val importsPackagesReplacementVisitor = ImportsPackagesReplacementVisitor(project)
116118
runReadAction { file.accept(importsPackagesReplacementVisitor) }
117119
importsPackagesReplacementVisitor.replaceImports { `package` ->
118-
`package`
119-
.replace(Regex("^\\\$lib/registry/[^/]+"), escapeRegexValue(config.aliases.components))
120-
.replace($$"$lib/utils", config.aliases.utils)
120+
var renamedPackage = `package`
121+
SvelteConfig.Aliases.Alias.entries.forEach { alias ->
122+
renamedPackage = renamedPackage.replace(
123+
alias.placeholder,
124+
config.aliases.fromRaw(alias.slotName).orEmpty()
125+
)
126+
}
127+
renamedPackage
121128
}
122129
}
123130

124131
override fun fetchColors(): JsonElement {
125-
val baseColor = getLocalConfig().tailwind.baseColor
126-
return RequestSender.sendRequest("$domain/registry/colors/$baseColor.json").ok {
132+
val config = getLocalConfig()
133+
return RequestSender.sendRequest("${config.registry}/colors/${config.tailwind.baseColor}.json").ok {
127134
Json.parseToJsonElement(it.body)
128135
} ?: throw Exception("Colors not found")
129136
}

src/test/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteReplacementsTests.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class SvelteReplacementsTests : ReplacementsTests() {
1414
baseColor = "slate"
1515
),
1616
aliases = SvelteConfig.Aliases(
17-
components = $$"$lib/components",
18-
utils = $$"$lib/replacedUtils"
17+
components = $$"$lib/replacedComponents",
18+
utils = $$"$lib/replacedUtils",
19+
ui = $$"$lib/components/replacedUi",
20+
hooks = $$"$lib/replacedHooks",
21+
lib = $$"$replacedLib",
1922
)
2023
)
2124

@@ -36,24 +39,20 @@ class SvelteReplacementsTests : ReplacementsTests() {
3639
)
3740
}
3841

39-
fun testImportMatchingComponentsAlias() {
40-
compareImports($$"$lib/components/foo", $$"$lib/registry/default/foo")
42+
fun testImportMatchingAlias() {
43+
compareImports($$"$lib/replacedComponents", $$"$COMPONENTS$")
4144
}
4245

43-
fun testImportNotMatchingComponentAlias1() {
44-
compareImports($$"$lib/components", $$"$lib/registry/foo")
46+
fun testImportContainingAlias() {
47+
compareImports($$"$lib/replacedHooks/myHook", $$"$HOOKS$/myHook")
4548
}
4649

47-
fun testImportNotMatchingComponentAlias2() {
48-
compareImports($$"$lib/notregistry/something/foo", $$"$lib/notregistry/something/foo")
50+
fun testImportMatchingAnywhere() {
51+
compareImports($$"something/$replacedLib", $$"something/$LIB$")
4952
}
5053

51-
fun testImportMatchingUtilsAlias() {
52-
compareImports($$"$lib/replacedUtils", $$"$lib/utils")
53-
}
54-
55-
fun testImportMatchingUtilsAlias2() {
56-
compareImports($$"$lib/replacedUtils.js", $$"$lib/utils.js")
54+
fun testImportNotMatchingWrongCase() {
55+
compareImports($$"$ui$", $$"$ui$")
5756
}
5857

5958
fun testImportNotMatchingUtilsAlias() {

0 commit comments

Comments
 (0)