-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsass-builder.js
70 lines (59 loc) · 1.61 KB
/
sass-builder.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
/* eslint no-console: 0 */
// console.log('start sass-builder.js ...');
const path = require('path');
const fs = require('fs');
// node-sass
const sass = require('node-sass');
const nodeSassGlobbing = require('node-sass-globbing');
// postcss
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const mqpacker = require('css-mqpacker');
// consoleの色付け
const red = '\u001b[31m';
const green = '\u001b[32m';
const writeCSS = (filePath, css) => {
const dir = path.dirname(filePath);
// ディレクトリがなければ作成
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// css書き出し
fs.writeFileSync(filePath, css);
};
// パス
const src = 'src/scss';
const dist = 'dist/css';
const files = [
'ssp',
'common',
'post',
'term',
];
files.forEach((fileName) => {
// renderSyncだとimporter使えない
sass.render(
{
file: path.resolve(__dirname, src, `${fileName}.scss`),
outputStyle: 'compressed',
importer: nodeSassGlobbing,
},
function (err, sassResult) {
if (err) {
console.error(red + err);
} else {
const css = sassResult.css.toString();
const filePath = path.resolve(__dirname, dist, `${fileName}.css`);
// postcss実行
postcss([autoprefixer, mqpacker, cssnano])
.process(css, { from: undefined })
.then((postcssResult) => {
console.log(green + 'Wrote CSS to ' + filePath);
writeCSS(filePath, postcssResult.css);
// if (postcssResult.map) {fs.writeFile('dest/app.css.map', postcssResult.map.toString(), () => true);}
});
}
}
);
});