Skip to content

fix: re-add tailwindcss plugins #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
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/two-crabs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix: re-add `tailwindcss` plugins
35 changes: 16 additions & 19 deletions packages/addons/_tests/tailwindcss/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ test.concurrent.for(variants)('none - %s', async (variant, { page, ...ctx }) =>
await expect(el).toHaveCSS('margin-top', '4px');
});

test.concurrent.for(variants)(
'typography without plugin - %s',
async (variant, { page, ...ctx }) => {
const cwd = await ctx.run(variant, { tailwindcss });

// ...add files
addFixture(cwd, variant);

const { close } = await prepareServer({ cwd, page });
// kill server process when we're done
ctx.onTestFinished(async () => await close());

const el = page.getByTestId('typography');
await expect(el).toHaveCSS('font-size', '18px');
await expect(el).toHaveCSS('line-height', '28px');
await expect(el).toHaveCSS('text-align', 'right');
await expect(el).toHaveCSS('text-decoration-line', 'line-through');
}
);
test.concurrent.for(variants)('typography - %s', async (variant, { page, ...ctx }) => {
const cwd = await ctx.run(variant, { tailwindcss: { plugins: ['typography'] } });

// ...add files
addFixture(cwd, variant);

const { close } = await prepareServer({ cwd, page });
// kill server process when we're done
ctx.onTestFinished(async () => await close());

const el = page.getByTestId('typography');
await expect(el).toHaveCSS('font-size', '18px');
await expect(el).toHaveCSS('line-height', '28px');
await expect(el).toHaveCSS('text-align', 'right');
await expect(el).toHaveCSS('text-decoration-line', 'line-through');
});
51 changes: 47 additions & 4 deletions packages/addons/tailwindcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import { defineAddon } from '@sveltejs/cli-core';
import { addImports } from '@sveltejs/cli-core/css';
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';

type Plugin = {
id: string;
package: string;
version: string;
identifier: string;
};

const plugins: Plugin[] = [
{
id: 'typography',
package: '@tailwindcss/typography',
version: '^0.5.15',
identifier: 'typography'
},
{
id: 'forms',
package: '@tailwindcss/forms',
version: '^0.5.9',
identifier: 'forms'
}
];

const options = defineAddonOptions({
plugins: {
type: 'multiselect',
question: 'Which plugins would you like to add?',
options: plugins.map((p) => ({ value: p.id, label: p.id, hint: p.package })),
default: []
}
});

export default defineAddon({
id: 'tailwindcss',
alias: 'tailwind',
shortDescription: 'css framework',
homepage: 'https://tailwindcss.com',
options: {},
run: ({ sv, typescript, kit, dependencyVersion }) => {
options,
run: ({ sv, options, typescript, kit, dependencyVersion }) => {
const ext = typescript ? 'ts' : 'js';
const prettierInstalled = Boolean(dependencyVersion('prettier'));

Expand All @@ -19,6 +50,12 @@ export default defineAddon({

if (prettierInstalled) sv.devDependency('prettier-plugin-tailwindcss', '^0.6.11');

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

sv.devDependency(plugin.package, plugin.version);
}

// add the vite plugin
sv.file(`vite.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);
Expand Down Expand Up @@ -46,6 +83,12 @@ export default defineAddon({

const nodes = addImports(ast, ["'tailwindcss'"]);

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' &&
Expand Down