-
Notifications
You must be signed in to change notification settings - Fork 87
/
build.js
133 lines (113 loc) · 3.41 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// @ts-check
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 postcss from 'postcss';
import { rollup } from 'rollup';
import virtual from '@rollup/plugin-virtual';
import _ from 'lodash';
import themes from './themes';
import pkg from './package.json';
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() {
fs.ensureDirSync(path.resolve(__dirname, 'dist'));
const result = {};
let lazyCode = 'module.exports={';
for (let [key, p] of Object.entries(themes)) {
console.log(key, 'start');
const code = await fetch(
`https://raw.githubusercontent.com/${p.owner}/${p.repo}/${p.ref}/${p.path}`
).then((res) => res.text())
.catch(() => {
throw new Error(
'Oops! themes.js format is wrong, ' +
'please refer to this link for corrections: ' +
'https://github.com/xitu/juejin-markdown-themes#-themesjs-%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E.'
);
});
const ext = path.extname(p.path).slice(1);
const css = await handlerMap[ext](code);
// check external url
await postcss()
.process(css)
.then((res) => {
res.root.walk((node) => {
if (
node.type === 'decl' &&
/url\(['"]?(https?:)?\/\//.test(node.value)
) {
throw new Error('External URL is not allowed');
}
if (
node.type === 'rule' &&
node.selectors.some((s) => {
return !s.startsWith('.markdown-body');
}) &&
node.parent.name !== 'keyframes' // allow keyframes
) {
throw new Error('Style must be wrapped with .markdown-body');
}
});
});
const { css: minCss } = await cssnano.process(css);
// write css
fs.writeFileSync(path.resolve(__dirname, 'dist', key + '.css'), css);
fs.writeFileSync(path.resolve(__dirname, 'dist', key + '.min.css'), minCss);
result[key] = {
style: minCss,
highlight: p.highlight,
};
fs.writeFileSync(
path.resolve(__dirname, 'dist', `${key}.js`),
`module.exports=${JSON.stringify(minCss)}`
);
lazyCode += `'${key}':{ highlight: ${JSON.stringify(
p.highlight
)}, style: () => import('./${key}') },`;
console.log(key, 'end');
}
lazyCode += '}';
// write index.json
fs.writeJsonSync(path.resolve(__dirname, 'dist/index.json'), result);
// write index.js
const build = await rollup({
input: pkg.name,
plugins: [
virtual({
[pkg.name]: 'export default ' + JSON.stringify(result, null, 2),
}),
],
});
await build.write({
format: 'umd',
name: _.camelCase(pkg.name),
file: path.resolve(__dirname, 'dist/index.js'),
});
// write lazy.js
fs.writeFileSync(path.resolve(__dirname, 'dist/lazy.js'), lazyCode);
// gallery
fs.writeFileSync(
path.resolve(__dirname, 'gallery/themes.js'),
'window.themes=' + JSON.stringify(result)
);
})();
process.on('unhandledRejection', (error) => {
console.error('unhandledRejection', error);
process.exit(1);
});