Skip to content

Commit 86ec12c

Browse files
authored
Merge pull request #4 from ryoppippi/feature/select-classes-by-query
feature/select classes by query
2 parents 697be50 + db46d68 commit 86ec12c

File tree

7 files changed

+134
-4
lines changed

7 files changed

+134
-4
lines changed

__snapshots__/mod_test.ts.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@ div {
2222
</style>
2323
\`
2424
`;
25+
26+
snapshot[`example svelte partial 1`] = `
27+
\`<script>
28+
let name = 'world';
29+
</script>
30+
31+
<div class="m2">This is a paragraph</div>
32+
<p id="message">This is a paragraph</p>
33+
34+
<style>
35+
36+
div {
37+
color: red;
38+
background: blue;
39+
}
40+
41+
42+
43+
p {
44+
color: blue;
45+
}
46+
47+
.m2 {
48+
background: yellow;
49+
}
50+
51+
52+
53+
#message {
54+
text-decoration: underline;
55+
}
56+
</style>
57+
\`
58+
`;

deno.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"@std/assert": "jsr:@std/assert@^0.226.0",
2121
"@std/testing": "jsr:@std/testing@^0.225.3",
2222
"@sveltejs/kit": "npm:@sveltejs/kit@^2.5.19",
23+
"css-selector-extract": "npm:css-selector-extract@^4.0.1",
2324
"magic-string": "npm:magic-string@^0.30.11",
2425
"pathe": "npm:pathe@^1.1.2",
26+
"query-string": "npm:query-string@^9.1.0",
2527
"svelte": "npm:svelte@^4.2.18",
2628
"unconfig": "npm:unconfig@^0.5.5",
2729
"vite": "npm:vite@^5.3.5"

deno.lock

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

mod.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import process from "node:process";
22
import * as path from "pathe";
33
import * as fs from "node:fs/promises";
44
import MagicString, { Bundle } from "magic-string";
5+
import queryString from "query-string";
6+
import cssSelectorExtract from "css-selector-extract";
57
import type { PreprocessorGroup } from "svelte/compiler";
68
import type { Config as SvelteKitConfig } from "@sveltejs/kit";
79
import type { UserConfig as ViteConfig } from "vite";
@@ -72,10 +74,15 @@ function matchAllImports(str: string) {
7274
while ((match = globalRegex.exec(str)) !== null) {
7375
const start = match.index;
7476
const end = start + match[0].length;
77+
const {
78+
url: file,
79+
query,
80+
} = queryString.parseUrl(match[1].substring(1, match[1].length - 1));
7581
matches.push({
7682
start,
7783
end,
78-
file: match[1].substring(1, match[1].length - 1),
84+
file,
85+
query,
7986
});
8087
}
8188
return matches;
@@ -96,7 +103,7 @@ export function importCSSPreprocess(): PreprocessorGroup {
96103
state.clone().remove(start, end);
97104
const out = [];
98105
const deps = [];
99-
for (const { start, end, file } of imports.reverse()) {
106+
for (const { start, end, file, query } of imports.reverse()) {
100107
// Right
101108
if (lastStart != null) {
102109
out.push(remove(lastStart, content.length).remove(0, end));
@@ -105,8 +112,22 @@ export function importCSSPreprocess(): PreprocessorGroup {
105112
}
106113
const absPath = await getAbsPath({ filename, file });
107114
deps.push(absPath);
108-
const text = (await fs.readFile(absPath)).toString();
109-
out.push(new MagicString(text, { filename: absPath }));
115+
116+
const cssText = (await fs.readFile(absPath)).toString();
117+
118+
let replaceText: string | undefined = undefined;
119+
120+
if (Object.keys(query).length > 0) {
121+
const selectedCss: string = await cssSelectorExtract.process({
122+
css: cssText,
123+
filters: Object.keys(query),
124+
});
125+
replaceText = selectedCss;
126+
} else {
127+
replaceText = cssText;
128+
}
129+
130+
out.push(new MagicString(replaceText, { filename: absPath }));
110131
lastStart = start;
111132
}
112133

mod_test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ Deno.test(
2020
await assertSnapshot(t, code);
2121
},
2222
);
23+
Deno.test(
24+
"example svelte partial",
25+
async function (t) {
26+
const filename = join(
27+
import.meta.dirname as string,
28+
"./test_project/Partial.svelte",
29+
);
30+
const source = Deno.readTextFileSync(filename);
31+
const { code } = await preprocess(
32+
source,
33+
[importCSSPreprocess()],
34+
{ filename },
35+
);
36+
37+
await assertSnapshot(t, code);
38+
},
39+
);

test_project/Partial.svelte

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let name = 'world';
3+
</script>
4+
5+
<div class="m2">This is a paragraph</div>
6+
<p id="message">This is a paragraph</p>
7+
8+
<style>
9+
@import './main.css#div' scoped;
10+
@import './partial.css?p&.m2' scoped;
11+
12+
#message {
13+
text-decoration: underline;
14+
}
15+
</style>

test_project/partial.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
p {
2+
color: blue;
3+
}
4+
5+
h1 {
6+
color: green;
7+
}
8+
9+
.m2 {
10+
background: yellow;
11+
}

0 commit comments

Comments
 (0)