1 import path from "node:path"
2 import type { Config } from "tailwindcss"
3 import type { Plugin } from "vite"
4
5 interface InjectTailwindCSSOptions {
6 entry: string
7 sources?: string[]
8 plugins?: string[]
9 }
10
11 export const injectTailwindCSS = (
12 options: InjectTailwindCSSOptions
13 ): Plugin => {
14 return {
15 name: "medusa:inject-tailwindcss",
16 config: () => ({
17 css: {
18 postcss: {
19 plugins: [
20 require("tailwindcss")({
21 config: createTailwindConfig(
22 options.entry,
23 options.sources,
24 options.plugins
25 ),
26 }),
27 ],
28 },
29 },
30 }),
31 }
32 }
33
34 function createTailwindConfig(
35 entry: string,
36 sources: string[] = [],
37 plugins: string[] = []
38 ) {
39 const root = path.join(entry, "**/*.{js,ts,jsx,tsx}")
40 const html = path.join(entry, "index.html")
41
42 let dashboard = ""
43
44 try {
45 dashboard = path.join(
46 path.dirname(require.resolve("@medusajs/dashboard")),
47 "**/*.{js,ts,jsx,tsx}"
48 )
49 } catch (_e) {
50 // ignore
51 }
52
53 let ui: string = ""
54
55 try {
56 ui = path.join(
57 path.dirname(require.resolve("@medusajs/ui")),
58 "**/*.{js,ts,jsx,tsx}"
59 )
60 } catch (_e) {
61 // ignore
62 }
63
64 const sourceExtensions = sources.map((s) =>
65 path.join(s, "**/*.{js,ts,jsx,tsx}")
66 )
67 const pluginExtensions: string[] = []
68
69 for (const plugin of plugins) {
70 try {
71 const pluginPath = path.join(
72 path.dirname(require.resolve(plugin)),
73 "**/*.{js,ts,jsx,tsx}"
74 )
75
76 pluginExtensions.push(pluginPath)
77 } catch (_e) {
78 // ignore
79 }
80 }
81
82 const config: Config = {
83 presets: [require("@medusajs/ui-preset")],
84 content: [
85 html,
86 root,
87 dashboard,
88 ui,
89 ...sourceExtensions,
90 ...pluginExtensions,
91 ],
92 darkMode: "class",
93 }
94
95 return config
96 }