Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-islands-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix: properly add tailwind plugins on subsequent add-on executions
48 changes: 24 additions & 24 deletions packages/addons/tailwindcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
import { addAtRule, addImports } from '@sveltejs/cli-core/css';
import { array, functions, imports, object, exports } from '@sveltejs/cli-core/js';
import { parseCss, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
import { addSlot } from '@sveltejs/cli-core/html';
Expand Down Expand Up @@ -74,37 +73,38 @@ export default defineAddon({
});

sv.file('src/app.css', (content) => {
if (content.includes('tailwindcss')) {
return content;
let atRules = parseCss(content).ast.nodes.filter((node) => node.type === 'atrule');

const findAtRule = (name: string, params: string) =>
atRules.find(
(rule) =>
rule.name === name &&
// checks for both double and single quote variants
rule.params.replace(/['"]/g, '') === params
);

let code = content;
const importsTailwind = findAtRule('import', 'tailwindcss');
if (!importsTailwind) {
code = "@import 'tailwindcss';\n" + code;
// reparse to account for the newly added tailwindcss import
atRules = parseCss(code).ast.nodes.filter((node) => node.type === 'atrule');
}

const { ast, generateCode } = parseCss(content);
const originalFirst = ast.first;

const nodes = addImports(ast, ["'tailwindcss'"]);
const lastAtRule = atRules.findLast((rule) => ['plugin', 'import'].includes(rule.name));
const pluginPos = lastAtRule!.source!.end!.offset;

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

addAtRule(ast, 'plugin', `'${plugin.package}'`, true);
}

if (
originalFirst !== ast.first &&
originalFirst?.type === 'atrule' &&
originalFirst.name === 'import'
) {
originalFirst.raws.before = '\n';
const pluginRule = findAtRule('plugin', plugin.package);
if (!pluginRule) {
const pluginImport = `\n@plugin '${plugin.package}';`;
code = code.substring(0, pluginPos) + pluginImport + code.substring(pluginPos);
}
}

// We remove the first node to avoid adding a newline at the top of the stylesheet
nodes.shift();

// Each node is prefixed with single newline, ensuring the imports will always be single spaced.
// Without this, the CSS printer will vary the spacing depending on the current state of the stylesheet
nodes.forEach((n) => (n.raws.before = '\n'));

return generateCode();
return code;
});

if (!kit) {
Expand Down
Loading