This repository was archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
118 lines (98 loc) · 3.43 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
#!/usr/bin/env node
const esbuild = require('esbuild');
const {readFile, writeFile, mkdir, rm} = require('fs').promises;
const minify = require('html-minifier-terser').minify;
const crypto = require('crypto');
const buildScript = async () => {
const {metafile} = esbuild.buildSync({
entryPoints: ['src/index.js', 'src/deck/index.js'],
entryNames: '[dir]/[name]-[hash]',
bundle: true,
minify: true,
format: 'esm',
target: ['esnext'],
outdir: 'dist/build',
metafile: true,
define: {
'process.env.SIGNALING_SERVER': JSON.stringify('https://api.deckdeckgo.com'),
'process.env.NO_REMOTE': 'false',
'process.env.KEEP_HISTORY': 'false'
}
});
const {outputs} = metafile;
const prepare = async (scriptPath) => ({
scriptPath: scriptPath.replace('dist', ''),
sha256: await scriptSha256(scriptPath)
});
const promises = Object.keys(outputs).map((scriptPath) => prepare(scriptPath));
return Promise.all(promises);
};
const scriptSha256 = async (scriptPath) => {
const script = await readFile(scriptPath, 'utf8');
// prettier-ignore
return `'sha256-${crypto.createHash("sha256").update(script).digest("base64")}'`;
};
const buildCSS = () => {
const {metafile} = esbuild.buildSync({
entryPoints: ['src/index.css', 'src/deck/index.css', 'src/doc/index.css'],
entryNames: '[dir]/[name]-[hash]',
bundle: true,
minify: true,
format: 'esm',
target: ['esnext'],
outdir: 'dist/build',
metafile: true
});
const {outputs} = metafile;
return Object.keys(outputs);
};
const buildHTML = async ({srcPath, destPath, scripts, cssPaths}) => {
const src = await readFile(srcPath, 'utf8');
const minifyOptions = {
collapseWhitespace: true,
keepClosingSlash: true,
removeComments: false,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true
};
const html = (await minify(src, minifyOptions))
.replace('{{DECKDECKGO_EXTRA_SHAS}}', scripts.map(({sha256}) => sha256).join(' '))
.replace(
'<!-- DECKDECKGO_HEAD_SCRIPT -->',
scripts.map(({scriptPath}) => `<script type="module" src="${scriptPath}"></script>`).join('')
)
.replace(
'<!-- DECKDECKGO_HEAD_CSS -->',
cssPaths.map((cssPath) => `<link href="${cssPath.replace('dist', '')}" rel="stylesheet">`).join('')
);
await writeFile(destPath, html);
};
(async () => {
await rm('./dist', {recursive: true, force: true});
await mkdir('./dist/p', {recursive: true});
await mkdir('./dist/d', {recursive: true});
const scripts = await buildScript();
const cssPaths = buildCSS();
await buildHTML({
srcPath: 'src/deck/index.html',
destPath: 'dist/p/index.html',
scripts,
cssPaths: cssPaths.filter((cssPath) => cssPath.indexOf('/doc/') === -1)
});
await buildHTML({
srcPath: 'src/doc/index.html',
destPath: 'dist/d/index.html',
scripts: scripts.filter(({scriptPath}) => scriptPath.indexOf('/deck/') === -1),
cssPaths: cssPaths.filter((cssPath) => cssPath.indexOf('/deck/') === -1)
});
await buildHTML({
srcPath: 'src/index.html',
destPath: 'dist/index.html',
scripts: scripts.filter(({scriptPath}) => scriptPath.indexOf('/deck/') === -1 && scriptPath.indexOf('/doc/') === -1),
cssPaths: cssPaths.filter((cssPath) => cssPath.indexOf('/deck/') === -1 && cssPath.indexOf('/doc/') === -1)
});
})();