-
Notifications
You must be signed in to change notification settings - Fork 4k
/
cli.js
539 lines (460 loc) · 14.7 KB
/
cli.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/env node
// eslint-disable
// import autoprefixer from 'autoprefixer'
import chokidar from 'chokidar'
import postcss from 'postcss'
import chalk from 'chalk'
import path from 'path'
import arg from 'arg'
import fs from 'fs'
import tailwindJit from './jit/processTailwindFeatures'
import tailwindAot from './processTailwindFeatures'
import resolveConfigInternal from '../resolveConfig'
import fastGlob from 'fast-glob'
import getModuleDependencies from './lib/getModuleDependencies'
import packageJson from '../package.json'
let env = {
DEBUG: process.env.DEBUG !== undefined,
}
// ---
function indentRecursive(node, indent = 0) {
node.each &&
node.each((child, i) => {
if (!child.raws.before || !child.raws.before.trim() || child.raws.before.includes('\n')) {
child.raws.before = `\n${node.type !== 'rule' && i > 0 ? '\n' : ''}${' '.repeat(indent)}`
}
child.raws.after = `\n${' '.repeat(indent)}`
indentRecursive(child, indent + 1)
})
}
function formatNodes(root) {
indentRecursive(root)
if (root.first) {
root.first.raws.before = ''
}
}
function help({ message, usage, commands, options }) {
// Render header
console.log()
console.log(' ', packageJson.name, packageJson.version)
// Render message
if (message) {
console.log()
console.log(' ', message)
}
// Render usage
if (usage && usage.length > 0) {
console.log()
console.log(' ', 'Usage:')
for (let example of usage) {
console.log(' ', ' ', example)
}
}
// Render commands
if (commands && commands.length > 0) {
console.log()
console.log(' ', 'Commands:')
for (let command of commands) {
console.log(' ', ' ', command)
}
}
// Render options
if (options) {
let groupedOptions = {}
for (let [key, value] of Object.entries(options)) {
if (typeof value === 'object') {
groupedOptions[key] = { ...value, flags: [key] }
} else {
groupedOptions[value].flags.push(key)
}
}
console.log()
console.log(' ', 'Options:')
for (let { flags, description } of Object.values(groupedOptions)) {
console.log(' ', ' ', flags.slice().reverse().join(', ').padEnd(15, ' '), description)
}
}
}
// ---
/*
TODOs:
- [x] Reduce getModuleDependencies calls (make configDeps global?)
- [x] Detect new files
- [x] Support raw content in purge config
- [x] Scaffold tailwind.config.js file (with postcss.config.js)
- [x] Support passing globs from command line
- [x] Make config file optional
- [ ] Support AOT mode
- [ ] Prebundle peer-dependencies
- [ ] Make minification work
- [x] --help option
- [x] conditional flags based on arguments
init -f, --full
build -f, --files
- [ ] --jit
Future:
- Detect project type, add sensible purge defaults
*/
let commands = {
init: {
run: init,
args: {
'--jit': { type: Boolean, description: 'Enable `JIT` mode' },
'--full': { type: Boolean, description: 'Generate a full tailwind.config.js file' },
'--postcss': { type: Boolean, description: 'Generate a PostCSS file' },
'-f': '--full',
'-p': '--postcss',
},
},
build: {
run: build,
args: {
'--jit': { type: Boolean, description: 'Build using `JIT` mode' },
'--files': { type: String, description: 'Use a glob as files to use' },
'--config': {
type: String,
description: 'Provide a custom config file, default: ./tailwind.config.js',
},
'--input': { type: String, description: 'The input css file' },
'--output': { type: String, description: 'The output css file' },
'--minify': { type: Boolean, description: 'Whether or not the result should be minified' },
'--watch': { type: Boolean, description: 'Start watching for changes' },
'-f': '--files',
'-c': '--config',
'-i': '--input',
'-o': '--output',
'-m': '--minify',
'-w': '--watch',
},
},
}
let sharedFlags = {
'--help': { type: Boolean, description: 'Prints this help message' },
'-h': '--help',
}
let command = process.argv.slice(2).find((arg) => !arg.startsWith('-')) || 'build'
if (commands[command] === undefined) {
help({
message: `Invalid command: ${command}`,
usage: ['tailwind <command> [options]'],
commands: ['init [file]', 'build <file> [options]'],
options: sharedFlags,
})
process.exit(1)
}
// Execute command
let { args: flags, run } = commands[command]
let args = (() => {
try {
return arg(
Object.fromEntries(
Object.entries({ ...flags, ...sharedFlags }).map(([key, value]) => [
key,
typeof value === 'object' ? value.type : value,
])
)
)
} catch (err) {
if (err.code === 'ARG_UNKNOWN_OPTION') {
help({
message: err.message,
usage: ['tailwind <command> [options]'],
options: sharedFlags,
})
process.exit(1)
}
throw err
}
})()
if (args['--help']) {
help({
options: { ...flags, ...sharedFlags },
usage: [`tailwind ${command} [options]`],
})
process.exit(0)
}
run()
function init() {
let tailwindConfigLocation = path.resolve('./tailwind.config.js')
if (fs.existsSync(tailwindConfigLocation)) {
console.log('tailwind.config.js already exists.')
} else {
let stubFile = fs.readFileSync(
args['--full']
? path.resolve(__dirname, '../stubs/defaultConfig.stub.js')
: path.resolve(__dirname, '../stubs/simpleConfig.stub.js'),
'utf8'
)
// Change colors import
stubFile = stubFile.replace('../colors', 'tailwindcss/colors')
// --jit mode
if (args['--jit']) {
// Add jit mode
stubFile = stubFile.replace('module.exports = {', "module.exports = {\n mode: 'jit',")
// Deleting variants
stubFile = stubFile.replace(/variants: {(.*)},\n /gs, '')
}
fs.writeFileSync(tailwindConfigLocation, stubFile, 'utf8')
console.log('Created Tailwind config file:', 'tailwind.config.js')
}
if (args['--postcss']) {
let postcssConfigLocation = path.resolve('./postcss.config.js')
if (fs.existsSync(postcssConfigLocation)) {
console.log('postcss.config.js already exists.')
} else {
let stubFile = fs.readFileSync(
path.resolve(__dirname, '../stubs/defaultPostCssConfig.stub.js'),
'utf8'
)
fs.writeFileSync(postcssConfigLocation, stubFile, 'utf8')
console.log('Created PostCSS config file:', 'tailwind.config.js')
}
}
}
function build() {
let input = args['--input']
let output = args['--output']
let shouldWatch = args['--watch']
let shouldMinify = args['--minify']
if (args['--config'] && !fs.existsSync(args['--config'])) {
console.error(`Specified config file ${args['--config']} does not exist.`)
process.exit(9)
}
let configPath =
args['--config'] ??
((defaultPath) => (fs.existsSync(defaultPath) ? defaultPath : null))(
path.resolve('./tailwind.config.js')
)
function resolveConfig() {
let config = require(configPath)
let resolvedConfig = resolveConfigInternal(config)
if (args['--files']) {
resolvedConfig.purge = args['--files'].split(',')
}
if (args['--jit']) {
resolvedConfig.mode = 'jit'
}
return resolvedConfig
}
if (!output) {
help({
message: 'Missing required output file: --output, -o, or first argument',
usage: [`tailwind ${command} [options]`],
options: { ...flags, ...sharedFlags },
})
process.exit(1)
}
function extractContent(config) {
return Array.isArray(config.purge) ? config.purge : config.purge.content
}
function extractFileGlobs(config) {
return extractContent(config).filter((file) => {
// Strings in this case are files / globs. If it is something else,
// like an object it's probably a raw content object. But this object
// is not watchable, so let's remove it.
return typeof file === 'string'
})
}
function extractRawContent(config) {
return extractContent(config).filter((file) => {
return typeof file === 'object' && file !== null
})
}
function getChangedContent(config) {
let changedContent = []
// Resolve globs from the purge config
let globs = extractFileGlobs(config)
let files = fastGlob.sync(globs)
for (let file of files) {
changedContent.push({
content: fs.readFileSync(path.resolve(file), 'utf8'),
extension: path.extname(file),
})
}
// Resolve raw content in the tailwind config
for (let { raw: content, extension = 'html' } of extractRawContent(config)) {
changedContent.push({ content, extension })
}
return changedContent
}
function buildOnce() {
let config = resolveConfig()
let changedContent = getChangedContent(config)
let tailwindPlugin =
config.mode === 'jit'
? (opts = {}) => {
return {
postcssPlugin: 'tailwindcss',
Once(root, { result }) {
tailwindJit(({ createContext }) => {
return () => {
return createContext(config, changedContent)
}
})(root, result)
},
}
}
: (opts = {}) => {
return {
postcssPlugin: 'tailwindcss',
plugins: [tailwindAot(() => config, configPath)],
}
}
tailwindPlugin.postcss = true
let plugins = [
// TODO: Bake in postcss-import support?
// TODO: Bake in postcss-nested support?
tailwindPlugin,
// require('autoprefixer'),
formatNodes,
]
let processor = postcss(plugins)
function processCSS(css) {
return processor.process(css, { from: input, to: output }).then((result) => {
fs.writeFile(output, result.css, () => true)
if (result.map) {
fs.writeFile(output + '.map', result.map.toString(), () => true)
}
})
}
let css = input
? fs.readFileSync(path.resolve(input), 'utf8')
: '@tailwind base; @tailwind components; @tailwind utilities'
return processCSS(css)
}
let context = null
function startWatcher() {
let changedContent = []
let configDependencies = []
let contextDependencies = new Set()
let watcher = null
function refreshConfig() {
env.DEBUG && console.time('Module dependencies')
for (let file of configDependencies) {
delete require.cache[require.resolve(file)]
}
if (configPath) {
configDependencies = getModuleDependencies(configPath).map(({ file }) => file)
for (let dependency of configDependencies) {
contextDependencies.add(dependency)
}
}
env.DEBUG && console.timeEnd('Module dependencies')
return resolveConfig()
}
async function rebuild(config) {
console.log('\nRebuilding...')
env.DEBUG && console.time('Finished in')
let tailwindPlugin =
config.mode === 'jit'
? (opts = {}) => {
return {
postcssPlugin: 'tailwindcss',
Once(root, { result }) {
env.DEBUG && console.time('Compiling CSS')
tailwindJit(({ createContext }) => {
return () => {
if (context !== null) {
context.changedContent = changedContent.splice(0)
return context
}
env.DEBUG && console.time('Creating context')
context = createContext(config, changedContent.splice(0))
env.DEBUG && console.timeEnd('Creating context')
return context
}
})(root, result)
env.DEBUG && console.timeEnd('Compiling CSS')
},
}
}
: (opts = {}) => {
return {
postcssPlugin: 'tailwindcss',
plugins: [tailwindAot(() => config, configPath)],
}
}
tailwindPlugin.postcss = true
let plugins = [
// TODO: Bake in postcss-import support?
// TODO: Bake in postcss-nested support?
tailwindPlugin,
// require('autoprefixer'),
formatNodes,
]
let processor = postcss(plugins)
function processCSS(css) {
return processor.process(css, { from: input, to: output }).then((result) => {
for (let message of result.messages) {
if (message.type === 'dependency') {
contextDependencies.add(message.file)
}
}
watcher.add([...contextDependencies])
fs.writeFile(output, result.css, () => true)
if (result.map) {
fs.writeFile(output + '.map', result.map.toString(), () => true)
}
})
}
let css = input
? fs.readFileSync(path.resolve(input), 'utf8')
: '@tailwind base; @tailwind components; @tailwind utilities'
let result = await processCSS(css)
env.DEBUG && console.timeEnd('Finished in')
return result
}
let configPath = args['--config'] ?? path.resolve('./tailwind.config.js')
let config = refreshConfig(configPath)
if (input) {
contextDependencies.add(path.resolve(input))
}
watcher = chokidar.watch([...contextDependencies, ...extractFileGlobs(config)], {
ignoreInitial: true,
})
let chain = Promise.resolve()
watcher.on('change', async (file) => {
if (contextDependencies.has(file)) {
env.DEBUG && console.time('Resolve config')
context = null
config = refreshConfig(configPath)
env.DEBUG && console.timeEnd('Resolve config')
env.DEBUG && console.time('Watch new files')
let globs = extractFileGlobs(config)
watcher.add(configDependencies)
watcher.add(globs)
env.DEBUG && console.timeEnd('Watch new files')
chain = chain.then(async () => {
changedContent.push(...getChangedContent(config))
await rebuild(config)
})
} else {
chain = chain.then(async () => {
changedContent.push({
content: fs.readFileSync(path.resolve(file), 'utf8'),
extension: path.extname(file),
})
await rebuild(config)
})
}
})
watcher.on('add', async (file) => {
chain = chain.then(async () => {
changedContent.push({
content: fs.readFileSync(path.resolve(file), 'utf8'),
extension: path.extname(file),
})
await rebuild(config)
})
})
chain = chain.then(() => {
changedContent.push(...getChangedContent(config))
return rebuild(config)
})
}
if (shouldWatch) {
startWatcher()
} else {
buildOnce()
}
}