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

Commit 751f8f2

Browse files
authored
Merge pull request #2 from ryoppippi/feature/test
feat:test
2 parents 4481d37 + b3dc13a commit 751f8f2

File tree

11 files changed

+135
-9
lines changed

11 files changed

+135
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"magic-string": "^0.30.11",
2323
"mlly": "^1.7.1",
2424
"svelte": "5.0.0-next.210",
25+
"svelte-parse-markup": "^0.1.5",
2526
"uint8array-extras": "^1.4.0",
2627
"unconfig": "^0.5.5",
2728
"zimmerframe": "^1.1.2"

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import MagicString from 'magic-string';
22
import type { PreprocessorGroup } from 'svelte/compiler';
3+
import { parse } from 'svelte-parse-markup';
34
import { findStaticImports } from 'mlly';
4-
import { genObjectFromRaw } from 'knitwork';
5+
import { genObjectFromValues } from 'knitwork';
56
import { loadAliases } from './utils/alias';
67
import type { CssModule } from './utils/css-module';
78
import { getCssModule, getCssModuleImports } from './utils/css-module';
@@ -11,6 +12,22 @@ import { getCssModule, getCssModuleImports } from './utils/css-module';
1112
export function cssModules(): PreprocessorGroup {
1213
const cssModuleCache = new Map<string, CssModule[]>();
1314
return {
15+
markup({ content, filename }) {
16+
const ast = parse(content);
17+
18+
/* if css is empty, add style tag */
19+
if (ast.css == null) {
20+
const s = new MagicString(content);
21+
s.append('\n<style>\n</style>\n');
22+
return {
23+
code: s.toString(),
24+
map: s.generateMap({
25+
source: filename,
26+
includeContent: true,
27+
}),
28+
};
29+
}
30+
},
1431
async script({ content, filename }) {
1532
const aliases = await loadAliases();
1633
const s = new MagicString(content);
@@ -21,7 +38,7 @@ export function cssModules(): PreprocessorGroup {
2138
);
2239

2340
/* find css/scss module imports */
24-
const cssModuleImpots
41+
const cssModuleImports
2542
= await getCssModuleImports({
2643
imports,
2744
aliases,
@@ -30,13 +47,13 @@ export function cssModules(): PreprocessorGroup {
3047

3148
/* transform css/scss modules */
3249
const cssModules = [];
33-
for (const cmi of cssModuleImpots) {
50+
for (const cmi of cssModuleImports) {
3451
const cssModule = await getCssModule(cmi);
3552
cssModules.push(cssModule);
3653

3754
/* generate css module exports */
38-
const obj = genObjectFromRaw(cssModule.exports);
39-
const gen = `const ${cmi.defaultImport} = ${obj};`;
55+
const obj = genObjectFromValues(cssModule.exports);
56+
const gen = `const ${cmi.defaultImport} = ${obj};\n`;
4057
s.overwrite(cmi.imp.start, cmi.imp.end, gen);
4158
}
4259
if (filename != null) {

src/utils/css-module.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import to from 'await-to-js';
44
import { transform } from 'lightningcss';
55
import MagicString from 'magic-string';
66
import type { StaticImport } from 'mlly';
7-
import { parseStaticImport, resolvePath } from 'mlly';
7+
import { parseStaticImport, pathToFileURL, resolvePath } from 'mlly';
88
import { stringToUint8Array, uint8ArrayToString } from 'uint8array-extras';
99

1010
type getCssModuleImportsProps = {
@@ -37,16 +37,18 @@ export async function getCssModuleImports(
3737
}
3838

3939
const aliasKey = Object.keys(aliases).find(a => specifier.startsWith(a));
40-
if (aliasKey == null) {
40+
if (aliasKey != null) {
4141
const s = new MagicString(specifier);
4242
s.overwrite(0, specifier.length, specifier);
4343
return { path: s.toString(), defaultImport, imp };
4444
}
4545

46-
const [err, resolved] = await to(resolvePath(specifier, { url: filename }));
46+
const [err, resolved] = await to(resolvePath(specifier, {
47+
url: filename == null ? undefined : pathToFileURL(filename),
48+
}));
4749

4850
if (err != null) {
49-
console.error(`Failed to resolve path: ${specifier}`);
51+
console.error(err.message);
5052
return undefined;
5153
}
5254

src/utils/css.ts

Whitespace-only changes.

tests/assets/class.module.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.error {
2+
color: red;
3+
}
4+
.success {
5+
color: green;
6+
}

tests/assets/style.module.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
section {
2+
padding: 10px;
3+
}
4+
.error {
5+
color: red;
6+
}
7+
.success-message {
8+
color: green;
9+
}

tests/class/Input.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import s from '../assets/class.module.css';
3+
</script>
4+
5+
<div class={s.error}>
6+
hello
7+
</div>
8+
9+
<div class={s.success}>
10+
world
11+
</div>

tests/class/Output.svelte

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script>
2+
const s = {
3+
error: 's-Lwza_error',
4+
success: 's-Lwza_success',
5+
};
6+
</script>
7+
8+
<div class={s.error}>
9+
hello
10+
</div>
11+
12+
<div class={s.success}>
13+
world
14+
</div>
15+
16+
<style>
17+
18+
/* /Users/ryoppippi/ghq/github.com/ryoppippi/svelte-preprocess-css-modules/tests/assets/class.module.css */
19+
.s-Lwza_error {
20+
color: red;
21+
}
22+
23+
.s-Lwza_success {
24+
color: green;
25+
}
26+
27+
</style>

tests/class/class.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { expect, it } from 'vitest';
4+
import { compiler } from '../compiler';
5+
6+
export function resolve(file: string) {
7+
return path.resolve(__dirname, file);
8+
}
9+
10+
it('load css module for class', async () => {
11+
const filename = resolve('Input.svelte');
12+
13+
const source = await fs.readFile(filename, 'utf-8');
14+
15+
const code = await compiler({
16+
source,
17+
preprocessOptions: { filename },
18+
});
19+
20+
await expect(code).toMatchFileSnapshot('./Output.svelte');
21+
});

0 commit comments

Comments
 (0)