-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
228 lines (202 loc) · 6.04 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var fs = require('fs-extra'),
child_process = require('child_process'),
build_dir = 'build';
check_folders();
build_automation();
build_assets();
build_js();
build_admin_index();
build_dev_index();
build_tool_used_css_index();
build_dev_used_css_index();
build_tool_critical_css_index();
build_tool_static_html_index();
build_dev_critical_css_index();
dist_index();
function check_folders() {
fs.ensureDirSync(build_dir);
}
function build_admin_index() {
fs.copySync('dtm/dtm.index.html', build_dir + '/index.html');
}
function build_assets() {
var _source = 'src/app/assets/css',
_dest = build_dir + '/assets/css';
fs.ensureDirSync(_dest);
// dtm admin
fs.copySync(_source + '/bootstrap.css', _dest + '/bootstrap.css');
// app
fconcat(
[_source + '/bootstrap.css', _source + '/app.css'],
_dest + '/style.css'
);
}
function build_automation() {
fcopyDir('src/app/automations', build_dir + '/automations');
}
function build_dev_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/dev.index.html',
substitutions: [
{
before: '<!-- @script -->',
after: '<script src="js/app.critical.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/style.css" rel="stylesheet">'
}
]
});
}
function build_js() {
shell('./node_modules/.bin/rollup -c --environment build:production');
}
function build_tool_used_css_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/tool.used_css.index.html',
substitutions: [
{
before: '<!-- @script -->',
after:
'<script src="js/app.critical.js"></script>' +
'<script src="js/tool.used_css.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/style.css" rel="stylesheet">'
}
]
});
}
function build_tool_critical_css_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/tool.critical_css.index.html',
substitutions: [
{
before: '<!-- @script -->',
after:
'<script src="js/app.critical.js"></script>' +
'<script src="js/tool.critical_css.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/used.css" rel="stylesheet">'
}
]
});
}
function build_dev_critical_css_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/dev.critical_css.index.html',
substitutions: [
{
before: '<!-- @script -->',
after: '<script src="js/app.critical.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/critical.css" rel="stylesheet">'
}
]
});
}
function build_tool_static_html_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/tool.static_html.index.html',
substitutions: [
{
before: '<!-- @script -->',
after:
'<script src="js/app.critical.js"></script>' +
'<script src="js/tool.static_html.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/used.css" rel="stylesheet">'
}
]
});
}
function dist_index() {
var _critical_js = build_dir + '/js/app.critical.js',
_critical_css = build_dir + '/assets/css/critical.css',
_static_html = build_dir + '/app.html';
if (fs.existsSync(_critical_js) === false) return;
if (fs.existsSync(_critical_css) === false) return;
if (fs.existsSync(_static_html) === false) return;
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/dist.index.html',
substitutions: [
{
before: '<div id="app" />',
after: '<div id="app">' + fread(_static_html) + '</div>'
},
{
before: '<!-- @script -->',
after: '<script>' + fread(_critical_js) + '</script>'
},
{
before: '<!-- @css -->',
after:
'<style>' +
fread(_critical_css) +
'</style>' +
'<link href="assets/css/not_critical.css" rel="stylesheet">'
}
]
});
}
function build_dev_used_css_index() {
fmultiSubstitutions({
input: 'index.html',
output: build_dir + '/dev.used_css.index.html',
substitutions: [
{
before: '<!-- @script -->',
after: '<script src="js/app.critical.js"></script>'
},
{
before: '<!-- @css -->',
after: '<link href="assets/css/used.css" rel="stylesheet">'
}
]
});
}
function fconcat(sources_, destination_) {
var _concat = '';
sources_.forEach(function(source_) {
_concat += fread(source_);
});
fwrite(destination_, _concat);
}
function fcopyDir(source_, destination_) {
fs.removeSync(destination_);
fs.copySync(source_, destination_);
}
function fmultiSubstitutions(in_) {
var _text = fread(in_.input);
in_.substitutions.forEach(function(v_) {
_text = _text.replace(v_.before, v_.after);
});
fwrite(in_.output, _text);
}
function fread(path_) {
return fs.readFileSync(path_).toString();
}
function fwrite(path_, string_) {
fs.writeFileSync(path_, string_, function(err_) {
if (err_) {
return console.log(err_);
}
});
}
function shell(cmd_) {
child_process.execSync(cmd_, { stdio: 'inherit' });
}