Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

ADDED - Now caches the client files in production mode. 


Requires client directory to be writeable.
  • Loading branch information...
commit a18ed125666aa577f4c39f8596ab286cc86034cb 1 parent 93fb092
Gabriel Lesperance authored

Showing 3 changed files with 170 additions and 51 deletions. Show diff stats Hide diff stats

  1. +164 50 lib/client/client.js
  2. +5 0 lib/config.js
  3. +1 1  package.json
214 lib/client/client.js
@@ -12,6 +12,7 @@ var async = require('async')
12 12
13 13 , gzip = require('gzippo')
14 14 , requirejs = require('../vendors/r')
  15 + , rimraf = require('rimraf')
15 16 ;
16 17
17 18 var asyncFs = require('../util/async_fs')
@@ -22,6 +23,9 @@ var asyncFs = require('../util/async_fs')
22 23
23 24 , config = require('../config')
24 25
  26 + , CLIENT_BUILD_DIR_NAME = config.CLIENT_BUILD_DIR_NAME
  27 + , CLIENT_BUILD_JSON_FILE_NAME = config.CLIENT_BUILD_JSON_FILE_NAME
  28 +
25 29 , CLIENT_STATIC_DIR_NAME = config.CLIENT_STATIC_DIR_NAME
26 30
27 31 , CLIENT_JS_DIR_NAME = config.CLIENT_JS_DIR_NAME
@@ -35,7 +39,7 @@ var asyncFs = require('../util/async_fs')
35 39
36 40 , JADE_RUNTIME_FILE_NAME = config.JADE_RUNTIME_FILE_NAME
37 41
38   - , STATIC_FILES_CACHE_LIFETIME = config.STATIC_FILES_CACHE_LIFETIME
  42 + , STATIC_FILES_CACHE_LIFETIME = config.STATIC_FILES_CACHE_LIFETIME
