Skip to content

Commit aecd8b1

Browse files
committed
get rid of postcss (core)
1 parent 0b25394 commit aecd8b1

File tree

14 files changed

+141
-90
lines changed

14 files changed

+141
-90
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@tailwind 'lib/path/file.ext';
2+
23
.foo {
34
color: red;
45
}
6+
57
@tailwind 'lib/path/file1.ext';
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { addAtRule, type CssAst } from '../../../../tooling/css/index.ts';
1+
import { addAtRule } from '../../../../tooling/css/index.ts';
2+
import { type SvelteAst } from '../../../../tooling/index.ts';
23

3-
export function run(ast: CssAst): void {
4+
export function run(ast: SvelteAst.CSS.StyleSheet): void {
45
addAtRule(ast, { name: 'tailwind', params: "'lib/path/file.ext'", append: false });
56
addAtRule(ast, { name: 'tailwind', params: "'lib/path/file1.ext'", append: true });
67
}

packages/sv/lib/core/tests/css/common/add-comment/input.css

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/sv/lib/core/tests/css/common/add-comment/output.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/sv/lib/core/tests/css/common/add-comment/run.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import 'lib/path/file.css';
2+
23
.foo {
34
color: red;
45
}

packages/sv/lib/core/tests/css/common/add-imports/run.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { addImports, type CssAst } from '../../../../tooling/css/index.ts';
1+
import { addImports } from '../../../../tooling/css/index.ts';
2+
import { type SvelteAst } from '../../../../tooling/index.ts';
23

3-
export function run(ast: CssAst): void {
4+
export function run(ast: SvelteAst.CSS.StyleSheet): void {
45
addImports(ast, {
56
imports: ["'lib/path/file.css'"]
67
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.foo {
22
color: red;
33
}
4+
45
.bar {
56
color: blue;
67
}

packages/sv/lib/core/tests/css/common/add-rule/run.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { addDeclaration, addRule, type CssAst } from '../../../../tooling/css/index.ts';
1+
import { addDeclaration, addRule } from '../../../../tooling/css/index.ts';
2+
import { type SvelteAst } from '../../../../tooling/index.ts';
23

3-
export function run(ast: CssAst): void {
4+
export function run(ast: SvelteAst.CSS.StyleSheet): void {
45
const barSelectorRule = addRule(ast, {
5-
selector: '.bar'
6+
selector: 'bar'
67
});
78
addDeclaration(barSelectorRule, {
89
property: 'color',

packages/sv/lib/core/tooling/css/index.ts

Lines changed: 107 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,142 @@
1-
import { Declaration, Rule, AtRule, Comment, type CssAst, type CssChildNode } from '../index.ts';
2-
3-
export type { CssAst };
4-
5-
export function addRule(node: CssAst, options: { selector: string }): Rule {
6-
const rules = node.nodes.filter((x): x is Rule => x.type === 'rule');
7-
let rule = rules.find((x) => x.selector === options.selector);
1+
import type { SvelteAst } from '../index.ts';
2+
3+
export function addRule(
4+
node: SvelteAst.CSS.StyleSheet,
5+
options: { selector: string }
6+
): SvelteAst.CSS.Rule {
7+
// we do not check for existing rules here, as the selector AST from svelte is really complex
8+
const rules = node.children.filter((x) => x.type === 'Rule');
9+
let rule = rules.find((x) => {
10+
const selector = x.prelude.children[0].children[0].selectors[0];
11+
return selector.type === 'ClassSelector' && selector.name === options.selector;
12+
});
813

914
if (!rule) {
10-
rule = new Rule();
11-
rule.selector = options.selector;
12-
node.nodes.push(rule);
15+
rule = {
16+
type: 'Rule',
17+
prelude: {
18+
type: 'SelectorList',
19+
children: [
20+
{
21+
type: 'ComplexSelector',
22+
children: [
23+
{
24+
type: 'RelativeSelector',
25+
selectors: [
26+
{
27+
type: 'ClassSelector',
28+
name: options.selector,
29+
start: 0,
30+
end: 0
31+
}
32+
],
33+
combinator: null,
34+
start: 0,
35+
end: 0
36+
}
37+
],
38+
start: 0,
39+
end: 0
40+
}
41+
],
42+
start: 0,
43+
end: 0
44+
},
45+
block: { type: 'Block', children: [], start: 0, end: 0 },
46+
start: 0,
47+
end: 0
48+
};
49+
node.children.push(rule);
1350
}
1451

1552
return rule;
1653
}
1754

1855
export function addDeclaration(
19-
node: Rule | CssAst,
56+
node: SvelteAst.CSS.Rule,
2057
options: { property: string; value: string }
2158
): void {
22-
const declarations = node.nodes.filter((x): x is Declaration => x.type === 'decl');
23-
let declaration = declarations.find((x) => x.prop === options.property);
59+
const declarations = node.block.children.filter((x) => x.type === 'Declaration');
60+
let declaration = declarations.find((x) => x.property === options.property);
2461

2562
if (!declaration) {
26-
declaration = new Declaration({ prop: options.property, value: options.value });
27-
node.append(declaration);
63+
declaration = {
64+
type: 'Declaration',
65+
property: options.property,
66+
value: options.value,
67+
start: 0,
68+
end: 0
69+
};
70+
node.block.children.push(declaration);
2871
} else {
2972
declaration.value = options.value;
3073
}
3174
}
3275

33-
export function addImports(node: Rule | CssAst, options: { imports: string[] }): CssChildNode[] {
34-
let prev: CssChildNode | undefined;
35-
const nodes = options.imports.map((param) => {
36-
const found = node.nodes.find(
37-
(x) => x.type === 'atrule' && x.name === 'import' && x.params === param
38-
);
39-
40-
if (found) return (prev = found);
76+
export function addImports(node: SvelteAst.CSS.StyleSheet, options: { imports: string[] }): void {
77+
let lastImportIndex = -1;
4178

42-
const rule = new AtRule({ name: 'import', params: param });
43-
if (prev) node.insertAfter(prev, rule);
44-
else node.prepend(rule);
79+
// Find the last existing @import to insert after it
80+
for (let i = 0; i < node.children.length; i++) {
81+
const child = node.children[i];
82+
if (child.type === 'Atrule' && child.name === 'import') {
83+
lastImportIndex = i;
84+
}
85+
}
4586

46-
return (prev = rule);
47-
});
87+
for (const param of options.imports) {
88+
const found = node.children.find(
89+
(x) => x.type === 'Atrule' && x.name === 'import' && x.prelude === param
90+
);
4891

49-
return nodes;
92+
if (found) continue;
93+
94+
const atRule: SvelteAst.CSS.Atrule = {
95+
type: 'Atrule',
96+
name: 'import',
97+
prelude: param,
98+
block: null,
99+
start: 0,
100+
end: 0
101+
};
102+
103+
if (lastImportIndex >= 0) {
104+
// Insert after the last @import
105+
lastImportIndex++;
106+
node.children.splice(lastImportIndex, 0, atRule);
107+
} else {
108+
// No existing imports, prepend at the start
109+
node.children.unshift(atRule);
110+
lastImportIndex = 0;
111+
}
112+
}
50113
}
51114

52115
export function addAtRule(
53-
node: CssAst,
116+
node: SvelteAst.CSS.StyleSheet,
54117
options: { name: string; params: string; append: boolean }
55-
): AtRule {
56-
const atRules = node.nodes.filter((x): x is AtRule => x.type === 'atrule');
57-
let atRule = atRules.find((x) => x.name === options.name && x.params === options.params);
118+
): SvelteAst.CSS.Atrule {
119+
const atRules = node.children.filter((x) => x.type === 'Atrule');
120+
let atRule = atRules.find((x) => x.name === options.name && x.prelude === options.params);
58121

59122
if (atRule) {
60123
return atRule;
61124
}
62125

63-
atRule = new AtRule({ name: options.name, params: options.params });
126+
atRule = {
127+
type: 'Atrule',
128+
name: options.name,
129+
prelude: options.params,
130+
block: null,
131+
start: 0,
132+
end: 0
133+
};
134+
64135
if (!options.append) {
65-
node.prepend(atRule);
136+
node.children.unshift(atRule);
66137
} else {
67-
node.append(atRule);
138+
node.children.push(atRule);
68139
}
69140

70141
return atRule;
71142
}
72-
73-
export function addComment(node: CssAst, options: { value: string }): void {
74-
const comment = new Comment({ text: options.value });
75-
node.append(comment);
76-
}

0 commit comments

Comments
 (0)