-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.js
421 lines (390 loc) · 14.1 KB
/
apps.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
var path = require('path');
var fs = require('fs');
var Q = require('q');
var _ = require('lodash');
var through = require('through');
var jsdepLibrary = require('jsDependencyAnalizer/library');
var jsdepExt = require('./jsdep.external');
var BuildLib = require('./lib/build');
var packageViewsPath = require.resolve('derby/lib/_packageViews');
var viewsPath = require.resolve('derby/lib/_views');
var derby;
function buildJsdep(options) {
var config = options.config;
var configPath = options.path;
configPath = path.normalize(configPath);
var frontDir = path.dirname(configPath);
if (!config) {
options.config = config = require(configPath);
}
if (!config.$$jsdep) {
config.general.frontDir = frontDir;
if (!('root' in config.general)) {
config.general.root = '';
}
if (!('site' in config.general)) {
config.general.site = config.general.root;
}
if (!('compiled' in config.general)) {
config.general.compiled = path.join(config.general.site, 'compiled');
}
if (!('resources' in config.general)) {
config.general.resources = path.join(config.general.site, 'resources');
}
var rootDir = path.resolve(frontDir, config.general.root);
config.general.appUses = config.use && config.use.map(function(x) {
return path.join(rootDir, x);
});
var jsdepConfig = jsdepExt.jsdep(config);
var buildlib = config.$$buildlib = new BuildLib({
config: _.defaults(config.build || {}, require('./croc.defaults.json').build),
apps: _.zipObject(
_.pluck(config.apps, 'path')
.concat(config.use || [])
.map(function(x) { return path.join(rootDir, x); })
.concat(path.join(__dirname, 'source/croc'))
.map(function(x) { return [path.basename(x), x]; })
),
frontDir: frontDir,
compiledDir: path.resolve(frontDir, config.general.compiled),
publicDir: path.resolve(frontDir, config.general.site),
projectDir: rootDir,
resourcesDir: config.general.rendererMode && !config.general.skipResources ?
path.resolve(frontDir, config.general.resources) : null
});
config.$$jsdep = buildlib.jsDependencies({
path: configPath || process.cwd(),
config: jsdepConfig,
alltypes: true,
abspath: true
}).then(function(targets) {
targets.forEach(function(target) {
target.buildlib = buildlib;
});
return targets;
});
}
return config.$$jsdep;
}
/**
* @param options
* @param options.config
* @param [options.path]
* @param options.name
* @param callback
*/
exports.resolveApp = function(options, callback) {
buildJsdep(options).then(function(targets) {
targets.some(function(target) {
if (target.target === 'app:' + options.name) {
callback(null, resolveApp(target));
return true;
}
});
}).done();
};
/**
* @param options
* @param options.config
* @param [options.path]
* @param callback
*/
exports.resolveApps = function(options, callback) {
buildJsdep(options).then(function(targets) {
return targets
.filter(function(target) {
return _.startsWith(target.target, 'app:');
})
.map(function(target) {
return resolveApp(target);
});
}).nodeify(callback);
};
var apps = {};
function resolveApp(target) {
target = target.targetObj;
if (target.rendererMode) {
process.env.DERBY_RENDERER = true;
}
derby = require('./index');
var pathToApp = jsdepLibrary.normalizePath(target, target.path);
pathToApp = path.resolve(pathToApp);
var name = path.basename(pathToApp);
if (apps[name]) {
return apps[name];
}
var appPath = pathToApp + '/app';
var app = apps[name] = derby.createApp(name);
_.assign(app, {
rootPath: target.root,
sitePath: target.site,
path: pathToApp,
uses: target.appUses,
resourcePath: !target.rendererMode ? '' :
'/' + path.relative(target.site, path.resolve(target.frontDir, target.resources))
});
if (!global.Stm) {
global.Stm = {};
}
if (!global.Stm.env) {
global.Stm.env = {};
}
global.Stm.env.resourcePath = app.resourcePath;
app.loadViews(appPath);
var appEntry = appPath + '/index.js';
if (fs.existsSync(appEntry)) {
app.entry = appEntry;
}
var packagesPath = appPath + '/packages';
app.packages = _.mapValues(target.packages, function(x, pack) {
var packPath = packagesPath + '/' + pack;
var packApp = derby.createApp(name + ':' + pack);
_.assign(packApp, {
isPackage: true,
coreApp: app,
proto: app.proto,
packageName: pack,
path: packPath,
resourcePath: app.resourcePath
});
packApp.Page.prototype = app.Page.prototype;
//todo лишний парсинг представлений ядра
packApp.loadViews(appPath, null, {serverOnly: true});
packApp.loadViews(packPath);
var packEntry = packPath + '/index.js';
if (fs.existsSync(packEntry)) {
packApp.entry = packEntry;
}
return packApp;
});
return app;
}
var stylesQueue = BuildLib.getTasksQueue(1, true);
function queueProcessStyles(options) {
if (!_.isEmpty(options.files)) {
var app = options.app;
var buildlib = options.target.buildlib;
app.styleUrl = '/' + path.relative(options.target.targetObj.site,
buildlib.getCompiledOutput(app.name, (Stm.env.ldevice !== 'desktop' ? Stm.env.ldevice + '.' : '') + 'css'));
return stylesQueue.addTask(function() {
return buildlib.processStyles(options.files, {
name: app.name,
map: !derby.util.isProduction,
skipOptimizations: !derby.util.isProduction,
//version: version,
//deferWrite: deferWrite,
variant: 'all',
device: Stm.env.ldevice
//notOptimized: notOptimized
});
});
}
return Q();
}
var jsBundlesQueue = BuildLib.getTasksQueue(10);
function createJsBundle(options) {
var buildlib = options.target.buildlib;
var app = options.app;
var output = buildlib.getCompiledOutput(app.name, 'js');
app.scriptUrl = '/' + path.relative(options.target.targetObj.site, output);
return jsBundlesQueue.addTask(function() {
return options.target.buildlib.createBundle({
name: app.name,
add: options.files.filter(function(x) {
return x.indexOf(app.path + '/app/index.') !== 0;
}),
external: options.external && _.without(options.external, packageViewsPath),
//debugVersion: true,
write: !derby.util.isProduction,
onFiles: options.onFiles,
//deferWrite: deferWrite,
core: !options.isPackage,
target: options.target,
map: !derby.util.isProduction,
onBundle: function(bundle) {
var viewsFilename = app.isPackage ? packageViewsPath : viewsPath;
bundle.require(require.resolve('derby'), {expose: 'derby'});
bundle.require(require.resolve(__dirname), {expose: 'crocodile-js'});
if (app.isPackage) {
bundle.require(viewsFilename, {expose: app.isPackage ? '_packageViews' : '_views'});
}
else {
var modeFilename = 'derby/lib/' + (process.env.DERBY_RENDERER ? 'rendererMode.js' : 'normalMode.js');
bundle.require(require.resolve(modeFilename), {expose: 'derbyMode'});
bundle.require(packageViewsPath, {expose: '_packageViews'});
}
if (app.entry) {
bundle.require(app.entry, {expose: app.isPackage ? 'packEntry' : 'appEntry'});
}
// Hack to inject the views script into the Browserify bundle by replacing
// the empty _views.js file with the generated source
bundle.transform(function(filename) {
var content;
if (filename === viewsFilename) {
content = app._viewsSource({minify: derby.util.isProduction});
}
return !content ? through() : through(
function write(curData) {},
function end() {
this.queue(content);
this.queue(null);
}
);
});
app.emit('bundle', bundle);
if (options.store) {
options.store.emit('bundle', bundle);
}
}
});
});
}
function compileApp(options) {
var app = options.app;
if (options.static) {
app.static = true;
}
var target = options.target;
var js = [];
var views = [];
var css = [];
var controllers = options.controllers ? options.controllers.concat() : [];
var files = options.files || target.files;
files.forEach(function(file) {
var ext = path.extname(file);
var fileDesc = target.filesHash[file];
var symbol = fileDesc.symbols[0];
if (ext === '.js' || ext === '.coffee') {
if (!fileDesc.meta || !fileDesc.meta.bower && fileDesc.meta.server !== false) {
require(file);
if (global.croc && croc.Class) {
var cls = croc.Class.getClass(symbol);
if (cls) {
if (!target.targetObj.rendererMode && croc.Controller &&
croc.Class.isSubClass(cls, croc.Controller) && !cls.CLIENT_ONLY) {
controllers.push(cls);
}
}
}
}
js.push(file);
}
else if (ext === '.wtpl' || ext === '.wjade') {
views.push(file);
}
else if (ext === '.css') {
css.push(file);
}
});
app._watchBundle(files);
app.views.app = app;
function addView(file, serverOnly) {
var cls;
var symbol = target.filesHash[file].symbols[0];
if (global.croc && croc.Class) {
cls = croc.Class.getClass(symbol);
app._widgetBase = cls ? cls.baseclass.classname : 'croc.cmp.Widget';
}
app._widgetCls = symbol;
var options = {widgetTemplate: true};
if (serverOnly) {
options.serverOnly = true;
}
app.loadViews(path.normalize(file), symbol, options);
app.component(symbol, cls || derby.getStubWidget());
delete app._widgetCls;
delete app._widgetBase;
}
//todo optimize (don't compile view)
if (options.coreViews) {
options.coreViews.forEach(function(file) { addView(file, true); });
}
views.forEach(function(file) { addView(file); });
queueProcessStyles({
app: app,
files: css,
target: target
});
var deferred = Q.defer();
var coreJsPromise = options.static ? deferred.promise : createJsBundle({
store: options.store,
app: app,
files: js,
external: options.coreJs,
onFiles: !app.isPackage && function(coreFiles) {
var promises = _(target.packages).map(function(files, name) {
return compileApp({
store: options.store,
app: app.packages[name],
target: target,
files: files,
coreJs: coreFiles,
coreViews: views,
controllers: controllers
});
}).compact().value().concat(coreJsPromise);
Q.all(promises).then(deferred.resolve).done();
},
target: target
});
if (options.static) {
deferred.resolve();
}
return (app.isPackage ? coreJsPromise : deferred.promise).then(function() {
app._initCroc(controllers);
function onModel(model) {
model.set('_page.device', Stm.env.device);
model.set('_page.ldevice', Stm.env.ldevice);
app.model = model;
if (app.isPackage && app.coreApp.entry) {
require(app.coreApp.entry)(app);
}
if (app.entry) {
require(app.entry)(app);
}
}
if (app.model) {
onModel(app.model);
}
else {
app.on('model', onModel);
}
return app;
});
}
/**
* @param {Object} options
* @param {Object} [options.config]
* @param {string} [options.path]
* @param {Store} [options.store]
* @param {boolean} [options.prototype]
* @param {boolean} [options.static]
* @param callback
*/
exports.buildApps = function(options, callback) {
var jsdepDef = buildJsdep(options);
var buildlib = options.config.$$buildlib;
if (options.prototype) {
stylesQueue.start(true);
}
Q.all([
jsdepDef,
buildlib.clean().then(function() {
return buildlib.copyResources(options.config.general.skipResources);
})
])
.spread(function(targets) {
return Q.all(targets.map(function(target) {
return compileApp({
store: options.store,
app: resolveApp(target),
target: target,
'static': options.static
});
}));
})
.then(function(apps) {
return stylesQueue.start().thenResolve(apps);
})
.nodeify(callback);
};