forked from xitu/juejin-markdown-themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
57 lines (46 loc) · 1.36 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
import fs from 'fs-extra';
import fetch from 'node-fetch';
import path from 'path';
import sass from 'sass';
import less from 'less';
import cssnano from 'cssnano';
import themes from './themes';
const sassHandler = (input) => {
const result = sass.renderSync({ data: input });
return result.css.toString();
};
const lessHandler = async (input) => {
const { css } = await less.render(input);
return css;
};
const handlerMap = {
css: (input) => input,
sass: sassHandler,
scss: sassHandler,
less: lessHandler,
};
(async function main() {
const result = {};
fs.ensureDirSync(path.resolve(__dirname, 'dist'));
for (let [key, p] of Object.entries(themes)) {
const code = await fetch(
`https://raw.githubusercontent.com/${p.owner}/${p.repo}/${p.ref}/${p.path}`
).then((res) => res.text());
const ext = path.extname(p.path).slice(1);
const css = await handlerMap[ext](code);
const { css: minifedCss } = await cssnano.process(css);
// write css
fs.writeFileSync(path.resolve(__dirname, 'dist', key + '.css'), minifedCss);
result[key] = minifedCss;
}
// write js
fs.writeFileSync(
path.resolve(__dirname, 'dist/index.js'),
'module.exports=' + JSON.stringify(result, null, 2)
);
// gallery
fs.writeFileSync(
path.resolve(__dirname, 'gallery/themes.js'),
'window.themes=' + JSON.stringify(result)
);
})();