/
build-config.js
283 lines (242 loc) · 11.7 KB
/
build-config.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(function() {
"use strict";
module.exports = function(tools) {
var spawn = tools.process.spawn();
var exec = tools.process.exec({ log: console.log });
var ensureDirExists = tools.fs.ensureDirExists();
var react = require('react-tools');
var compressor = require('node-minify');
var path = require('path');
var fs = require('fs');
var clientModules = ["fora-lib-db-backend", "fora-lib-extensions-service", "fora-lib-request"];
var serverNpmModules = ["ceramic"];
var libModules = ["fora-lib-ui", "fora-lib-services", "fora-lib-models", "fora-lib-logger", "fora-lib-renderer", "fora-lib-sandbox",
"fora-lib-types-service", "fora-lib-initialize", "fora-lib-randomizer", "fora-lib-db-connector", "fora-lib-data-utils",
"fora-lib-router"];
var cmd_browserify = "../node_modules/.bin/browserify";
var cmd_regenerator = "../node_modules/.bin/regenerator";
var cmd_lessc = "../node_modules/.bin/lessc";
return function() {
var extensions = [];
/*
When the build starts, recreate the app directory
*/
this.onStart(function*() {
console.log("*****************************");
console.log("Started fora/www-client build");
console.log("*****************************");
this.state.start = Date.now();
yield* exec("rm -rf app");
yield* exec("mkdir app");
}, "client_build_start");
/*
Copy all files, except .less
*/
this.watch(
["src/www/css/*.*", "src/www/fonts/*.*", "src/www/images/*.*",
"src/www/js/*.js", "src/www/vendor/*.*"],
function*(filePath) {
var exlcudes = ["\\.less$", "\\.sh$"];
if (!exlcudes.some(function(ex) { return new RegExp(ex).test(path.extname(filePath)); })) {
var dest = filePath.replace(/^src\//, 'app/');
yield* ensureDirExists(dest);
yield* exec("cp " + filePath + " " + dest);
}
},
"client_files_copy"
);
/*
Watch ../server/config
*/
this.watch(
["../server/src/config/*.*"],
function*(filePath) {
var dest = filePath.replace(/^\.\.\/server\/src\//, 'app/www/js/');
yield* ensureDirExists(dest);
yield* exec("cp " + filePath + " " + dest);
},
"client_server_config_copy"
);
/*
Watch ../node_modules
*/
this.watch(
serverNpmModules.map(function(m) { return "../node_modules/" + m + "/*.*";}),
function*(filePath) {
//We dont want anything inside .git dir and inner node_modules
var mustCopy = !/\/\.git\//.test(filePath) &&
(filePath.split('/').filter(function(x) { return x === "node_modules"; }).length <= 1);
if (mustCopy) {
var dest = filePath.replace(/^\.\.\/node_modules\//, 'app/www/js/lib/');
yield* ensureDirExists(dest);
yield* exec("cp " + filePath + " " + dest);
}
},
"client_server_npm_copy"
);
/*
watch ../server/src/extensions/*.*
*/
this.watch(
["../server/app/extensions/*.*"],
function*(filePath) {
var dest = filePath.replace(/^\.\.\/server\/app\/extensions\//, 'app/www/js/extensions/');
yield* ensureDirExists(dest);
yield* exec("cp " + filePath + " " + dest);
extensions.push(dest);
},
"client_server_extensions_copy"
);
/*
watch ../server/src/lib/*.js
*/
this.watch(
libModules.map(function(m) { return "../server/app/lib/" + m + "/*.*";}),
function*(filePath) {
var dest = filePath.replace(/^\.\.\/server\/app\/lib\//, 'app/www/js/lib/');
yield* ensureDirExists(dest);
var parts = filePath.split("/");
if (parts[4] === "fora-lib-ui" || !fs.existsSync(dest))
yield* exec("cp " + filePath + " " + dest);
else
console.log("Skipping " + filePath + " -> " + dest);
},
"client_server_lib_copy",
["client_files_copy"]
);
/*
Compile less files. Schedule it at the end.
*/
var lessCompile = function*() { yield* exec(cmd_lessc + " --verbose src/www/css/main.less app/www/css/main.css"); };
this.watch(["src/www/css/*.less"], function*(filePath) {
yield* ensureDirExists('app/www/css/main.css');
this.queue(lessCompile);
}, "client_less_compile");
/*
Do regenerator transform, if not in es6 mode?
*/
if (!this.build.state.useES6) {
this.watch(
["app/www/js/*.js"],
function*(filePath) {
var result = yield* exec(cmd_regenerator + " " + filePath);
fs.writeFileSync(filePath, result);
},
"client_regenerator_transform",
[
"client_files_copy", "client_server_npm_copy", "client_server_lib_copy",
"client_server_config_copy", "client_server_extensions_copy"
]
);
}
/*
Hook the bundle step
*/
var bundleTrigger = this.build.state.useES6 ?
["client_files_copy", "client_server_npm_copy", "client_server_lib_copy", "client_server_config_copy", "client_server_extensions_copy"]
: ['client_regenerator_transform'];
this.watch(["app/www/js/*.js"], function*(filePath) {
this.queue("client_bundle_files");
}, "client_bundle_hook", bundleTrigger);
/*
1. Do facebook regenerator transform on all client side js files
2. Browserify bundle
*/
this.job(function*() {
extensions = extensions.filter(function(e) {
var parts = e.split("/");
return ["model", "definition", "web"].indexOf(parts[7]) > -1 &&
["index.js", "index.json"].indexOf(parts[8]) > -1;
});
console.log("Writing out app/www/js/extensions/extensions.json");
yield* ensureDirExists("app/www/js/extensions/extensions.json");
fs.writeFileSync("app/www/js/extensions/extensions.json", JSON.stringify(
extensions.map(function(e) {
return e.replace(/\/index\.json$|\/index\.js$/, '')
.replace(/\.json$|\.js$/, '')
.replace(/^app\/www\/js\//,'/');
})
));
if (!this.build.state.debugClient) {
var minify = function*(options) {
yield function(options) {
return function(cb) {
options.callback = cb;
new compressor.minify(options);
};
}(options);
};
console.log("Minifying CSS to lib.css");
yield* minify({
type: 'sqwish',
buffer: 1000 * 1024,
tempPath: '../temp/',
fileIn: [
'app/www/vendor/components/font-awesome/css/font-awesome.css',
'app/www/vendor/css/HINT.css',
'app/www/vendor/css/toggle-switch.css',
'app/www/vendor/components/medium-editor/css/medium-editor.css',
'app/www/vendor/components/medium-editor/css/themes/default.css',
],
fileOut: 'app/www/css/lib.css'
});
console.log("Minifying JS to vendor.js");
yield* minify({
type: 'no-compress',
buffer: 1000 * 2048,
tempPath: '../temp/',
fileIn: [
'app/www/vendor/js/co.js',
'app/www/vendor/js/markdown.min.js',
'app/www/vendor/js/setImmediate.js',
'app/www/vendor/js/regenerator-runtime.js',
'app/www/vendor/js/react.min.js'
],
fileOut: 'app/www/js/vendor.js'
});
}
console.log("Running browserify");
var cmdMakeLib = cmd_browserify + " " +
"-r ./app/www/vendor/js/shims/react.shim.js:react " +
"-r ./app/www/js/lib/jayschema:jayschema " +
"-r ./app/www/vendor/js/shims/co.shim.js:co " +
"-r ./app/www/vendor/js/shims/markdown.shim.js:markdown " +
"-r ./app/www/js/lib/path-to-regexp:path-to-regexp " +
"-x jayschema " +
serverNpmModules.map(function(m) {
return "-r ./app/www/js/lib/" + m + "/lib/" + m + ":" + m;
}).join(" ") + " " +
libModules.concat(clientModules).map(function(m) {
return "-r ./app/www/js/lib/" + m + ":" + m;
}).join(" ") + " " +
"> app/www/js/lib.js";
var cmdMakeBundle = cmd_browserify + " " +
" -x react -x co -x path-to-regexp -x jayschema -x markdown " +
serverNpmModules.concat(clientModules).concat(libModules).map(function(m) {
return "-x " + m;
}).join(" ") + " " +
extensions.map(function(x) {
//Take out .js, .json, /index.js and /index.json since require doesn't need it
var dest = x.replace(/\/index\.json$|\/index\.js$/, '').replace(/\.json$|\.js$/, '');
return "-r ./" + x + ":" + dest.replace(/^app\/www\/js\//,'/');
}).join(" ") + " " +
"-r ./app/www/js/extensions/extensions.json:/extensions/models " +
"./app/www/js/app " +
"> app/www/js/bundle.js";
if (this.build.state.debugClient) {
cmdMakeLib += " --debug";
cmdMakeBundle += " --debug";
}
yield* exec(cmdMakeLib);
yield* exec(cmdMakeBundle);
}, "client_bundle_files");
/*
If debug, include all unminified js files. Otherwise minify.
Finally, go back and change debug.hbs
*/
this.onComplete(function*() {
this.state.end = Date.now();
}, "client_build_complete");
};
};
})();