Skip to content
This repository was archived by the owner on May 5, 2024. It is now read-only.

Commit 3571371

Browse files
feat: support prettier-plugin-tailwindcss (#64)
* ⚡ feat: add prettier-plugin-tailwindcss --------- Co-authored-by: Manuel Serret <[email protected]>
1 parent 64d7695 commit 3571371

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

__info.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { extension } from "../postcss/stuff.js";
2+
import { prettierConfigPath } from "./stuff.js";
23

34
export const name = "Tailwind CSS";
45

@@ -70,4 +71,25 @@ export const heuristics = [
7071
return true;
7172
},
7273
},
74+
{
75+
description: "prettier is configured (if installed)",
76+
async detector({ folderInfo, readFile }) {
77+
// skip if prettier is not installed
78+
const isInstalled = "prettier" in folderInfo.allDependencies;
79+
if (!isInstalled) return true;
80+
81+
// check plugins
82+
const tailwindPlugin = "prettier-plugin-tailwindcss" in folderInfo.allDependencies;
83+
const sveltePlugin = "prettier-plugin-svelte" in folderInfo.allDependencies;
84+
if (!tailwindPlugin || sveltePlugin) return false;
85+
86+
// prettier-plugin-tailwindcss should replace prettier-plugin-svelte in .prettierrc
87+
const prettierConfig = await readFile({ path: prettierConfigPath });
88+
if (!prettierConfig) return false;
89+
90+
const { text } = prettierConfig;
91+
const { plugins } = JSON.parse(text);
92+
return plugins.includes("prettier-plugin-tailwindcss") && !plugins.includes("prettier-plugin-svelte");
93+
},
94+
},
7395
];

__run.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { walk } from "estree-walker";
22
import { AtRule } from "postcss";
33
import { addImport, findImport, setDefaultDefaultExport, setDefault, addRootBlockComment } from "../../ast-tools.js";
44
import { extension, postcssConfigCjsPath, stylesHint } from "../postcss/stuff.js";
5-
import { tailwindConfigCjsPath } from "./stuff.js";
5+
import { tailwindConfigCjsPath, prettierConfigPath } from "./stuff.js";
66

77
/**
88
* @param {import("../../ast-io.js").RecastAST} postcssConfigAst
@@ -101,7 +101,6 @@ const updatePostcssConfig = (postcssConfigAst) => {
101101
pluginsList.elements.splice(maxIndex, 0, {
102102
// @ts-expect-error - Force accept the comment - TODO: find a better way to handle this
103103
type: "Line",
104-
// @ts-expect-error - Force accept the comment
105104
value: `Some plugins, like ${goAfter[0]}, need to run before Tailwind`,
106105
});
107106

@@ -122,7 +121,6 @@ const updatePostcssConfig = (postcssConfigAst) => {
122121
pluginsList.elements.splice(maxIndex + 2, 0, {
123122
// @ts-expect-error - Force accept the comment
124123
type: "Line",
125-
// @ts-expect-error - Force accept the comment
126124
value: `But others, like ${goBefore[0]}, need to run after`,
127125
});
128126
} else if (pluginsList.type === "ObjectExpression") {
@@ -290,7 +288,7 @@ const updateGlobalStylesheet = (postcss) => {
290288
};
291289

292290
/** @type {import("../..").AdderRun<import("./__info.js").Options>} */
293-
export const run = async ({ install, options, updateCss, updateJavaScript }) => {
291+
export const run = async ({ install, options, updateCss, updateJavaScript, updateJson, folderInfo }) => {
294292
await updateJavaScript({
295293
path: tailwindConfigCjsPath,
296294
async script({ typeScriptEstree }) {
@@ -322,4 +320,27 @@ export const run = async ({ install, options, updateCss, updateJavaScript }) =>
322320
if (options.forms) await install({ package: "@tailwindcss/forms" });
323321
if (options.typography) await install({ package: "@tailwindcss/typography" });
324322
if (options.daisyui) await install({ package: "daisyui" });
323+
324+
if ("prettier" in folderInfo.allDependencies) {
325+
// update plugins in prettier config
326+
await updateJson({
327+
path: prettierConfigPath,
328+
async json({ obj }) {
329+
const plugins = obj.plugins.filter((/** @type {string} */ plugin) => plugin !== "prettier-plugin-svelte");
330+
obj.plugins = ["prettier-plugin-tailwindcss", ...plugins];
331+
return { obj };
332+
},
333+
});
334+
335+
// update package.json
336+
await updateJson({
337+
path: "/package.json",
338+
async json({ obj }) {
339+
delete obj.devDependencies["prettier-plugin-svelte"];
340+
return { obj };
341+
},
342+
});
343+
344+
await install({ package: "prettier-plugin-tailwindcss" });
345+
}
325346
};

stuff.js

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const tailwindConfigCjsPath = "/tailwind.config.cjs";
2+
export const prettierConfigPath = "/.prettierrc";

0 commit comments

Comments
 (0)