Skip to content

Commit 4bd924c

Browse files
authored
fix: re-add tailwindcss plugins (#448)
* fix: re-add `tailwindcss` plugins * fix tests
1 parent 2b4d63d commit 4bd924c

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

.changeset/two-crabs-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: re-add `tailwindcss` plugins

packages/addons/_tests/tailwindcss/test.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ test.concurrent.for(variants)('none - %s', async (variant, { page, ...ctx }) =>
2222
await expect(el).toHaveCSS('margin-top', '4px');
2323
});
2424

25-
test.concurrent.for(variants)(
26-
'typography without plugin - %s',
27-
async (variant, { page, ...ctx }) => {
28-
const cwd = await ctx.run(variant, { tailwindcss });
29-
30-
// ...add files
31-
addFixture(cwd, variant);
32-
33-
const { close } = await prepareServer({ cwd, page });
34-
// kill server process when we're done
35-
ctx.onTestFinished(async () => await close());
36-
37-
const el = page.getByTestId('typography');
38-
await expect(el).toHaveCSS('font-size', '18px');
39-
await expect(el).toHaveCSS('line-height', '28px');
40-
await expect(el).toHaveCSS('text-align', 'right');
41-
await expect(el).toHaveCSS('text-decoration-line', 'line-through');
42-
}
43-
);
25+
test.concurrent.for(variants)('typography - %s', async (variant, { page, ...ctx }) => {
26+
const cwd = await ctx.run(variant, { tailwindcss: { plugins: ['typography'] } });
27+
28+
// ...add files
29+
addFixture(cwd, variant);
30+
31+
const { close } = await prepareServer({ cwd, page });
32+
// kill server process when we're done
33+
ctx.onTestFinished(async () => await close());
34+
35+
const el = page.getByTestId('typography');
36+
await expect(el).toHaveCSS('font-size', '18px');
37+
await expect(el).toHaveCSS('line-height', '28px');
38+
await expect(el).toHaveCSS('text-align', 'right');
39+
await expect(el).toHaveCSS('text-decoration-line', 'line-through');
40+
});

packages/addons/tailwindcss/index.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1-
import { defineAddon } from '@sveltejs/cli-core';
2-
import { addImports } from '@sveltejs/cli-core/css';
1+
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
2+
import { addAtRule, addImports } from '@sveltejs/cli-core/css';
33
import { array, functions, imports, object, exports } from '@sveltejs/cli-core/js';
44
import { parseCss, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
55
import { addSlot } from '@sveltejs/cli-core/html';
66

7+
type Plugin = {
8+
id: string;
9+
package: string;
10+
version: string;
11+
identifier: string;
12+
};
13+
14+
const plugins: Plugin[] = [
15+
{
16+
id: 'typography',
17+
package: '@tailwindcss/typography',
18+
version: '^0.5.15',
19+
identifier: 'typography'
20+
},
21+
{
22+
id: 'forms',
23+
package: '@tailwindcss/forms',
24+
version: '^0.5.9',
25+
identifier: 'forms'
26+
}
27+
];
28+
29+
const options = defineAddonOptions({
30+
plugins: {
31+
type: 'multiselect',
32+
question: 'Which plugins would you like to add?',
33+
options: plugins.map((p) => ({ value: p.id, label: p.id, hint: p.package })),
34+
default: []
35+
}
36+
});
37+
738
export default defineAddon({
839
id: 'tailwindcss',
940
alias: 'tailwind',
1041
shortDescription: 'css framework',
1142
homepage: 'https://tailwindcss.com',
12-
options: {},
13-
run: ({ sv, typescript, kit, dependencyVersion }) => {
43+
options,
44+
run: ({ sv, options, typescript, kit, dependencyVersion }) => {
1445
const ext = typescript ? 'ts' : 'js';
1546
const prettierInstalled = Boolean(dependencyVersion('prettier'));
1647

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

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

53+
for (const plugin of plugins) {
54+
if (!options.plugins.includes(plugin.id)) continue;
55+
56+
sv.devDependency(plugin.package, plugin.version);
57+
}
58+
2259
// add the vite plugin
2360
sv.file(`vite.config.${ext}`, (content) => {
2461
const { ast, generateCode } = parseScript(content);
@@ -46,6 +83,12 @@ export default defineAddon({
4683

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

86+
for (const plugin of plugins) {
87+
if (!options.plugins.includes(plugin.id)) continue;
88+
89+
addAtRule(ast, 'plugin', `'${plugin.package}'`, true);
90+
}
91+
4992
if (
5093
originalFirst !== ast.first &&
5194
originalFirst?.type === 'atrule' &&

0 commit comments

Comments
 (0)