-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbuild.js
202 lines (194 loc) · 5.85 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs/promises');
const path = require('path');
const http = require('http');
const rollup = require('rollup');
const typescriptPlugin = require('@rollup/plugin-typescript');
const stringPlugin = require('rollup-plugin-string');
const terserPlugin = require('rollup-plugin-terser');
const marked = require('marked');
const jsdom = require('jsdom');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');
const chokidar = require('chokidar');
async function buildWorklet(fileName) {
console.log(`building ${fileName} worklet...`);
const inputOptions = {
input: `./src/${fileName}-processor.ts`,
plugins: [
typescriptPlugin({
include: [`./src/${fileName}*.ts`, './src/global.d.ts'],
exclude: ['node_modules', 'lib', '**/*.test.ts'],
lib: ['es2016', 'DOM'],
sourceMap: false,
}),
terserPlugin.terser(),
],
};
const outPath = `./src/${fileName}-processor.txt`;
const outputOptions = {
file: outPath,
format: 'esm',
};
const bundle = await rollup.rollup(inputOptions);
const { output } = await bundle.generate(outputOptions);
await fs.writeFile(outPath, output[0].code);
await bundle.close();
console.log(`${fileName} worklet rebuilt.`);
}
async function buildMain() {
console.log('building main library...');
const inputOptions = {
input: './src/index.ts',
plugins: [
stringPlugin.string({
include: './src/*.txt',
}),
typescriptPlugin(),
terserPlugin.terser(),
],
};
const version = process.env.npm_package_version;
const iifeOutput = {
file: `./docs/web-audio-peak-meter-${version}.min.js`,
format: 'iife',
name: 'webAudioPeakMeter',
exports: 'named',
sourcemap: true,
};
const esmOutput = {
file: './lib/index.esm.js',
format: 'esm',
sourcemap: true,
};
const cjsOutput = {
file: './lib/index.js',
format: 'cjs',
sourcemap: true,
};
try {
const bundle = await rollup.rollup(inputOptions);
await bundle.write(iifeOutput);
await bundle.write(esmOutput);
await bundle.write(cjsOutput);
await bundle.close();
console.log('Main library rebuilt.');
} catch (err) {
console.log(`problem creating bundle: ${err}`);
}
}
function escapeHtml(unsafe) {
// console.log(unsafe);
return unsafe
.toString()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function docHtml(content, markup, js) {
const dom = new jsdom.JSDOM(content);
const title = dom.window.document.querySelector('h1').textContent;
const version = process.env.npm_package_version;
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${title}</title>
<link href="/web-audio-peak-meter/docs.css" rel="stylesheet">
</head>
<body>
${content}
${
markup
? `<h2>Working Example</h2>
${markup}
<h2>HTML code</h2>
<pre class="code-block"><code>${escapeHtml(markup)}</code></pre>`
: ''
}
${
js
? `<h2>Javascript code</h2>
<pre class="code-block"><code>${escapeHtml(js)}</code></pre>
<script src="/web-audio-peak-meter/web-audio-peak-meter-${version}.min.js"></script>
<script>
${js}
</script>`
: ''
}
</body>
</html>
`;
}
async function buildDocExample(mdFile) {
const contents = await fs.readFile(path.join('examples', mdFile));
const { name } = path.parse(mdFile);
const htmlFile = `${name}.html`;
const htmlContents = await fs.readFile(path.join('examples', htmlFile));
const jsFile = `${name}.js`;
const jsContents = await fs.readFile(path.join('examples', jsFile));
const markup = marked.marked(contents.toString(), { smartypants: true });
const fullHtml = docHtml(markup, htmlContents, jsContents);
await fs.writeFile(path.join('docs', 'examples', `${name}.html`), fullHtml);
}
async function buildDocs() {
const examples = await fs.readdir('examples');
const mds = examples.filter((ex) => ex.endsWith('.md'));
await Promise.all(mds.map(buildDocExample));
const readme = await fs.readFile('README.md');
const readmeHtml = marked.marked(readme.toString(), { smartypants: true });
const subbed = readmeHtml.replace(/https:\/\/esonderegger.github.io/g, '');
const readmeDoc = docHtml(subbed);
await fs.writeFile(path.join('docs', 'index.html'), readmeDoc);
console.log('Docs rebuilt.');
}
async function buildAll() {
await buildWorklet('peak-sample');
await buildWorklet('true-peak');
await buildMain();
await buildDocs();
}
async function localDev() {
await buildAll();
const port = process.env.PORT || 6080;
const serve = serveStatic('./docs');
const server = http.createServer((req, res) => {
if (req.url.length < '/web-audio-peak-meter'.length) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('404: File not found');
return;
}
req.url = req.url.slice('/web-audio-peak-meter'.length);
const done = finalhandler(req, res);
serve(req, res, done);
});
const watcher = chokidar.watch(['./src', './examples']);
watcher.on('change', (filePath) => {
if (filePath.includes('peak-sample') && filePath.endsWith('.ts')) {
buildWorklet('peak-sample');
} else if (filePath.includes('true-peak') && filePath.endsWith('.ts')) {
buildWorklet('true-peak');
} else if (filePath.startsWith('examples/')) {
buildDocs();
} else {
buildMain();
}
});
server.listen(port);
console.log(`Now serving at http://localhost:${port}`);
process.on('SIGINT', async () => {
console.log('Stopping local server and watchers...');
await server.close();
await watcher.close();
console.log('Local server and watchers have been stopped');
});
}
if (require.main === module) {
if (process.argv.length > 2 && process.argv[2] === 'dev') {
localDev();
} else {
buildAll();
}
}