Skip to content

Commit

Permalink
IMPROVED - Optimized loading of client js files ; allowing les redund…
Browse files Browse the repository at this point in the history
…ance and faster build process
  • Loading branch information
Gabriel Lesperance committed Aug 7, 2013
1 parent 949680a commit d99a1ca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
36 changes: 34 additions & 2 deletions lib/client/client.js
Expand Up @@ -14,9 +14,11 @@ var async = require('async')
, rimraf = require('rimraf') , rimraf = require('rimraf')
, wrench = require('wrench') , wrench = require('wrench')
, util = require('util') , util = require('util')
, _ = require('underscore')
; ;


var asyncFs = require('../util/async_fs') var asyncFs = require('../util/async_fs')
, dadt = require('../util/dadt')


, FileEventEmitter = require('../util/file_event_emitter') , FileEventEmitter = require('../util/file_event_emitter')


Expand All @@ -43,6 +45,30 @@ var asyncFs = require('../util/async_fs')
, STATIC_FILES_CACHE_LIFETIME = config.STATIC_FILES_CACHE_LIFETIME , 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`. * Compile client views located at `src_path` and saves them in `dst_path`.
* *
Expand Down Expand Up @@ -372,7 +398,7 @@ function optimizeDir(dir_path, dst_tmpdir, options_tmpdir, callback) {
return; return;
} }


var suffix = '_client.js'; var suffix = '.client.js';


if (filename.substr(-suffix.length) === suffix) { if (filename.substr(-suffix.length) === suffix) {
module_name = filename.substr(0, filename.length - 3); module_name = filename.substr(0, filename.length - 3);
Expand Down Expand Up @@ -504,7 +530,8 @@ exports.setup = function setup_client() {


, app = args.shift() , app = args.shift()
, middlewares = args.shift() , middlewares = args.shift()

, routes_dadt = args.shift()

, client = args.shift() , client = args.shift()


, callback = args.pop() , callback = args.pop()
Expand Down Expand Up @@ -679,6 +706,8 @@ exports.setup = function setup_client() {


callback(null); callback(null);
} }

, async.apply(watchAndBuildClientFilesInventory, routes_dadt, js_dir_path)


] ]
, callback , callback
Expand Down Expand Up @@ -746,9 +775,12 @@ exports.setup = function setup_client() {


app.use('/js', express.static(client_views_tmpdir.path)); app.use('/js', express.static(client_views_tmpdir.path));




async.parallel([ async.parallel([
async.apply(setupDevModeViews, js_dir_path, client_views_tmpdir) async.apply(setupDevModeViews, js_dir_path, client_views_tmpdir)
, async.apply(watchAndExportConfig, require_js_config_path) , async.apply(watchAndExportConfig, require_js_config_path)
, async.apply(watchAndBuildClientFilesInventory, routes_dadt, js_dir_path)
], doneCallback); ], doneCallback);


} }
Expand Down
34 changes: 24 additions & 10 deletions lib/controller.js
Expand Up @@ -20,9 +20,10 @@ var asyncFs = require('./util/async_fs')
/****************************************************************************** /******************************************************************************
* Rocket template name space * Rocket template name space
*/ */
var rocket_tmpl = {};


rocket_tmpl.header = function() { function RocketTemplate(controllerInfo) {

this.header = function() {


var args = Array.prototype.slice.call(arguments) var args = Array.prototype.slice.call(arguments)
, controller_name = args.shift().replace(/[.,-;\s]+/g, '_') , controller_name = args.shift().replace(/[.,-;\s]+/g, '_')
Expand All @@ -36,32 +37,42 @@ var rocket_tmpl = {};


}; };


rocket_tmpl.footer = function() { this.footer = function() {


var args = Array.prototype.slice.call(arguments) var args = Array.prototype.slice.call(arguments)
, controller_name = args.shift().replace(/[.,-;\s]+/g, '_') , controller_name = args.shift()


, footer = [] , footer = []


, require_configuration = global.require_configuration || {} , 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("<script>");
footer.push("var _b = " + JSON.stringify(args) + "; "); footer.push("var _b = " + JSON.stringify(args) + "; ");
footer.push("var require = " + JSON.stringify(require_configuration) + ";"); footer.push("var require = " + JSON.stringify(require_configuration) + ";");
footer.push("</script>"); footer.push("</script>");
footer.push("<script src=\"//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js\"></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>") footer.push("<script>window.define||document.write('<script src=\"/js/rocket/vendors/require.min.js\"><\\/script>')</script>")


if (process.env['NODE_ENV'] === 'production') if (client_file) {
footer.push("<script src=\"/js/" + controller_name + "_client.js?" + require_configuration.urlArgs + "\"></script>") if (process.env['NODE_ENV'] === 'production')

footer.push("<script src=\"/js/" + client_file + ".js?" + require_configuration.urlArgs + "\"></script>")
footer.push("<script>require(['" + controller_name + "_client" + "'], function(m) { m && m.init && m.init.apply(m, _b); })</script>")

footer.push("<script>require(['" + client_file + "'], function(m) { m && m.init && m.init.apply(m, _b); })</script>")
}


return footer.join('\n'); return footer.join('\n');

}; };
}


/****************************************************************************** /******************************************************************************
* Destroy all the routes associated with a given controller * Destroy all the routes associated with a given controller
Expand Down Expand Up @@ -117,6 +128,8 @@ function buildWrapper(controller_info, method_func) {
; ;


res._boot_options || (res._boot_options = {}); 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. //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. //If the view is missing or if we have an XHR-Request, skip view rendering.
Expand Down Expand Up @@ -377,6 +390,7 @@ function setupController(app, routes_dadt, controller_file, matches, callback) {
, controller_path = controller_file.path , controller_path = controller_file.path


, reload , reload
, controllerInfo
; ;




Expand Down
2 changes: 1 addition & 1 deletion lib/loader.js
Expand Up @@ -73,7 +73,7 @@ exports.createServer = function createServer_rocket() {
} }


async.forEachSeries( 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) ] , [ CONTROLLERS_DIR_NAME , async.apply(controller.setup, app, routes) ]


Expand Down

0 comments on commit d99a1ca

Please sign in to comment.