-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyles.js
108 lines (88 loc) · 3.23 KB
/
styles.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
108
const package = require(`${process.env.PWD}/package.json`);
const entrypoints = require(`${process.env.PWD}/entrypoints`);
const sass = require('sass');
const postcss = require('postcss');
const purgecss = require('@fullhuman/postcss-purgecss') // Removes unused CSS from output
const autoprefixer = require('autoprefixer'); // Adds vendor spec prefixes
const mqpacker = require('@hail2u/css-mqpacker'); // Packs media queries together
const cssnano = require('cssnano'); // CSS optimization
const path = require('path');
const fs = require('fs');
/**
* Plugin configuration
*/
let plugins = {
// TODO: Regex for hljs styles are not working
// purgecss: purgecss({
// content: [
// path.join(__dirname, 'dist/**/*.html'),
// path.join(__dirname, 'eleventy.config.js')
// ],
// fontFace: true,
// keyframes: true,
// // variables: true,
// safelist: {
// // standard: [
// // /hljs[\S]*?(?=")/g
// // ],
// deep: [
// /hljs-[-_:\w]*/g,
// // USWDS Navigation/Header
// /usa-js-mobile-nav--active/,
// /is-visible/
// ]
// // greedy: []
// },
// defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
// }),
autoprefixer: autoprefixer('last 4 version'),
mqpacker: mqpacker({
sort: true
}),
cssnano: cssnano({
preset: [
'default', {
// TODO: The current calc preset is not up-to-date with postcss-calc so
// this needs to be disabled for now https://github.com/postcss/postcss-simple-vars/issues/94
calc: false
}
]
})
};
/**
* Executing function
*/
(async () => {
try {
for (let i = 0; i < entrypoints.length; i++) {
const style = entrypoints[i];
if (!style.hasOwnProperty('input') || path.extname(style.input) != '.scss') continue;
console.log(`[${package.name}] Compiling Sass for "${style.input.replace(process.env.PWD, '')}"`);
for (let i = 0; i < style.output.length; i++) {
let output = style.output[i];
let result = await sass.compileAsync(style.input, output.options);
let css = result.css;
if (result.hasOwnProperty('sourceMap')) {
console.log(`[${package.name}] Generating sourcemaps`);
let sourceMap = JSON.stringify(result.sourceMap);
let sourceMapBase64 = (Buffer.from(sourceMap, 'utf8') || '').toString('base64');
let sourceMapComment = '/*# sourceMappingURL=data:application/json;charset=utf-8;base64,' + sourceMapBase64 + ' */';
css = `${css}\n\n${sourceMapComment}`;
}
fs.writeFileSync(output.file, css);
console.log(`[${package.name}] Sass compiled. Running output through PostCSS`);
// Set plugins if they are string keys for pre-configured plugins
style.plugins = style.plugins
.map(p => typeof p === 'string' ? plugins[p] : p);
let optim = await postcss(style.plugins)
.process(css, {
from: output.file
});
fs.writeFileSync(output.file, optim.css);
console.log(`[${package.name}] Styles written to "${output.file.replace(process.env.PWD, '')}"`);
}
}
} catch (err) {
console.dir(err);
}
})();