Skip to content
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

feat: add tailwindcss 4.0 #422

Merged
merged 12 commits into from
Feb 7, 2025
5 changes: 5 additions & 0 deletions .changeset/empty-geese-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/addons': patch
---

using tailwindcss 4.0.0
jycouet marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/addons/_tests/tailwindcss/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.concurrent.for(variants)('none - %s', async (variant, { page, ...ctx }) =>
});

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

// ...add files
addFixture(cwd, variant);
Expand Down
114 changes: 9 additions & 105 deletions packages/addons/tailwindcss/index.ts
Original file line number Diff line number Diff line change
@@ -1,121 +1,25 @@
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
import { defineAddon } from '@sveltejs/cli-core';
import { addImports } from '@sveltejs/cli-core/css';
import { array, common, exports, imports, object } from '@sveltejs/cli-core/js';
import { parseCss, parseScript, parseJson, parseSvelte } from '@sveltejs/cli-core/parsers';
import { imports } from '@sveltejs/cli-core/js';
import { parseCss, parseJson, 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.16',
identifier: 'typography'
},
{
id: 'forms',
package: '@tailwindcss/forms',
version: '^0.5.10',
identifier: 'forms'
},
{
id: 'container-queries',
package: '@tailwindcss/container-queries',
version: '^0.1.1',
identifier: 'containerQueries'
}
];

manuel3108 marked this conversation as resolved.
Show resolved Hide resolved
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, options, typescript, kit, dependencyVersion }) => {
const ext = typescript ? 'ts' : 'js';
options: {},
run: ({ sv, typescript, kit, dependencyVersion }) => {
const prettierInstalled = Boolean(dependencyVersion('prettier'));

sv.devDependency('tailwindcss', '^3.4.17');
sv.devDependency('autoprefixer', '^10.4.20');

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

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

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

sv.file(`tailwind.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);
let root;
const rootExport = object.createEmpty();
if (typescript) {
imports.addNamed(ast, 'tailwindcss', { Config: 'Config' }, true);
root = common.satisfiesExpression(rootExport, 'Config');
}

const { astNode: exportDeclaration, value: node } = exports.defaultExport(
ast,
root ?? rootExport
);

const config = node.type === 'TSSatisfiesExpression' ? node.expression : node;
if (config.type !== 'ObjectExpression') {
throw new Error(`Unexpected tailwind config shape: ${config.type}`);
}

if (!typescript) {
common.addJsDocTypeComment(exportDeclaration, "import('tailwindcss').Config");
}

const contentArray = object.property(config, 'content', array.createEmpty());
array.push(contentArray, './src/**/*.{html,js,svelte,ts}');

const themeObject = object.property(config, 'theme', object.createEmpty());
object.property(themeObject, 'extend', object.createEmpty());

const pluginsArray = object.property(config, 'plugins', array.createEmpty());

for (const plugin of plugins) {
if (!options.plugins.includes(plugin.id)) continue;
imports.addDefault(ast, plugin.package, plugin.identifier);
array.push(pluginsArray, { type: 'Identifier', name: plugin.identifier });
}
sv.devDependency('@tailwindcss/vite', '^4.0.0');
sv.devDependency('tailwindcss', '^4.0.0');
jycouet marked this conversation as resolved.
Show resolved Hide resolved

return generateCode();
});

sv.file('postcss.config.js', (content) => {
const { ast, generateCode } = parseScript(content);
const { value: rootObject } = exports.defaultExport(ast, object.createEmpty());
const pluginsObject = object.property(rootObject, 'plugins', object.createEmpty());

object.property(pluginsObject, 'tailwindcss', object.createEmpty());
object.property(pluginsObject, 'autoprefixer', object.createEmpty());
return generateCode();
});
if (prettierInstalled) sv.devDependency('prettier-plugin-tailwindcss', '^0.6.11');

sv.file('src/app.css', (content) => {
const layerImports = ['base', 'components', 'utilities'].map(
(layer) => `tailwindcss/${layer}`
);
const layerImports = ['tailwindcss'];
if (layerImports.every((i) => content.includes(i))) {
jycouet marked this conversation as resolved.
Show resolved Hide resolved
return content;
}
Expand Down
Loading