forked from voorhoede/front-end-tooling-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postcss.js
54 lines (44 loc) · 1.69 KB
/
postcss.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
// include depenencies (a-z)
const fs = require('fs');
const mkdirp = require('mkdirp');
const postcss = require('postcss');
// configure input, output and processors:
const inputFilename = 'src/index.css';
const outputDir = 'dist/';
const outputFilename = 'dist/index.css';
const processors = [
require('postcss-import'), // combine imports into one file
require('postcss-custom-properties'), // replace variables by their calculated values
require('postcss-color-function'), // replaces color functions with rgba values
require('postcss-color-rgba-fallback'), // adds a hex value before every rgba value
require('pixrem'), // adds pixel fallback before every rem value
require('postcss-calc'), // pre-calculcates calc functions where possible
require('autoprefixer') // vendor prefix for older browsers
];
if (process.env.NODE_ENV === 'production') {
processors.push(require('cssnano')); // minify css
}
// process input and write output to disk:
postcss(processors)
.process(fs.readFileSync(inputFilename), {
from: inputFilename,
to: outputFilename.substr(outputDir.length), // file path relative to output dir
map: { inline: false }
})
.then(writeOutput)
.catch(err => handleError(err.message));
function writeOutput(result) {
mkdirp(outputDir, (err) => {
handleError(err);
fs.writeFile(outputFilename, result.css, handleError);
if (result.map) {
fs.writeFile(`${outputFilename}.map`, result.map, handleError);
}
});
}
function handleError(err) {
if (err) {
console.log(err);
process.exit(1);
}
}