-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
autoprefixer.js
46 lines (36 loc) · 1.21 KB
/
autoprefixer.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
import process from 'node:process';
import childProcess from 'node:child_process';
import getStdin from 'get-stdin';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
import postcssSafeParser from 'postcss-safe-parser';
import postcssScssParser from 'postcss-scss';
const subprocess = childProcess.spawn('npx', ['browserslist@latest', '--update-db'], {
cwd: import.meta.dirname, stdio: 'ignore',
});
subprocess.unref();
subprocess.on('error', () => {});
const data = await getStdin();
const options = JSON.parse(process.argv[2]);
if (options.browsers) {
options.overrideBrowserslist = options.browsers;
delete options.browsers;
}
const postcssParser = options.is_css ? postcssSafeParser : postcssScssParser;
try {
const {css} = await postcss(autoprefixer(options)).process(data, {
parser: postcssParser,
from: undefined,
});
process.stdout.write(css);
// TODO: Log warnings in an non-error way somehow.
// const warnings = result.warnings();
// if (warnings.length > 0) {
// console.error(warnings.join('\n '));
// }
} catch (error) {
if (error.name === 'CssSyntaxError') {
error.message += `\n${error.showSourceCode()}`;
}
console.error(error.message.replace('<css input>:', ''));
}