Permalink
Browse files

IMPROVED - Optimized loading of client js files ; allowing les redund…

…ance and faster build process
  • Loading branch information...
1 parent 949680a commit d99a1cac1041773eeb8e6f22807256cde92bf583 @glesperance committed Jul 30, 2013
Showing with 59 additions and 13 deletions.
  1. +34 −2 lib/client/client.js
  2. +24 −10 lib/controller.js
  3. +1 −1 lib/loader.js
View
@@ -14,9 +14,11 @@ var async = require('async')
, rimraf = require('rimraf')
, wrench = require('wrench')
, util = require('util')
+ , _ = require('underscore')
;
var asyncFs = require('../util/async_fs')
+ , dadt = require('../util/dadt')
, FileEventEmitter = require('../util/file_event_emitter')
@@ -43,6 +45,30 @@ var asyncFs = require('../util/async_fs')
, STATIC_FILES_CACHE_LIFETIME = config.STATIC_FILES_CACHE_LIFETIME
;
+var CLIENT_FILE_REGEXP = /(([^.]+)(\.[^.]+)*)?.client\.js$/
+
+function watchAndBuildClientFilesInventory(routesDadt, jsFilesPath, callback) {
+ asyncFs.forEachFile(jsFilesPath, function (file, callback) {
+ var matches = CLIENT_FILE_REGEXP.exec(file)
+
+ if (!matches)
+ return callback(null)
+
+ var controlerName = matches[1]
+ var controlerNameParts = controlerName.split('.')
+
+ var controllerDatd = _.reduce(controlerNameParts, function (currentNode, part) {
+ currentNode.setIfNone(part, dadt.createNode())
+ return currentNode.get(part)
+ }, routesDadt)
+
+ controllerDatd.set('_client', true)
+
+ callback(null)
+ }
+ , callback)
+}
+
/******************************************************************************
* Compile client views located at `src_path` and saves them in `dst_path`.
*
@@ -372,7 +398,7 @@ function optimizeDir(dir_path, dst_tmpdir, options_tmpdir, callback) {
return;
}
- var suffix = '_client.js';
+ var suffix = '.client.js';
if (filename.substr(-suffix.length) === suffix) {
module_name = filename.substr(0, filename.length - 3);
@@ -504,7 +530,8 @@ exports.setup = function setup_client() {
, app = args.shift()
, middlewares = args.shift()
-
+ , routes_dadt = args.shift()
+
, client = args.shift()
, callback = args.pop()
@@ -679,6 +706,8 @@ exports.setup = function setup_client() {
callback(null);
}
+
+ , async.apply(watchAndBuildClientFilesInventory, routes_dadt, js_dir_path)
]
, callback
@@ -746,9 +775,12 @@ exports.setup = function setup_client() {
app.use('/js', express.static(client_views_tmpdir.path));
+
+
async.parallel([
async.apply(setupDevModeViews, js_dir_path, client_views_tmpdir)
, async.apply(watchAndExportConfig, require_js_config_path)
+ , async.apply(watchAndBuildClientFilesInventory, routes_dadt, js_dir_path)
], doneCallback);
}
View
@@ -20,9 +20,10 @@ var asyncFs = require('./util/async_fs')
/******************************************************************************
* Rocket template name space
*/
-var rocket_tmpl = {};
- rocket_tmpl.header = function() {
+function RocketTemplate(controllerInfo) {
+
+ this.header = function() {
var args = Array.prototype.slice.call(arguments)
, controller_name = args.shift().replace(/[.,-;\s]+/g, '_')
@@ -36,32 +37,42 @@ var rocket_tmpl = {};
};
- rocket_tmpl.footer = function() {
+ this.footer = function() {
var args = Array.prototype.slice.call(arguments)
- , controller_name = args.shift().replace(/[.,-;\s]+/g, '_')
+ , controller_name = args.shift()
, footer = []
, require_configuration = global.require_configuration || {}
;
+ var client_file = (function (controllerInfo) {
+ if (controllerInfo.get('_client'))
+ return controllerInfo.get('name') + '.client'
+ else if (controllerInfo.parent)
+ return arguments.callee(controllerInfo.parent)
+ else
+ return null
+ }(controllerInfo))
+
footer.push("<script>");
footer.push("var _b = " + JSON.stringify(args) + "; ");
footer.push("var require = " + JSON.stringify(require_configuration) + ";");
footer.push("</script>");
footer.push("<script src=\"//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js\"></script>");
footer.push("<script>window.define||document.write('<script src=\"/js/rocket/vendors/require.min.js\"><\\/script>')</script>")
- if (process.env['NODE_ENV'] === 'production')
- footer.push("<script src=\"/js/" + controller_name + "_client.js?" + require_configuration.urlArgs + "\"></script>")
-
- footer.push("<script>require(['" + controller_name + "_client" + "'], function(m) { m && m.init && m.init.apply(m, _b); })</script>")
-
+ if (client_file) {
+ if (process.env['NODE_ENV'] === 'production')
+ footer.push("<script src=\"/js/" + client_file + ".js?" + require_configuration.urlArgs + "\"></script>")
+
+ footer.push("<script>require(['" + client_file + "'], function(m) { m && m.init && m.init.apply(m, _b); })</script>")
+ }
return footer.join('\n');
-
};
+}
/******************************************************************************
* Destroy all the routes associated with a given controller
@@ -117,6 +128,8 @@ function buildWrapper(controller_info, method_func) {
;
res._boot_options || (res._boot_options = {});
+
+ var rocket_tmpl = new RocketTemplate(controller_info)
//check if we have a view & if that we do not have an XHR-Request.
//If the view is missing or if we have an XHR-Request, skip view rendering.
@@ -377,6 +390,7 @@ function setupController(app, routes_dadt, controller_file, matches, callback) {
, controller_path = controller_file.path
, reload
+ , controllerInfo
;
View
@@ -73,7 +73,7 @@ exports.createServer = function createServer_rocket() {
}
async.forEachSeries(
- [ [ CLIENT_DIR_NAME , async.apply(client.setup, app, middlewares) ]
+ [ [ CLIENT_DIR_NAME , async.apply(client.setup, app, middlewares, routes) ]
, [ CONTROLLERS_DIR_NAME , async.apply(controller.setup, app, routes) ]

0 comments on commit d99a1ca

Please sign in to comment.