-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·336 lines (271 loc) · 8.71 KB
/
Copy pathindex.js
File metadata and controls
executable file
·336 lines (271 loc) · 8.71 KB
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
#!/usr/bin/env node
import path from 'node:path'
import { text } from 'node:stream/consumers'
import prettyHrtime from 'pretty-hrtime'
import read from 'read-cache'
import pc from 'picocolors'
import { glob } from 'tinyglobby'
import slash from 'slash'
import chokidar from 'chokidar'
import postcss from 'postcss'
import postcssrc from 'postcss-load-config'
import postcssReporter from 'postcss-reporter/lib/formatter.js'
import argv from './lib/args.js'
import createDependencyGraph from './lib/DependencyGraph.js'
import getMapfile from './lib/getMapfile.js'
import outputFile from './lib/outputFile.js'
const reporter = postcssReporter()
const depGraph = createDependencyGraph()
const expandGlob = async (patterns) => {
if (!Array.isArray(patterns)) patterns = [patterns]
const paths = await glob(
patterns.map((i) => slash(String(i))),
{ dot: argv.includeDotfiles },
)
return paths.map((i) => path.resolve(i))
}
let input = argv._
const { dir, output, replace } = argv
if (argv.map) argv.map = { inline: false }
// will be just one config file in the vast majority of cases
const configFiles = new Set()
let argvConfigSet = false
if (argv.env) process.env.NODE_ENV = argv.env
if (argv.config) {
argvConfigSet = true
argv.config = path.resolve(argv.config)
}
let { isTTY } = process.stdin
if (process.env.FORCE_IS_TTY === 'true') {
isTTY = true
}
if (argv.watch && isTTY) {
process.stdin.on('end', () => process.exit(0))
process.stdin.resume()
}
/* istanbul ignore next */
if (parseInt(postcss().version) < 8) {
error('Please install PostCSS 8 or above')
}
const cliConfig = {
options: {
map: argv.map !== undefined ? argv.map : { inline: true },
parser: argv.parser ? await import(argv.parser) : undefined,
syntax: argv.syntax ? await import(argv.syntax) : undefined,
stringifier: argv.stringifier ? await import(argv.stringifier) : undefined,
},
plugins: argv.use
? await Promise.all(
argv.use.map(async (plugin) => {
try {
return (await import(plugin)).default()
} catch (e) {
const msg = e.message || `Unknown error in '${plugin}'`
let prefix = msg.includes(plugin) ? '' : ` (${plugin})`
if (e.name && e.name !== 'Error') prefix += `: ${e.name}`
error(`Plugin Error${prefix}: ${msg}`)
}
}),
)
: [],
}
if (argv.watch && !(output || replace || dir)) {
error('Cannot write to stdout in watch mode')
}
if (input && input.length) {
input = await expandGlob(input)
if (!input.length) {
error('Input Error: You must pass a valid list of files to parse')
}
if (input.length > 1 && !dir && !replace) {
error('Input Error: Must use --dir or --replace with multiple input files')
}
} else {
if (replace || dir) {
error('Input Error: Cannot use --dir or --replace when reading from stdin')
}
if (argv.watch) {
error('Input Error: Cannot run in watch mode when reading from stdin')
}
input = ['stdin']
}
try {
const results = await files(input)
if (argv.watch) {
const printMessage = () =>
printVerbose(pc.dim('\nWaiting for file changes...'))
const deps = await dependencies(results)
const watcher = chokidar.watch(input.concat(deps), {
usePolling: argv.poll,
interval: argv.poll && typeof argv.poll === 'number' ? argv.poll : 100,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
})
if (configFiles.size) watcher.add([...configFiles])
watcher.on('ready', printMessage).on('change', (file) => {
let recompile = []
// if it's a config file, skip to recompiling everything
if (!configFiles.has(file)) {
if (input.includes(file)) recompile.push(file)
const dependants = depGraph
.dependantsOf(file)
.concat(getAncestorDirs(file).flatMap(depGraph.dependantsOf))
recompile = recompile.concat(
dependants.filter((file) => input.includes(file)),
)
}
if (!recompile.length) recompile = input
return files([...new Set(recompile)])
.then((results) => dependencies(results))
.then((deps) => watcher.add(deps))
.then(printMessage)
.catch((err) => {
// Watch mode shouldn't exit on file processing error
error(err, argv.watch)
})
})
}
} catch (err) {
error(err)
}
function rc(ctx, path) {
if (argv.use) return Promise.resolve(cliConfig)
return postcssrc(ctx, path)
.then((rc) => {
if (rc.options.from || rc.options.to) {
error(
'Config Error: Can not set from or to options in config file, use CLI arguments instead',
)
}
configFiles.add(rc.file)
return rc
})
.catch((err) => {
// if a config path is passed explicitly in CLI do not ignore the error
if (!err.message.includes('No PostCSS Config found') || argvConfigSet) {
throw err
}
})
}
function files(files) {
return Promise.all(
files.map((file) => {
if (file === 'stdin') {
return text(process.stdin).then((content) => {
if (!content) return error('Input Error: Did not receive any STDIN')
return css(content, 'stdin')
})
}
return read(file).then((content) => css(content, file))
}),
)
}
async function css(css, file) {
const ctx = { options: cliConfig.options }
if (file !== 'stdin') {
ctx.file = {
dirname: path.dirname(file),
basename: path.basename(file),
extname: path.extname(file),
}
}
const relativePath =
file !== 'stdin' ? path.relative(path.resolve(), file) : file
const configDir = argv.config || ctx.file?.dirname || process.cwd()
const time = process.hrtime()
printVerbose(pc.cyan(`Processing ${pc.bold(relativePath)}...`))
const config = (await rc(ctx, configDir)) || cliConfig
const options = { ...config.options }
if (file === 'stdin' && output) file = output
// TODO: Unit test this
options.from = file === 'stdin' ? path.join(process.cwd(), 'stdin') : file
if (output || dir || replace) {
const base = argv.base
? file.replace(path.resolve(argv.base), '')
: path.basename(file)
options.to = output || (replace ? file : path.join(dir, base))
if (argv.ext) {
options.to = options.to.replace(path.extname(options.to), argv.ext)
}
options.to = path.resolve(options.to)
}
if (!options.to && config.options.map && !config.options.map.inline) {
error(
'Output Error: Cannot output external sourcemaps when writing to STDOUT',
)
}
const result = await postcss(config.plugins).process(css, options)
const tasks = []
if (options.to) {
tasks.push(outputFile(options.to, result.css))
if (result.map) {
const mapfile = getMapfile(options)
tasks.push(outputFile(mapfile, result.map.toString()))
}
} else process.stdout.write(result.css, 'utf8')
await Promise.all(tasks)
const prettyTime = prettyHrtime(process.hrtime(time))
printVerbose(
pc.green(`Finished ${pc.bold(relativePath)} in ${pc.bold(prettyTime)}`),
)
const messages = result.warnings()
if (messages.length) {
console.warn(reporter({ ...result, messages }))
}
return result
}
async function dependencies(results) {
if (!Array.isArray(results)) results = [results]
const depArrays = await Promise.all(
results.map(async (result) => {
if (result.messages.length <= 0) return []
return Promise.all(
result.messages
.filter(
(msg) => msg.type === 'dependency' || msg.type === 'dir-dependency',
)
.map(depGraph.add)
.map(async (dependency) => {
if (dependency.type === 'dir-dependency') {
return dependency.glob
? await expandGlob(path.join(dependency.dir, dependency.glob))
: dependency.dir
}
return dependency.file
}),
)
}),
)
// depth of 2 is needed because glob dir-dependency can return an array of files
return depArrays.flat(2)
}
function printVerbose(message) {
if (argv.verbose) console.warn(message)
}
function error(err, dontExit) {
// Seperate error from logging output
if (argv.verbose) console.error()
if (typeof err === 'string') {
console.error(pc.red(err))
} else if (err.name === 'CssSyntaxError') {
console.error(err.toString())
} else {
console.error(err)
}
if (dontExit) return
process.exit(1)
}
// Input: '/imports/components/button.css'
// Output: ['/imports/components', '/imports', '/']
function getAncestorDirs(file) {
const { root } = path.parse(file)
const ancestors = []
let current = file
while (current !== root) {
current = path.dirname(current)
ancestors.push(current)
}
return ancestors
}