/
bundle.js
68 lines (62 loc) · 2.54 KB
/
bundle.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
import babel from 'rollup-plugin-babel';
import { fs } from 'memfs';
import { join } from 'path';
import { readFileSync } from 'fs';
import replace from 'rollup-plugin-replace';
import webpack from 'webpack';
import webpackConfig from '../webpack/config';
const workerFile = readFileSync('src/worker/worker.ts', 'utf-8');
const result = /export\sconst\sworker\s=\s`(?<workerString>.*)`;/g.exec(workerFile);
if (result === null) {
throw new Error('The worker file could not be parsed.');
}
const workerString = result.groups.workerString;
export default new Promise((resolve, reject) => { // eslint-disable-line import/no-default-export
const compiler = webpack(webpackConfig);
compiler.outputFileSystem = { ...fs, join };
compiler.run((err, stats) => {
if (stats.hasErrors() || stats.hasWarnings()) {
reject(new Error(stats.toString({ errorDetails: true, warnings: true })));
} else {
const transpiledWorkerString = fs
.readFileSync('/worker.js', 'utf-8')
.replace(/\\/g, '\\\\')
.replace(/\${/g, '\\${');
resolve({
input: 'build/es2018/module.js',
output: {
file: 'build/es5/bundle.js',
format: 'umd',
name: 'webAudioBeatDetector'
},
plugins: [
replace({
delimiters: [ '`', '`' ],
include: 'build/es2018/worker/worker.js',
values: {
// V8 does only accept substrings with a maximum length of 32767 characters. Otherwise it throws a SyntaxError.
[ workerString.slice(0, 32767) ]: `\`${ transpiledWorkerString }\``,
[ workerString.slice(32767) ]: ''
}
}),
babel({
exclude: 'node_modules/**',
plugins: [
'@babel/plugin-external-helpers',
'@babel/plugin-transform-runtime'
],
presets: [
[
'@babel/preset-env',
{
modules: false
}
]
],
runtimeHelpers: true
})
]
});
}
});
});