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

Commit ad7ac27

Browse files
committed
feat: handle empty CSS in Svelte components
This commit introduces a new feature to handle cases where the CSS in a Svelte component is empty. If the CSS is empty, a style tag is now added to the component. This change is reflected in the src/index.ts file where the new feature is implemented. Additionally, the svelte-parse-markup package has been added to the project to assist with this feature, as reflected in the package.json and pnpm-lock.yaml files.
1 parent 2277a42 commit ad7ac27

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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';
45
import { genObjectFromValues } from 'knitwork';
56
import { loadAliases } from './utils/alias';
@@ -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);

0 commit comments

Comments
 (0)