|
1 |
| -import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core'; |
| 1 | +import { defineAddon } from '@sveltejs/cli-core'; |
2 | 2 | import { addImports } from '@sveltejs/cli-core/css';
|
3 |
| -import { array, common, exports, imports, object } from '@sveltejs/cli-core/js'; |
4 |
| -import { parseCss, parseScript, parseJson, parseSvelte } from '@sveltejs/cli-core/parsers'; |
| 3 | +import { array, functions, imports, object, exports } from '@sveltejs/cli-core/js'; |
| 4 | +import { parseCss, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers'; |
5 | 5 | import { addSlot } from '@sveltejs/cli-core/html';
|
6 | 6 |
|
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.16', |
19 |
| - identifier: 'typography' |
20 |
| - }, |
21 |
| - { |
22 |
| - id: 'forms', |
23 |
| - package: '@tailwindcss/forms', |
24 |
| - version: '^0.5.10', |
25 |
| - identifier: 'forms' |
26 |
| - }, |
27 |
| - { |
28 |
| - id: 'container-queries', |
29 |
| - package: '@tailwindcss/container-queries', |
30 |
| - version: '^0.1.1', |
31 |
| - identifier: 'containerQueries' |
32 |
| - } |
33 |
| -]; |
34 |
| - |
35 |
| -const options = defineAddonOptions({ |
36 |
| - plugins: { |
37 |
| - type: 'multiselect', |
38 |
| - question: 'Which plugins would you like to add?', |
39 |
| - options: plugins.map((p) => ({ value: p.id, label: p.id, hint: p.package })), |
40 |
| - default: [] |
41 |
| - } |
42 |
| -}); |
43 |
| - |
44 | 7 | export default defineAddon({
|
45 | 8 | id: 'tailwindcss',
|
46 | 9 | alias: 'tailwind',
|
47 | 10 | shortDescription: 'css framework',
|
48 | 11 | homepage: 'https://tailwindcss.com',
|
49 |
| - options, |
50 |
| - run: ({ sv, options, typescript, kit, dependencyVersion }) => { |
| 12 | + options: {}, |
| 13 | + run: ({ sv, typescript, kit, dependencyVersion }) => { |
51 | 14 | const ext = typescript ? 'ts' : 'js';
|
52 | 15 | const prettierInstalled = Boolean(dependencyVersion('prettier'));
|
53 | 16 |
|
54 |
| - sv.devDependency('tailwindcss', '^3.4.17'); |
55 |
| - sv.devDependency('autoprefixer', '^10.4.20'); |
56 |
| - |
57 |
| - if (prettierInstalled) sv.devDependency('prettier-plugin-tailwindcss', '^0.6.10'); |
| 17 | + sv.devDependency('tailwindcss', '^4.0.0'); |
| 18 | + sv.devDependency('@tailwindcss/vite', '^4.0.0'); |
58 | 19 |
|
59 |
| - for (const plugin of plugins) { |
60 |
| - if (!options.plugins.includes(plugin.id)) continue; |
| 20 | + if (prettierInstalled) sv.devDependency('prettier-plugin-tailwindcss', '^0.6.11'); |
61 | 21 |
|
62 |
| - sv.devDependency(plugin.package, plugin.version); |
63 |
| - } |
64 |
| - |
65 |
| - sv.file(`tailwind.config.${ext}`, (content) => { |
| 22 | + // add the vite plugin |
| 23 | + sv.file(`vite.config.${ext}`, (content) => { |
66 | 24 | const { ast, generateCode } = parseScript(content);
|
67 |
| - let root; |
68 |
| - const rootExport = object.createEmpty(); |
69 |
| - if (typescript) { |
70 |
| - imports.addNamed(ast, 'tailwindcss', { Config: 'Config' }, true); |
71 |
| - root = common.satisfiesExpression(rootExport, 'Config'); |
72 |
| - } |
73 |
| - |
74 |
| - const { astNode: exportDeclaration, value: node } = exports.defaultExport( |
75 |
| - ast, |
76 |
| - root ?? rootExport |
77 |
| - ); |
78 |
| - |
79 |
| - const config = node.type === 'TSSatisfiesExpression' ? node.expression : node; |
80 |
| - if (config.type !== 'ObjectExpression') { |
81 |
| - throw new Error(`Unexpected tailwind config shape: ${config.type}`); |
82 |
| - } |
83 |
| - |
84 |
| - if (!typescript) { |
85 |
| - common.addJsDocTypeComment(exportDeclaration, "import('tailwindcss').Config"); |
86 |
| - } |
87 |
| - |
88 |
| - const contentArray = object.property(config, 'content', array.createEmpty()); |
89 |
| - array.push(contentArray, './src/**/*.{html,js,svelte,ts}'); |
90 |
| - |
91 |
| - const themeObject = object.property(config, 'theme', object.createEmpty()); |
92 |
| - object.property(themeObject, 'extend', object.createEmpty()); |
93 | 25 |
|
94 |
| - const pluginsArray = object.property(config, 'plugins', array.createEmpty()); |
| 26 | + const vitePluginName = 'tailwindcss'; |
| 27 | + imports.addDefault(ast, '@tailwindcss/vite', vitePluginName); |
95 | 28 |
|
96 |
| - for (const plugin of plugins) { |
97 |
| - if (!options.plugins.includes(plugin.id)) continue; |
98 |
| - imports.addDefault(ast, plugin.package, plugin.identifier); |
99 |
| - array.push(pluginsArray, { type: 'Identifier', name: plugin.identifier }); |
100 |
| - } |
| 29 | + const { value: rootObject } = exports.defaultExport(ast, functions.call('defineConfig', [])); |
| 30 | + const param1 = functions.argumentByIndex(rootObject, 0, object.createEmpty()); |
101 | 31 |
|
102 |
| - return generateCode(); |
103 |
| - }); |
104 |
| - |
105 |
| - sv.file('postcss.config.js', (content) => { |
106 |
| - const { ast, generateCode } = parseScript(content); |
107 |
| - const { value: rootObject } = exports.defaultExport(ast, object.createEmpty()); |
108 |
| - const pluginsObject = object.property(rootObject, 'plugins', object.createEmpty()); |
| 32 | + const pluginsArray = object.property(param1, 'plugins', array.createEmpty()); |
| 33 | + const pluginFunctionCall = functions.call(vitePluginName, []); |
| 34 | + array.push(pluginsArray, pluginFunctionCall); |
109 | 35 |
|
110 |
| - object.property(pluginsObject, 'tailwindcss', object.createEmpty()); |
111 |
| - object.property(pluginsObject, 'autoprefixer', object.createEmpty()); |
112 | 36 | return generateCode();
|
113 | 37 | });
|
114 | 38 |
|
115 | 39 | sv.file('src/app.css', (content) => {
|
116 |
| - const layerImports = ['base', 'components', 'utilities'].map( |
117 |
| - (layer) => `tailwindcss/${layer}` |
118 |
| - ); |
119 |
| - if (layerImports.every((i) => content.includes(i))) { |
| 40 | + if (content.includes('tailwindcss')) { |
120 | 41 | return content;
|
121 | 42 | }
|
122 | 43 |
|
123 | 44 | const { ast, generateCode } = parseCss(content);
|
124 | 45 | const originalFirst = ast.first;
|
125 | 46 |
|
126 |
| - const specifiers = layerImports.map((i) => `'${i}'`); |
127 |
| - const nodes = addImports(ast, specifiers); |
| 47 | + const nodes = addImports(ast, ["'tailwindcss'"]); |
128 | 48 |
|
129 | 49 | if (
|
130 | 50 | originalFirst !== ast.first &&
|
|
0 commit comments