-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsass-builder.js
75 lines (64 loc) · 1.78 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
71
72
73
74
75
/* 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 globImporter = require( 'node-sass-glob-importer' );
// 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 = [
'icon',
'main',
'editor',
'admin/menu',
'admin/customizer',
'admin/nav-menus',
'admin/edit-table',
'module/luminous',
'module/-overlay-header',
];
files.forEach( ( fileName ) => {
// renderSyncだとimporter使えない
sass.render(
{
file: path.resolve( __dirname, src, `${ fileName }.scss` ),
outputStyle: 'compressed',
importer: globImporter(),
},
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);}
} );
}
}
);
} );