Skip to content

Commit 8ed4274

Browse files
committed
feat: select classes by query string
1 parent 697be50 commit 8ed4274

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

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

0 commit comments

Comments
 (0)