39 43 ;
40 44
41 45 /******************************************************************************
@@ -265,6 +269,8 @@ function optimizeDir(dir_path, dst_tmpdir, options_tmpdir, callback) {
265 269 , baseUrl : './'
266 270 , dir : dst_tmpdir.path
267 271 , modules : []
  272 +
  273 + // , optimize : 'none'
268 274 , optimize: 'uglify'
269 275 }
270 276
@@ -486,36 +492,45 @@ function watchAndOptimize(path, dst_tmpdir, options_tmpdir, callback) {
486 492 */
487 493 exports.setup = function setup_client() {
488 494
489   - var args = Array.prototype.slice.call(arguments)
  495 + var args = Array.prototype.slice.call(arguments)
490 496
491   - , app = args.shift()
492   - , middlewares = args.shift()
  497 + , app = args.shift()
  498 + , middlewares = args.shift()
493 499
494   - , client = args.shift()
  500 + , client = args.shift()
495 501
496   - , callback = args.pop()
  502 + , callback = args.pop()
497 503
498   - , js_dir_path = path.join(client.path, CLIENT_JS_DIR_NAME)
  504 + , js_dir_path = path.join(client.path, CLIENT_JS_DIR_NAME)
499 505
500   - , css_dir_path = path.join(client.path, CLIENT_CSS_DIR_NAME)
  506 + , css_dir_path = path.join(client.path, CLIENT_CSS_DIR_NAME)
501 507
502   - , views_dir_path = path.join(js_dir_path, CLIENT_VIEWS_DIR_NAME)
  508 + , views_dir_path = path.join(js_dir_path, CLIENT_VIEWS_DIR_NAME)
503 509
504   - , static_dir_path = path.join(client.path, CLIENT_STATIC_DIR_NAME)
  510 + , static_dir_path = path.join(client.path, CLIENT_STATIC_DIR_NAME)
505 511
506   - , require_js_config_path = path.join(js_dir_path, REQUIRE_JS_CONFIG_FILE_NAME)
  512 + , require_js_config_path = path.join(js_dir_path, REQUIRE_JS_CONFIG_FILE_NAME)
507 513
508   - , client_js_tmpdir = tmpdir.mkuniqueSync()
509   - , client_css_tmpdir = tmpdir.mkuniqueSync()
  514 + , client_views_tmpdir = tmpdir.mkuniqueSync()
510 515
511   - , client_views_tmpdir = tmpdir.mkuniqueSync()
512   -
513   - , optimize_js_tmpdir = tmpdir.mkuniqueSync()
514   - , optimize_css_tmpdir = tmpdir.mkuniqueSync()
  516 + , optimize_js_tmpdir = tmpdir.mkuniqueSync()
  517 + , optimize_css_tmpdir = tmpdir.mkuniqueSync()
  518 +
  519 + , app_dir_path = path.join(client.path, '..')
  520 + , app_package_json_path = path.join(app_dir_path, 'package.json')
  521 +
  522 + , client_build_dir_path = path.join(app_dir_path, CLIENT_BUILD_DIR_NAME)
  523 +
  524 + , client_build_json_path = path.join(client_build_dir_path, CLIENT_BUILD_JSON_FILE_NAME)
  525 + , client_build_js_dir_path = path.join(client_build_dir_path, CLIENT_JS_DIR_NAME)
  526 + , client_build_css_dir_path = path.join(client_build_dir_path, CLIENT_CSS_DIR_NAME)
515 527
516 528 , js_tree_event_emitter
517 529
518 530 , css_tree_event_emitter
  531 +
  532 + , packageInfo
  533 + , buildClient
519 534 ;
520 535
521 536 /***
@@ -546,42 +561,141 @@ exports.setup = function setup_client() {
546 561 }
547 562
548 563 if (process.env['NODE_ENV'] === 'production') {
549   -
550   - async.parallel([
551   - function(callback) { async.waterfall([
552   -
553   - async.apply(watchAndOptimize, js_dir_path, client_js_tmpdir, optimize_js_tmpdir)
554   -
555   - , function(callback) {
  564 +
  565 + async.waterfall(
  566 + [ function (callback) {
  567 + fs.readFile(app_package_json_path, 'utf8', function (err, packageJSON) {
  568 +
  569 + if (err && err.code === 'ENOENT')
  570 + callback(null, true);
  571 + else if (err)
  572 + callback(err);
  573 +
  574 + else {
  575 + packageInfo = JSON.parse(packageJSON);
  576 +
  577 + fs.readFile(client_build_json_path, 'utf8', function (err, buildJSON) {
  578 + var buildInfo;
  579 +
  580 + if (err && err.code === 'ENOENT')
  581 + callback(null, true);
  582 + else if (err)
  583 + callback(err);
  584 +
  585 + else {
  586 + buildInfo = JSON.parse(buildJSON);
  587 +
  588 + if (buildInfo.version === packageInfo.version)
  589 + callback(null, false);
  590 + else
  591 + callback(null, true);
  592 + }
  593 + })
  594 + }
  595 + })
  596 + }
  597 +
  598 + , function (_buildClient, callback) {
  599 + buildClient = _buildClient;
  600 +
  601 + if (buildClient)
  602 + rimraf(client_build_dir_path, function (err) {
  603 + if (err && err.code === 'ENOENT')
  604 + callback(null);
  605 + else
  606 + callback(err);
  607 + });
  608 +
  609 + else
  610 + callback(null);
  611 + }
  612 +
  613 + , function (callback) {
  614 +
  615 + if (buildClient)
  616 + async.forEachSeries(
  617 + [ client_build_dir_path
  618 + , client_build_js_dir_path
  619 + , client_build_css_dir_path
  620 + ]
556 621
557   - //sets up a static middleware to serve the webapp optimized tmp js folder
558   - //as `/js`
559   - app.use('/js', gzip.staticGzip(client_js_tmpdir.path));
  622 + , function (dirPath, callback) {
  623 + fs.mkdir(dirPath, callback);
  624 + }
  625 +
  626 + , callback
  627 + );
  628 +
  629 + else
  630 + callback(null);
  631 + }
  632 +
  633 + , function (callback) {
  634 + async.parallel(
  635 + [ function (callback) {
  636 + async.waterfall(
  637 + [ function (callback) {
  638 + if (buildClient)
  639 + watchAndOptimize(js_dir_path, { path : client_build_js_dir_path }, optimize_js_tmpdir, callback);
  640 +
  641 + else
  642 + callback(null);
  643 + }
  644 +
  645 + , function (callback) {
  646 + if (packageInfo)
  647 + fs.writeFile(client_build_json_path, JSON.stringify(packageInfo), 'utf8', function (err) {
  648 + callback(err);
  649 + });
  650 + else
  651 + callback(null);
  652 + }
  653 +
  654 + , function(callback) {
  655 +
  656 + //sets up a static middleware to serve the webapp optimized tmp js folder
  657 + //as `/js`
  658 + app.use('/js', gzip.staticGzip(client_build_js_dir_path));
  659 +
  660 + callback(null);
  661 + }
  662 +
  663 + ]
  664 + , callback
  665 + );
  666 + }
  667 +
  668 + , function (callback) {
  669 + async.waterfall(
  670 +
  671 + [ function (callback) {
  672 + if (buildClient)
  673 + watchAndOptimize(css_dir_path, { path : client_build_css_dir_path }, optimize_css_tmpdir, callback);
  674 +
  675 + else
  676 + callback(null);
  677 + }
  678 +
  679 + , function(callback) {
  680 + //sets up a static middleware to serve the webapp optimized tmp css folder
  681 + //as `/css`
  682 + app.use('/css', gzip.staticGzip(client_build_css_dir_path));
  683 +
  684 + callback(null);
  685 + }
560 686
561   - callback(null);
562   - }
563   -
564   - ], callback);}
565   -
566   - , function(callback) { async.waterfall([
567   -
568   - async.apply(watchAndOptimize, css_dir_path, client_css_tmpdir, optimize_css_tmpdir)
569   -
570   - , function(callback) {
571   -
572   - //sets up a static middleware to serve the webapp optimized tmp css folder
573   - //as `/css`
574   - app.use('/css', gzip.staticGzip(client_css_tmpdir.path));
  687 + ]
  688 + , callback
  689 + );
  690 + }
575 691
576   - callback(null);
577   -
578   - }
579   -
580   - ], callback); }
581   -
582   - , async.apply(watchAndExportConfig, require_js_config_path)
583   -
584   - ], doneCallback);
  692 + , async.apply(watchAndExportConfig, require_js_config_path)
  693 + ]
  694 + , callback
  695 + );
  696 + }
  697 + ]
  698 + , doneCallback);
585 699
586 700 } else {
587 701
5 lib/config.js
@@ -8,7 +8,12 @@ module.exports = new function() {
8 8 this.CONTROLLERS_DIR_NAME = 'controllers';
9 9 this.PLUGINS_DIR_NAME = 'plugins';
10 10 this.EXPORTS_DIR_NAME = 'exports';
  11 +
11 12 this.CLIENT_DIR_NAME = 'client';
  13 + this.CLIENT_BUILD_DIR_NAME = 'client-build';
  14 +
  15 + this.CLIENT_BUILD_JSON_FILE_NAME = 'build.json';
  16 +
12 17 this.LOCALES_DIR_NAME = 'locales';
13 18
14 19 /* client specific dirs */
2  package.json
@@ -2,7 +2,7 @@
2 2 "name": "rocket"
3 3 , "description": "The rapid development framework for node.js"
4 4 , "keywords": ["mvc", "model view controller", "comet","rapid development", "Rocket", "framework"]
5   -, "version": "0.4.2"
  5 +, "version": "0.5.0"
6 6 , "homepage": "http://wavo.me"
7 7 , "repository":
8 8 { "type" : "git"

0 comments on commit a18ed12

Please sign in to comment.
Something went wrong with that request. Please try again.