-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·107 lines (87 loc) · 2.74 KB
/
build.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const glob = require( 'glob' );
const fs = require( 'fs' );
const path = require( 'path' );
const pascal = require( 'camelcase' );
const atImport = require( 'postcss-easy-import' );
const precss = require( 'precss' );
const namespace = require( 'postcss-namespace' );
const autoprefixer = require( 'autoprefixer' );
const postcss = require( 'postcss' );
const combineDuplicates = require( 'postcss-combine-duplicated-selectors' );
const promises = [];
// add @prefix rule to top of css ast so that postcss-namespace can work
const addPrefixRule = postcss.plugin( 'postcss-add-prefix-rule', opts => {
return root => {
root.nodes[0].before( `@prefix ${opts.prefix}` );
};
} );
async function writeFile( outFile, content ) {
try {
await fs.promises.mkdir( path.dirname( outFile ) );
} catch ( err ) {
if ( err && err.code === 'EEXIST' ) {
return fs.promises.writeFile( outFile, content );
}
throw err;
}
return fs.promises.writeFile( outFile, content );
}
async function buildCss( input, outFile ) {
outFile = outFile + '.css';
const scss = await fs.promises.readFile( input );
const result = await postcss( [
atImport(),
combineDuplicates( { removeDuplicatedProperties: true } ),
precss(),
addPrefixRule( { prefix: 'NP' } ),
namespace( { token: '-' } ),
autoprefixer()
] ).process( scss, { from: input, to: outFile } );
return writeFile( outFile, result.css );
}
async function buildScss( input, outFile ) {
outFile = outFile + '.module.scss';
const scss = await fs.promises.readFile( input );
const result = await postcss( [
atImport(),
combineDuplicates( {
removeDuplicatedProperties: true
} )
] ).process( scss, { from: input, to: outFile } );
return writeFile( outFile, result.css );
}
function buildStrategies() {
const stylesheets = glob.sync( 'src/strategy/*/*/styles.module.scss' );
const promises = [];
stylesheets.forEach( stylesheet => {
const parts = stylesheet.split( path.sep );
const outFile = path.join(
'dist',
'styles',
pascal( [ parts[2], parts[3] ], { pascalCase: true } )
);
promises.push( buildCss( stylesheet, outFile ) );
promises.push( buildScss( stylesheet, outFile ) );
} );
return Promise.all( promises );
}
function buildHamburger() {
const Hamburger = 'src/Hamburger/styles.module.scss';
const dest = 'dist/styles/Hamburger';
return Promise.all( [ buildCss( Hamburger, dest ), buildScss( Hamburger, dest ) ] );
}
function init() {
promises.push( buildStrategies() );
promises.push( buildHamburger() );
Promise.all( promises ).then(
() => {
console.log( 'styles created' );
},
err => {
console.error( err );
process.exit( 1 );
}
);
}
init();