forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailwindcss.js
164 lines (146 loc) · 4.33 KB
/
tailwindcss.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
import fs from 'fs'
import path from 'path'
import execa from 'execa'
import { outputFileSync } from 'fs-extra'
import Listr from 'listr'
import { errorTelemetry } from '@redwoodjs/telemetry'
import { getPaths } from '../../../../lib'
import c from '../../../../lib/colors'
export const command = 'tailwindcss'
export const aliases = ['tailwind', 'tw']
export const description = 'Set up tailwindcss and PostCSS'
export const builder = (yargs) => {
yargs.option('force', {
alias: 'f',
default: false,
description: 'Overwrite existing configuration',
type: 'boolean',
})
yargs.option('install', {
alias: 'i',
default: true,
description: 'Install packages',
type: 'boolean',
})
}
const tailwindImports = [
'@import "tailwindcss/base";',
'@import "tailwindcss/components";',
'@import "tailwindcss/utilities";',
]
const tailwindImportsExist = (indexCSS) =>
tailwindImports
.map((el) => new RegExp(el))
.every((tailwindDirective) => tailwindDirective.test(indexCSS))
const tailwindImportsAndNotes = [
'/**',
' * START --- SETUP TAILWINDCSS EDIT',
' *',
' * `yarn rw setup ui tailwindcss` placed these imports here',
" * to inject Tailwind's styles into your CSS.",
' * For more information, see: https://tailwindcss.com/docs/installation#include-tailwind-in-your-css',
' */',
...tailwindImports,
'/**',
' * END --- SETUP TAILWINDCSS EDIT',
' */\n',
]
export const handler = async ({ force, install }) => {
const rwPaths = getPaths()
const packages = [
'postcss',
'postcss-loader',
'tailwindcss',
'autoprefixer@^9.8.8',
]
const tasks = new Listr([
{
title: 'Installing packages...',
skip: () => !install,
task: () => {
return new Listr([
{
title: `Install ${packages.join(', ')}`,
task: async () => {
await execa('yarn', [
'workspace',
'web',
'add',
'-D',
...packages,
])
},
},
])
},
},
{
title: 'Configuring PostCSS...',
task: () => {
/**
* Check if PostCSS config already exists.
* If it exists, throw an error.
*/
const postCSSConfigPath = rwPaths.web.postcss
if (!force && fs.existsSync(postCSSConfigPath)) {
throw new Error(
'PostCSS config already exists.\nUse --force to override existing config.'
)
} else {
const postCSSConfig = fs.readFileSync(
path.join(__dirname, '../templates/postcss.config.js.template'),
'utf-8'
)
return outputFileSync(postCSSConfigPath, postCSSConfig)
}
},
},
{
title: 'Initializing Tailwind CSS...',
task: async () => {
const tailwindConfigPath = path.join(
rwPaths.web.config,
'tailwind.config.js'
)
if (fs.existsSync(tailwindConfigPath)) {
if (force) {
// `yarn tailwindcss init` will fail these files already exists
fs.unlinkSync(tailwindConfigPath)
} else {
throw new Error(
'Tailwindcss config already exists.\nUse --force to override existing config.'
)
}
}
await execa('yarn', ['tailwindcss', 'init', tailwindConfigPath])
// Replace `content`.
const tailwindConfig = fs.readFileSync(tailwindConfigPath, 'utf-8')
const newTailwindConfig = tailwindConfig.replace(
'content: []',
"content: ['src/**/*.{js,jsx,ts,tsx}']"
)
fs.writeFileSync(tailwindConfigPath, newTailwindConfig)
},
},
{
title: 'Adding import to index.css...',
task: (_ctx, task) => {
const INDEX_CSS_PATH = path.join(rwPaths.web.src, 'index.css')
const indexCSS = fs.readFileSync(INDEX_CSS_PATH, 'utf-8')
if (tailwindImportsExist(indexCSS)) {
task.skip('Imports already exist in index.css')
} else {
const newIndexCSS = tailwindImportsAndNotes.join('\n') + indexCSS
fs.writeFileSync(INDEX_CSS_PATH, newIndexCSS)
}
},
},
])
try {
await tasks.run()
} catch (e) {
errorTelemetry(process.argv, e.message)
console.error(c.error(e.message))
process.exit(e?.exitCode || 1)
}
}