Skip to content

Commit 5a78da1

Browse files
committed
make tailwind work again
1 parent aecd8b1 commit 5a78da1

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

packages/sv/lib/addons/tailwindcss/index.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineAddon, defineAddonOptions } from '../../core/index.ts';
22
import { imports, vite } from '../../core/tooling/js/index.ts';
33
import * as svelte from '../../core/tooling/svelte/index.ts';
4+
import * as css from '../../core/tooling/css/index.ts';
45
import { parseCss, parseJson, parseScript, parseSvelte } from '../../core/tooling/parsers.ts';
56

67
const plugins = [
@@ -59,38 +60,28 @@ export default defineAddon({
5960
});
6061

6162
sv.file(files.stylesheet, (content) => {
62-
let atRules = parseCss(content).ast.nodes.filter((node) => node.type === 'atrule');
63-
64-
const findAtRule = (name: string, params: string) =>
65-
atRules.find(
66-
(rule) =>
67-
rule.name === name &&
68-
// checks for both double and single quote variants
69-
rule.params.replace(/['"]/g, '') === params
70-
);
71-
72-
let code = content;
73-
const importsTailwind = findAtRule('import', 'tailwindcss');
74-
if (!importsTailwind) {
75-
code = "@import 'tailwindcss';\n" + code;
76-
// reparse to account for the newly added tailwindcss import
77-
atRules = parseCss(code).ast.nodes.filter((node) => node.type === 'atrule');
78-
}
63+
const { ast, generateCode } = parseCss(content);
7964

80-
const lastAtRule = atRules.findLast((rule) => ['plugin', 'import'].includes(rule.name));
81-
const pluginPos = lastAtRule!.source!.end!.offset;
65+
// since we are prepending all the `AtRule` let's add them in reverse order,
66+
// so they appear in the expected order in the final file
8267

8368
for (const plugin of plugins) {
8469
if (!options.plugins.includes(plugin.id)) continue;
8570

86-
const pluginRule = findAtRule('plugin', plugin.package);
87-
if (!pluginRule) {
88-
const pluginImport = `\n@plugin '${plugin.package}';`;
89-
code = code.substring(0, pluginPos) + pluginImport + code.substring(pluginPos);
90-
}
71+
css.addAtRule(ast, {
72+
name: 'plugin',
73+
params: `'${plugin.package}'`,
74+
append: false
75+
});
9176
}
9277

93-
return code;
78+
css.addAtRule(ast, {
79+
name: 'import',
80+
params: `'tailwindcss'`,
81+
append: false
82+
});
83+
84+
return generateCode();
9485
});
9586

9687
if (!kit) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ export function serializeCss(ast: SvelteAst.CSS.StyleSheet): string {
111111
const child = ast.children[i];
112112
result += sveltePrint(child).code;
113113

114-
if (i < ast.children.length - 1) result += '\n\n';
114+
if (i < ast.children.length - 1) {
115+
const next = ast.children[i + 1];
116+
117+
if (child.type === 'Atrule' && next.type === 'Atrule') result += '\n';
118+
else result += '\n\n';
119+
}
115120
}
116121

117122
return result;

0 commit comments

Comments
 (0)