Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit 31e232c

Browse files
committed
fix: alias resolution for css modules
This commit introduces the ability to use aliases when importing css modules. The alias configuration is defined in the svelte.config.js file. The changes include an update to the getCssModuleImports function to handle aliases and a new test case to verify the functionality.
1 parent f187794 commit 31e232c

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

src/utils/css-module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { parseStaticImport, pathToFileURL, resolvePath } from 'mlly';
77
import { stringToUint8Array, uint8ArrayToString } from 'uint8array-extras';
88
import { betterr } from 'betterr';
99
import type { ResolvedOptions } from '../options';
10+
import { addSlash } from './alias';
1011

1112
type getCssModuleImportsProps = {
1213
imports: StaticImport[];
@@ -37,10 +38,11 @@ export async function getCssModuleImports(
3738
throw new Error(`Default import is required for css modules: ${specifier}`);
3839
}
3940

40-
const aliasKey = Object.keys(aliases).find(a => specifier.startsWith(a));
41+
const aliasKey = Object.keys(aliases).find(a => specifier.startsWith(addSlash(a)) || specifier === a);
4142
if (aliasKey != null) {
43+
const alias = aliases[aliasKey];
4244
const s = new MagicString(specifier);
43-
s.overwrite(0, specifier.length, specifier);
45+
s.overwrite(0, aliasKey.length, alias);
4446
return { path: s.toString(), defaultImport, imp };
4547
}
4648

svelte.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import path from 'node:path';
2+
3+
/** @param {...string} args */
4+
function relativePath(...args) {
5+
return path.resolve(import.meta.dirname, ...args);
6+
}
7+
8+
const config = {
9+
kit: {
10+
alias: {
11+
$css: relativePath('./tests/assets'),
12+
},
13+
},
14+
};
15+
16+
export default config;

tests/class/Input.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
22
import s from '../assets/class.module.css';
3+
import s1 from '$css/style.module.css';
34
</script>
45

56
<div class={s.error}>
@@ -9,3 +10,7 @@
910
<div class={s.success}>
1011
world
1112
</div>
13+
14+
<div class={s1.suceessMessage}>
15+
foo
16+
</div>

tests/class/Output.svelte

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<script>
22
const s = {
3-
error: "error_class-module-css-module-test",
4-
success: "success_class-module-css-module-test"
3+
success: "success_class-module-css-module-test",
4+
error: "error_class-module-css-module-test"
5+
};
6+
const s1 = {
7+
error: "error_style-module-css-module-test",
8+
successMessage: "success-message_style-module-css-module-test"
59
};
610
</script>
711

@@ -13,6 +17,10 @@
1317
world
1418
</div>
1519

20+
<div class={s1.suceessMessage}>
21+
foo
22+
</div>
23+
1624
<style>
1725
1826
@@ -24,4 +32,18 @@
2432
color: green;
2533
}
2634
35+
36+
37+
section {
38+
padding: 10px;
39+
}
40+
41+
.error_style-module-css-module-test {
42+
color: red;
43+
}
44+
45+
.success-message_style-module-css-module-test {
46+
color: green;
47+
}
48+
2749
</style>

0 commit comments

Comments
 (0)