-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefork.js
39 lines (34 loc) · 1.38 KB
/
makefork.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
const fs = require('fs');
const babel = require('babel-core');
const UglifyJS = require('uglify-js');
function babelResult(code) {
return babel.transform(code, {presets: ['es2015']}).code;
}
function uglifyResult(code) {
return UglifyJS.minify(code, {fromString: true}).code;
}
function makeWorkerScript(shiori_class_name, shiori_loader_id, shiori_module) {
const worker_routine = catServerRoutine(shiori_class_name, shiori_module);
return babelResult(
fs.readFileSync(require.resolve('./rc/fork.js'), {encoding: 'utf8'})
.replace(/SHIORI_CLASS/g, shiori_class_name)
.replace(/SHIORI_LOADER_ID/g, shiori_loader_id)
.replace(/WORKER_ROUTINE/g, worker_routine)
);
}
function catServerRoutine(shiori_class_name, shiori_module) {
const requires = [
[shiori_class_name, shiori_module],
['NativeShiori', 'nativeshiori'],
['NativeShioriEncode', 'nativeshiori/nativeshiori-encode'],
['WorkerServer', 'worker-client-server/WorkerServer'],
['SingleFileWorker', 'single-file-worker'],
['Promise', 'bluebird'],
].map((module) => `const ${module[0]} = require('${module[1]}');\n`).join("");
const server_code = babelResult(
fs.readFileSync(require.resolve('./rc/server.js'), {encoding: 'utf8'})
.replace(/SHIORI_CLASS/g, shiori_class_name)
);
return uglifyResult(requires + server_code);
}
module.exports = {catServerRoutine, makeWorkerScript};