forked from YahooArchive/mojito
/
deploy.server.js
192 lines (156 loc) · 6.48 KB
/
deploy.server.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
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true, stupid:true*/
/*global YUI*/
/**
* @module ActionContextAddon
*/
YUI.add('mojito-deploy-addon', function(Y, NAME) {
var fs = require('fs'),
minify;
// TODO: [Issue 64] improve this, it's a poor man's minify.
// a. build minification into static handler?
// b. build minification into prod-build script ?
// c. build minification into server-start?
minify = function(str) {
// Remove comment blocks /* ... */ and
// remove white space at the start of lines
return str.replace(/\/\*[\s\S]*?\*\//g, '').
replace(/^[ \t\r\n]+/gm, '');
};
/**
* <strong>Access point:</strong> <em>ac.deploy.*</em>
* Provides ability to create client runtime deployment HTML
* @class Deploy.server
*/
function Addon(command, adapter, ac) {
this.instance = command.instance;
this.scripts = {};
this.ac = ac;
this.rs = null;
}
Addon.prototype = {
namespace: 'deploy',
/**
* Declaration of store requirement.
* @method setStore
* @private
* @param {ResourceStore} rs The resource store instance.
*/
setStore: function(rs) {
this.rs = rs;
},
/**
* Builds up the browser Mojito runtime.
* @method constructMojitoClientRuntime
* @param {AssetHandler} assetHandler asset handler used to add scripts
* to the DOM under construction.
* @param {object} binderMap information about the binders that will be
* deployed to the client.
*/
constructMojitoClientRuntime: function(assetHandler, binderMap) {
//Y.log('Constructing Mojito client runtime', 'debug', NAME);
var store = this.rs,
contextServer = this.ac.context,
appConfigServer = store.getAppConfig(contextServer),
contextClient,
appConfigClient,
yuiConfig = {},
fwConfig,
yuiConfigEscaped,
yuiConfigStr,
yuiModules,
yuiCombo,
yuiJsUrls = [],
yuiCssUrls = [],
viewId,
binder,
i,
id,
clientConfig = {},
clientConfigEscaped,
clientConfigStr,
initialModuleList = {},
initializer, // script for YUI initialization
type,
module,
path,
pathToRoot;
contextClient = Y.mojito.util.copy(contextServer);
contextClient.runtime = 'client';
appConfigClient = store.getAppConfig(contextClient);
clientConfig.context = contextClient;
appConfigClient.yui = appConfigClient.yui || {};
appConfigClient.yui.config = appConfigClient.yui.config || {};
yuiConfig = appConfigClient.yui.config;
yuiConfig.lang = contextServer.lang; // same as contextClient.lang
// If we have a "base" for YUI use it
if (appConfigClient.yui.base) {
yuiConfig.base = appConfigClient.yui.base;
yuiConfig.combine = !!appConfigClient.yui.combine;
} else {
yuiConfig.combine = true;
yuiConfig.comboBase = "/combo?";
yuiConfig.root = "";
}
clientConfig.store = store.serializeClientStore(contextClient);
// Set the YUI URL to use on the client (This has to be done
// before any other scripts are added)
assetHandler.addAsset('js', 'top',
(appConfigClient.yui.url ||
'/combo?yui-base/yui-base.js&loader-base/loader-base.js&loader-app-base.js'));
// defaults to true if missing
if (!yuiConfig.hasOwnProperty('fetchCSS') || yuiConfig.fetchCSS) {
for (i = 0; i < yuiCssUrls.length; i += 1) {
assetHandler.addCss(yuiCssUrls[i], 'top');
}
}
// adding the default module for the Y.use statement in the client
initialModuleList['mojito-client'] = true;
// add binders' dependencies
for (viewId in binderMap) {
if (binderMap.hasOwnProperty(viewId)) {
if (binderMap[viewId].name) {
initialModuleList[binderMap[viewId].name] = true;
}
}
}
clientConfig.binderMap = binderMap;
// TODO: [Issue 65] Split the app config in to server client
// sections.
// we need the app config on the client for log levels (at least)
clientConfig.appConfig = clientConfig.store.appConfig;
// this is mainly used by html5app
pathToRoot = this.ac.http.getHeader('x-mojito-build-path-to-root');
if (pathToRoot) {
clientConfig.pathToRoot = pathToRoot;
}
clientConfig.routes = this.ac.url.getRouteMaker().getComputedRoutes();
delete clientConfig.store;
// Unicode escape the various strings in the config data to help
// fight against possible script injection attacks.
yuiConfigEscaped = Y.mojito.util.cleanse(yuiConfig);
yuiConfigStr = JSON.stringify(yuiConfigEscaped);
clientConfigEscaped = Y.mojito.util.cleanse(clientConfig);
clientConfigStr = JSON.stringify(clientConfigEscaped);
initialModuleList = "'" + Y.Object.keys(initialModuleList).join("','") + "'";
initializer = '<script type="text/javascript">\n' +
' YUI.applyConfig(' + yuiConfigStr + ');\n' +
' YUI().use(' + initialModuleList + ', function(Y) {\n' +
' window.YMojito = { client: new Y.mojito.Client(' +
clientConfigStr + ') };\n' +
' });\n' +
'</script>\n';
// Add the boot script
assetHandler.addAsset('blob', 'bottom', initializer);
}
};
Y.namespace('mojito.addons.ac').deploy = Addon;
}, '0.1.0', {requires: [
'mojito-util',
'mojito-http-addon',
'mojito-url-addon'
]});