This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
115 lines (106 loc) · 2.6 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
const browserify = require('browserify');
const UglifyJS = require('uglify-js');
const fs = require('fs-extra');
const babel = require('@babel/core');
function nodeToBrowser(file, ignore) {
return new Promise((resolve) => {
const b = browserify();
b.add(file);
if (ignore) {
b.ignore(ignore);
}
b.bundle((err, buf) => {
if (err) {
console.log(err);
}
resolve(buf.toString());
});
});
}
function uglyContent(content) {
const result = UglifyJS.minify(content, {
compress: true
});
if (result.error) {
throw result.error;
}
return result.code;
}
function uglyFile(file) {
return fs.readFile(file, 'utf8')
.then((content) => uglyContent(content));
}
function babelify(code) {
return new Promise((resolve, reject) => {
const cnf = {
'presets': [[
'@babel/preset-env'
]],
'plugins': ['minify-mangle-names']
};
babel.transform(code, cnf, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result.code);
}
});
});
}
function compileDatatype() {
return nodeToBrowser('lib/datatype-lib.js')
.then((code) => babelify(code))
.then((content) => uglyContent(content));
}
function compileRaml2obj() {
return nodeToBrowser('node_modules/raml2obj/browser.js', 'datatype-expansion')
.then((code) => babelify(code))
.then((content) => uglyContent(content));
}
function compileWorker() {
return fs.readFile('lib/normalize-worker.js', 'utf8')
.then((content) => babelify(content))
.then((content) => uglyContent(content));
}
function build() {
let result;
return uglyFile('lib/polyfills.js')
.then((data) => {
result = data;
return uglyFile('bower_components/promise-polyfill/Promise.js');
})
.then((data) => {
result += '\n' + data;
return compileDatatype();
})
.then((data) => {
result += '\n' + data;
return compileRaml2obj();
})
.then((data) => {
result += '\n' + data;
return uglyFile('lib/init-script.js');
})
.then((data) => {
result += '\n' + data;
return uglyFile('bower_components/promise-polyfill/Promise-Statics.js');
})
.then((data) => {
result += '\n' + data;
return compileWorker();
})
.then((data) => {
result += '\n' + data;
return fs.readFile('raml-json-enhance-template.html', 'utf8');
})
.then((content) => {
content = content.replace('{{RAML2OBJ_CONTENT}}', result);
return fs.writeFile('raml-json-enhance.html', content, 'utf8');
});
}
build()
.then((result) => {
console.log(result);
console.log('Completed');
})
.catch((cause) => console.error(cause));