-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-build.js
55 lines (48 loc) · 1.53 KB
/
post-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
import { readFileSync, writeFileSync, readdirSync, unlinkSync } from 'fs';
// regex to find font-face string in generated css
// there are 3 capture groups:
// 1. font-family name
// 2. data
// 3. format
const fontFaceRegex = /@font-face{font-family:(\w+).*url\((data:.*)\)\s(format\("\w+"\))}/g;
const fontFaceFileName = 'style.css.js';
const style = readFileSync('./dist/style.css', 'utf8');
const output = `export default \`${style.replace(fontFaceRegex, '').replace('\\', '\\\\')}\`;`
writeFileSync(`./dist/${fontFaceFileName}`, output);
const matches = [...style.matchAll(fontFaceRegex)];
const fontFamilyName = matches[0][1];
const dataStr = matches[0][2];
const formatStr = matches[0][3];
// create a font-face file
const fontFaceFile = `
const monacoEditorFontFace = new FontFace('${fontFamilyName}',
'url("${dataStr}") ${formatStr}');
monacoEditorFontFace.load()
.then(loadedFontFace => {
document.fonts.add(loadedFontFace);
});
`;
writeFileSync('./dist/monaco-editor-font-face.js', fontFaceFile);
// delete unused files
/*
const filesToKeep = [
'index.js',
'editor.worker', // this file will have an generated hash
'monaco-editor',
'sql.js', // this is the only language we support for now
fontFaceFileName,
'style.css', // this is the original style.css file
];
const files = readdirSync('./dist');
files.forEach(file => {
let deleteFile = true;
filesToKeep.forEach(fileToKeep => {
if (file.startsWith(fileToKeep)) {
deleteFile = false;
}
});
if (deleteFile) {
unlinkSync(`./dist/${file}`);
}
});
*/