-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscss.js
49 lines (40 loc) · 1.31 KB
/
scss.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
const sass = require("node-sass");
const postcss = require("postcss");
const autoprefixer = require("autoprefixer");
const CleanCSS = require("clean-css");
const fs = require("fs-extra");
const path = require("path");
const srcDir = path.join(__dirname, "src", "styles");
const outDir = path.join(__dirname, "dist", "css");
const isProduction = process.env.NODE_ENV === "production";
// Empty the output directory
fs.emptyDirSync(outDir);
fs.readdir(srcDir, (err, files) => {
if (err) throw err;
files.forEach((file) => {
if (path.extname(file) === ".scss") {
const srcFile = path.join(srcDir, file);
const outFile = path.join(outDir, file.replace(".scss", ".css"));
sass.render(
{
file: srcFile,
outputStyle: isProduction ? "compressed" : "expanded",
},
(err, result) => {
if (err) throw err;
postcss([autoprefixer])
.process(result.css, { from: srcFile, to: outFile })
.then((result) => {
let outputCss = result.css;
if (isProduction) {
outputCss = new CleanCSS({}).minify(result.css).styles;
}
fs.writeFile(outFile, outputCss, (err) => {
if (err) throw err;
});
});
}
);
}
});
});