This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path__run.js
336 lines (287 loc) · 9.81 KB
/
__run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { walk } from "estree-walker";
import { AtRule } from "postcss";
import { addImport, findImport, setDefaultDefaultExport, setDefault, addRootBlockComment } from "../../ast-tools.js";
import { extension, postcssConfigCjsPath, stylesHint } from "../postcss/stuff.js";
import { tailwindConfigCjsPath, prettierConfigPath } from "./stuff.js";
/**
* @param {import("../../ast-io.js").RecastAST} postcssConfigAst
* @returns {import("../../ast-io.js").RecastAST}
*/
const updatePostcssConfig = (postcssConfigAst) => {
const configObject = setDefaultDefaultExport({
cjs: true,
defaultValue: {
type: "ObjectExpression",
properties: [],
},
typeScriptEstree: postcssConfigAst,
});
if (configObject.type !== "ObjectExpression") throw new Error("PostCSS config must be an object");
const pluginsList = setDefault({
default: {
type: "ArrayExpression",
elements: [],
},
object: configObject,
property: "plugins",
});
if (pluginsList.type === "ArrayExpression") {
const goAfter = ["tailwindcss/nesting", "postcss-nested", "postcss-import"];
let minIndex = 0;
const goBefore = ["autoprefixer", "cssnano"];
let maxIndex = pluginsList.elements.length;
// Find `tailwindcss` import or add it if it's not there
let tailwindcssImportedAs = findImport({ cjs: true, package: "tailwindcss", typeScriptEstree: postcssConfigAst }).require;
if (!tailwindcssImportedAs) {
tailwindcssImportedAs = "tailwindcss";
addImport({ cjs: true, package: "tailwindcss", require: tailwindcssImportedAs, typeScriptEstree: postcssConfigAst });
}
/** @type {Record<string, string>} Identifier name -> imported package */
const imports = {};
walk(postcssConfigAst, {
enter(node) {
if (node.type !== "VariableDeclarator") return;
const declarator = /** @type {import("estree").VariableDeclarator} */ (node);
if (declarator.id.type !== "Identifier") return;
const identifier = declarator.id;
if (!declarator.init) return;
if (declarator.init.type !== "CallExpression") return;
const callExpression = declarator.init;
if (callExpression.callee.type !== "Identifier") return;
if (callExpression.callee.name !== "require") return;
if (callExpression.arguments[0].type !== "Literal") return;
const requireArgValue = callExpression.arguments[0].value;
if (typeof requireArgValue !== "string") return;
imports[identifier.name] = requireArgValue;
},
});
for (const [index, plugin] of pluginsList.elements.entries()) {
if (!plugin) continue;
/** @type {string | undefined} */
let determinedPlugin;
if (plugin.type === "CallExpression") {
if (plugin.callee.type === "Identifier") {
determinedPlugin = imports[plugin.callee.name];
}
} else if (plugin.type === "Identifier") {
determinedPlugin = imports[plugin.name];
}
// TODO: detect conditional plugins (e.x. !dev && cssnano())
if (!determinedPlugin) continue;
if (goAfter.includes(determinedPlugin)) {
if (index > minIndex) minIndex = index;
} else if (goBefore.includes(determinedPlugin)) {
if (index < maxIndex) maxIndex = index;
}
}
if (minIndex > maxIndex) throw new Error(`cannot find place to slot \`${tailwindcssImportedAs}()\` as a plugin in the PostCSS config`);
// We have a range of acceptable values
// Let's use the latest slot because it's probably the most likely to work correctly
pluginsList.elements.splice(maxIndex, 0, {
// @ts-expect-error - Force accept the comment - TODO: find a better way to handle this
type: "Line",
value: `Some plugins, like ${goAfter[0]}, need to run before Tailwind`,
});
pluginsList.elements.splice(
maxIndex + 1,
0,
/** @type {import("estree").CallExpression} */ {
type: "CallExpression",
arguments: [],
callee: {
type: "Identifier",
name: tailwindcssImportedAs,
},
optional: false,
},
);
pluginsList.elements.splice(maxIndex + 2, 0, {
// @ts-expect-error - Force accept the comment
type: "Line",
value: `But others, like ${goBefore[0]}, need to run after`,
});
} else if (pluginsList.type === "ObjectExpression") {
// TODO
throw new Error("`plugins` in PostCSS config must be an array (object support is coming soon)");
} else {
throw new Error("`plugins` in PostCSS config must be an array (object support is coming soon)");
}
return postcssConfigAst;
};
/**
* @param {import("../../ast-io.js").RecastAST} tailwindConfigAst
* @param {boolean} forms
* @param {boolean} typography
* @param {boolean} daisyui
* @returns {import("../../ast-io.js").RecastAST}
*/
const updateTailwindConfig = (tailwindConfigAst, forms, typography, daisyui) => {
const configObject = setDefaultDefaultExport({
cjs: true,
defaultValue: {
type: "ObjectExpression",
properties: [],
},
typeScriptEstree: tailwindConfigAst,
});
if (configObject.type !== "ObjectExpression") throw new Error("Tailwind config must be an object");
const typeComment = "* @type {import('tailwindcss').Config}";
addRootBlockComment({ typeScriptEstree: tailwindConfigAst, value: typeComment });
setDefault({
default: /** @type {import("estree").ArrayExpression} */ ({
type: "ArrayExpression",
elements: [
{
type: "Literal",
value: "./src/**/*.{html,js,svelte,ts}",
},
],
}),
object: configObject,
property: "content",
});
/** @type {import("estree").ObjectExpression} */
const emptyTheme = {
type: "ObjectExpression",
properties: [],
};
const themeConfig = setDefault({
default: emptyTheme,
object: configObject,
property: "theme",
});
if (themeConfig.type !== "ObjectExpression") throw new Error("`theme` in Tailwind config must be an object");
/** @type {import("estree").ObjectExpression} */
const emptyThemeExtend = {
type: "ObjectExpression",
properties: [],
};
setDefault({
default: emptyThemeExtend,
object: themeConfig,
property: "extend",
});
/** @type {import("estree").ArrayExpression} */
const emptyPlugins = {
type: "ArrayExpression",
elements: [],
};
const pluginsList = setDefault({
default: emptyPlugins,
object: configObject,
property: "plugins",
});
if (pluginsList.type !== "ArrayExpression") throw new Error("`plugins` in Tailwind config must be an array");
if (forms) {
let formsImportedAs = findImport({ cjs: true, package: "@tailwindcss/forms", typeScriptEstree: tailwindConfigAst }).require;
// Add a forms plugin import if it's not there
if (!formsImportedAs) {
formsImportedAs = "forms";
addImport({ require: formsImportedAs, cjs: true, package: "@tailwindcss/forms", typeScriptEstree: tailwindConfigAst });
}
pluginsList.elements.push({
type: "Identifier",
name: formsImportedAs,
});
}
if (typography) {
let typographyImportedAs = findImport({ cjs: true, package: "@tailwindcss/typography", typeScriptEstree: tailwindConfigAst }).require;
// Add a typography plugin import if it's not there
if (!typographyImportedAs) {
typographyImportedAs = "typography";
addImport({ require: typographyImportedAs, cjs: true, package: "@tailwindcss/typography", typeScriptEstree: tailwindConfigAst });
}
pluginsList.elements.push({
type: "Identifier",
name: typographyImportedAs,
});
}
if (daisyui) {
let daisyuiImportedAs = findImport({ cjs: true, package: "daisyui", typeScriptEstree: tailwindConfigAst }).require;
// Add a daisyUI plugin import if it's not there
if (!daisyuiImportedAs) {
daisyuiImportedAs = "daisyui";
addImport({ require: daisyuiImportedAs, cjs: true, package: "daisyui", typeScriptEstree: tailwindConfigAst });
}
pluginsList.elements.push({
type: "Identifier",
name: daisyuiImportedAs,
});
}
return tailwindConfigAst;
};
/**
* @param {import("../../ast-io.js").PostCSSAst} postcss
* @returns {import("../../ast-io.js").PostCSSAst}
*/
const updateGlobalStylesheet = (postcss) => {
const base = new AtRule({
name: "tailwind",
params: "base",
});
const components = new AtRule({
name: "tailwind",
params: "components",
});
const utilities = new AtRule({
name: "tailwind",
params: "utilities",
});
postcss.append(components);
postcss.append(utilities);
const imports = postcss.nodes.filter((node) => node.type === "atrule" && node.name === "import");
const lastImport = imports[imports.length - 1];
if (lastImport) {
lastImport.after(base);
} else {
const [stylesHintComment] = postcss.nodes.filter((node) => node.type === "comment" && node.text === stylesHint);
if (stylesHintComment) {
stylesHintComment.after(base);
} else {
postcss.prepend(base);
}
}
return postcss;
};
/** @type {import("../..").AdderRun<import("./__info.js").Options>} */
export const run = async ({ install, options, updateCss, updateJavaScript, updateJson, folderInfo }) => {
await updateJavaScript({
path: tailwindConfigCjsPath,
async script({ typeScriptEstree }) {
return {
typeScriptEstree: updateTailwindConfig(typeScriptEstree, options.forms, options.typography, options.daisyui),
};
},
});
await updateJavaScript({
path: postcssConfigCjsPath,
async script({ typeScriptEstree }) {
return {
typeScriptEstree: updatePostcssConfig(typeScriptEstree),
};
},
});
await updateCss({
path: `/src/app.${extension}`,
async style({ postcss }) {
return {
postcss: updateGlobalStylesheet(postcss),
};
},
});
await install({ package: "tailwindcss" });
if (options.forms) await install({ package: "@tailwindcss/forms" });
if (options.typography) await install({ package: "@tailwindcss/typography" });
if (options.daisyui) await install({ package: "daisyui" });
if ("prettier" in folderInfo.allDependencies) {
// update plugins in prettier config
await updateJson({
path: prettierConfigPath,
async json({ obj }) {
obj.plugins = obj.plugins ? [...obj.plugins, "prettier-plugin-tailwindcss"] : ["prettier-plugin-tailwindcss"];
return { obj };
},
});
await install({ package: "prettier-plugin-tailwindcss" });
}
};