-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
47 lines (40 loc) · 1.89 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
// noinspection JSUnresolvedLibraryURL
const fs = require('fs');
const { gzipSync } = require('node:zlib');
const BuildDocs = process.argv.includes('--docs');
const DIST = BuildDocs ? './docs' : './dist';
const CurveCore = fs.readFileSync('node_modules/@mojs/core/dist/mo.umd.js', { encoding: 'utf8' });
const Player = fs.readFileSync('node_modules/@mojs/player/build/mojs-player.min.js', { encoding: 'utf8' });
const CurveEditor = fs.readFileSync('node_modules/@mojs/curve-editor/app/build/mojs-curve-editor.min.js', { encoding: 'utf8' });
/**
* @param {string} str
* @param {string} oldStr
* @param {string} newStr
* @return {string}
*/
function replace(str, oldStr, newStr) {
const index = str.indexOf(oldStr);
return str.substring(0, index) + newStr + str.substring(index + oldStr.length);
}
let IndexHTML = fs.readFileSync('./index.html', { encoding: 'utf8' });
IndexHTML = replace(
IndexHTML, '<script id="allape_dev_id_core" src="node_modules/@mojs/core/dist/mo.umd.js"></script>',
BuildDocs ? '<script src="https://cdn.jsdelivr.net/npm/@mojs/core"></script>' : `<script>${CurveCore}</script>`,
);
IndexHTML = replace(
IndexHTML,
'<script id="allape_dev_id_player" src="node_modules/@mojs/player/build/mojs-player.js"></script>',
BuildDocs ? '<script src="https://cdn.jsdelivr.net/npm/@mojs/player"></script>' : `<script>${Player}</script>`,
);
IndexHTML = replace(
IndexHTML,
'<script id="allape_dev_id_curve_editor" src="node_modules/@mojs/curve-editor/app/build/mojs-curve-editor.js"></script>',
BuildDocs ? '<script src="https://cdn.jsdelivr.net/npm/@mojs/curve-editor"></script>' : `<script>${CurveEditor}</script>`,
);
fs.mkdirSync(DIST, { recursive: true });
fs.writeFileSync(`${DIST}/index.html`, IndexHTML);
if (!BuildDocs) {
const gzipped = gzipSync(IndexHTML);
fs.writeFileSync(`${DIST}/index.html.gz`, gzipped);
fs.writeFileSync(`./esp32/src/assets/index.html.gz`, gzipped);
}