1
1
import { walk } from "estree-walker" ;
2
2
import { AtRule } from "postcss" ;
3
- import { newTypeScriptEstreeAst } from "../../ast-io.js" ;
4
- import { addImport , findImport , getConfigExpression , setDefault } from "../../ast-tools.js" ;
3
+ import { addImport , findImport , getConfigExpression , setDefault , setPropertyValue } from "../../ast-tools.js" ;
5
4
import { extension , postcssConfigCjsPath , stylesHint } from "../postcss/stuff.js" ;
6
5
import { tailwindConfigCjsPath } from "./stuff.js" ;
7
6
8
- const tailwindAotConfig = `const { tailwindExtractor } = require("tailwindcss/lib/lib/purgeUnusedStyles");
9
-
10
- const config = {
11
- mode: "aot",
12
- purge: {
13
- content: [
14
- "./src/**/*.{html,js,svelte,ts}",
15
- ],
16
- options: {
17
- defaultExtractor: (content) => [
18
- // If this stops working, please open an issue at https://github.com/svelte-add/svelte-add/issues rather than bothering Tailwind Labs about it
19
- ...tailwindExtractor(content),
20
- // Match Svelte class: directives (https://github.com/tailwindlabs/tailwindcss/discussions/1731)
21
- ...[...content.matchAll(/(?:class:)*([\\w\\d-/:%.]+)/gm)].map(([_match, group, ..._rest]) => group),
22
- ],
23
- },
24
- safelist: [/^svelte-[\\d\\w]+$/],
25
- },
26
- theme: {
27
- extend: {},
28
- },
29
- variants: {
30
- extend: {},
31
- },
32
- plugins: [],
33
- };
34
-
35
- module.exports = config;
36
- ` ;
37
-
38
- const tailwindJitConfig = `const config = {
39
- mode: "jit",
40
- purge: [
41
- "./src/**/*.{html,js,svelte,ts}",
42
- ],
43
- theme: {
44
- extend: {},
45
- },
46
- plugins: [],
47
- };
48
-
49
- module.exports = config;
50
- ` ;
51
-
52
7
/**
53
8
* @param {import("../../ast-io.js").RecastAST } postcssConfigAst
9
+ * @returns {import("../../ast-io.js").RecastAST }
54
10
*/
55
11
const updatePostcssConfig = ( postcssConfigAst ) => {
56
12
const configObject = getConfigExpression ( {
57
13
cjs : true ,
58
14
typeScriptEstree : postcssConfigAst ,
59
15
} ) ;
60
16
61
- if ( configObject . type !== "ObjectExpression" ) throw new Error ( "postcss config must be an object" ) ;
17
+ if ( configObject . type !== "ObjectExpression" ) throw new Error ( "PostCSS config must be an object" ) ;
62
18
63
19
const pluginsList = setDefault ( {
64
20
default : {
@@ -173,6 +129,83 @@ const updatePostcssConfig = (postcssConfigAst) => {
173
129
return postcssConfigAst ;
174
130
} ;
175
131
132
+ /**
133
+ * @param {import("../../ast-io.js").RecastAST } tailwindConfigAst
134
+ * @param {boolean } tailwind3
135
+ * @returns {import("../../ast-io.js").RecastAST }
136
+ */
137
+ const updateTailwindConfig = ( tailwindConfigAst , tailwind3 ) => {
138
+ const configObject = getConfigExpression ( {
139
+ cjs : true ,
140
+ typeScriptEstree : tailwindConfigAst ,
141
+ } ) ;
142
+
143
+ if ( configObject . type !== "ObjectExpression" ) throw new Error ( "Tailwind config must be an object" ) ;
144
+
145
+ if ( ! tailwind3 ) {
146
+ setPropertyValue ( {
147
+ object : configObject ,
148
+ property : "mode" ,
149
+ value : {
150
+ type : "Literal" ,
151
+ value : "jit" ,
152
+ } ,
153
+ } ) ;
154
+ }
155
+
156
+ /** @type {import("estree").ArrayExpression } */
157
+ const content = {
158
+ type : "ArrayExpression" ,
159
+ elements : [
160
+ {
161
+ type : "Literal" ,
162
+ value : "./src/**/*.{html,js,svelte,ts}" ,
163
+ } ,
164
+ ] ,
165
+ } ;
166
+ setDefault ( {
167
+ default : content ,
168
+ object : configObject ,
169
+ property : tailwind3 ? "content" : "purge" ,
170
+ } ) ;
171
+
172
+ /** @type {import("estree").ObjectExpression } */
173
+ const emptyTheme = {
174
+ type : "ObjectExpression" ,
175
+ properties : [ ] ,
176
+ } ;
177
+ const themeConfig = setDefault ( {
178
+ default : emptyTheme ,
179
+ object : configObject ,
180
+ property : "theme" ,
181
+ } ) ;
182
+ if ( themeConfig . type !== "ObjectExpression" ) throw new Error ( "`theme` in Tailwind config must be an object" ) ;
183
+
184
+ /** @type {import("estree").ObjectExpression } */
185
+ const emptyThemeExtend = {
186
+ type : "ObjectExpression" ,
187
+ properties : [ ] ,
188
+ } ;
189
+ setDefault ( {
190
+ default : emptyThemeExtend ,
191
+ object : themeConfig ,
192
+ property : "extend" ,
193
+ } ) ;
194
+
195
+ /** @type {import("estree").ArrayExpression } */
196
+ const emptyPlugins = {
197
+ type : "ArrayExpression" ,
198
+ elements : [ ] ,
199
+ } ;
200
+ setDefault ( {
201
+ default : emptyPlugins ,
202
+ object : configObject ,
203
+ property : "plugins" ,
204
+ } ) ;
205
+
206
+ return tailwindConfigAst ;
207
+ } ;
208
+
176
209
/**
177
210
*
178
211
* @param {import("../../ast-io.js").PostCSSAst } postcss
@@ -219,9 +252,9 @@ const updateGlobalStylesheet = (postcss) => {
219
252
export const run = async ( { install, options, updateCss, updateJavaScript } ) => {
220
253
await updateJavaScript ( {
221
254
path : tailwindConfigCjsPath ,
222
- async script ( ) {
255
+ async script ( { typeScriptEstree } ) {
223
256
return {
224
- typeScriptEstree : newTypeScriptEstreeAst ( options . jit ? tailwindJitConfig : tailwindAotConfig ) ,
257
+ typeScriptEstree : updateTailwindConfig ( typeScriptEstree , options . v3 ) ,
225
258
} ;
226
259
} ,
227
260
} ) ;
@@ -244,5 +277,5 @@ export const run = async ({ install, options, updateCss, updateJavaScript }) =>
244
277
} ,
245
278
} ) ;
246
279
247
- await install ( { package : "tailwindcss" } ) ;
280
+ await install ( { package : "tailwindcss" , versionOverride : options . v3 ? "next" : undefined } ) ;
248
281
} ;
0 commit comments