From eb6495f1ec1034b9e06da84f20b8384fbb725e5e Mon Sep 17 00:00:00 2001 From: Henrik Vendelbo Date: Sun, 8 Mar 2015 17:23:32 +0100 Subject: [PATCH] test(bundler): better client reset --- lib/client/index.js | 41 +++++++++++++++--------- lib/client/serve/ondemand.js | 3 ++ test/unit/client/bundler/default.test.js | 1 + test/unit/client/index.test.js | 31 +++++++++++++++--- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/lib/client/index.js b/lib/client/index.js index f707635c..989d26b0 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -14,18 +14,23 @@ var fs = require('fs'), log = require('../utils/log'), systemAssets = require('./system'); -// Determine if assets should be (re)packed on startup -var packAssets = process.env['SS_PACK']; - // Set defaults -var options = { - packedAssets: packAssets || false, - liveReload: ['code', 'css', 'static', 'templates', 'views'], - defaultEntryInit: 'require("/entry");', - urls: { +var options = {}; + +function setDefaultOptions(options) { + + // Determine if assets should be (re)packed on startup + var packAssets = process.env['SS_PACK']; + + //TODO packAssets vs packedAssets + options.packedAssets = packAssets || false; + + options.liveReload = ['code', 'css', 'static', 'templates', 'views']; + options.defaultEntryInit = 'require("/entry");'; + options.urls = { assets: '/assets/' - }, - dirs: { + }; + options.dirs = { client: '/client', code: '/client/code', css: '/client/css', @@ -34,8 +39,10 @@ var options = { templates: '/client/templates', views: '/client/views', workers: '/client/workers' - } -}; + }; +} + +setDefaultOptions(options); // Store each client as an object var clients = {}; @@ -66,7 +73,6 @@ module.exports = function(ss, router) { formatters.add('html'); formatters.add('map'); - systemAssets.load(); // Return API @@ -75,6 +81,7 @@ module.exports = function(ss, router) { templateEngine: templateEngine, assets: systemAssets, options: options, + needToPackAssets: options.packedAssets, // Merge optional options set: function(newOption) { @@ -167,10 +174,11 @@ module.exports = function(ss, router) { systemAssets.send('code', 'init', options.defaultEntryInit); if (options.packedAssets) { + var api = this; // Attempt to find and serve existing pre-packed assets // If unsuccessful, assets will be re-packed automatically - if (!packAssets) { + if (!this.needToPackAssets) { log.info('i'.green, 'Attempting to find pre-packed assets... (force repack with SS_PACK=1)'.grey); ss.bundler.forEach(function(bundler) { var id = options.packedAssets.id || bundler.latestPackedId; @@ -179,13 +187,13 @@ module.exports = function(ss, router) { log.info('✓'.green, ('Serving client \'' + bundler.client.name + '\' using pre-packed assets (ID ' + bundler.client.id + ')').grey); } else { log.info('!'.red, ('Unable to find pre-packed assets for \'' + bundler.client.name + '\'. All assets will be repacked').grey); - packAssets = true; + api.needToPackAssets = true; } }); } // Pack Assets - if (packAssets) { + if (this.needToPackAssets) { for (var name in clients) { if (clients.hasOwnProperty(name)) { ss.bundler.pack(clients[name]); @@ -210,6 +218,7 @@ module.exports = function(ss, router) { forget: function() { clients = {}; + setDefaultOptions(options); } }; }; diff --git a/lib/client/serve/ondemand.js b/lib/client/serve/ondemand.js index 5741ca13..bad85c41 100644 --- a/lib/client/serve/ondemand.js +++ b/lib/client/serve/ondemand.js @@ -24,6 +24,7 @@ module.exports = function(ss, router, options) { function serve(processor) { return function(request, response) { var path = utils.parseUrl(request.url); + //TODO packAssets vs packedAssets if (options.packAssets && queryCache[path]) { return utils.serve.js(queryCache[path], response); } else { @@ -50,6 +51,7 @@ module.exports = function(ss, router, options) { client: params.client, clientId: params.ts, //pathPrefix: options.globalModules? null : path, + //TODO packAssets vs packedAssets compress: options.packAssets }, function(js) { output.push(js); @@ -70,6 +72,7 @@ module.exports = function(ss, router, options) { params = qs.parse(thisUrl.query); return bundler.get(params).asset.worker(path, { + //TODO packAssets vs packedAssets compress: options.packAssets }, cb); } diff --git a/test/unit/client/bundler/default.test.js b/test/unit/client/bundler/default.test.js index 13c4233a..77208d4d 100644 --- a/test/unit/client/bundler/default.test.js +++ b/test/unit/client/bundler/default.test.js @@ -159,6 +159,7 @@ describe('default bundler', function () { options.defaultEntryInit = origDefaultEntryInit; ss.client.assets.unload(); + ss.client.forget(); ss.client.assets.load(); }); diff --git a/test/unit/client/index.test.js b/test/unit/client/index.test.js index 507773d4..8e665e4f 100644 --- a/test/unit/client/index.test.js +++ b/test/unit/client/index.test.js @@ -152,20 +152,41 @@ describe('client asset manager index', function () { var view = require('../../../lib/client/view'); describe('unpacked #view',function() { - it('should render the SS view'); + + beforeEach(function() { + + // back to initial client state + ss.client.unload(); + ss.client.assets.unload(); + ss.client.forget(); + ss.client.assets.load(); + + // options and load client + options.packedAssets = false; + ss.client.load(); + }); + + afterEach(function() { + ss.client.unload(); + }); + + it('should render the SS view'); + }); describe('packed #view', function() { - options.packedAssets = true; beforeEach(function() { - options.defaultEntryInit = origDefaultEntryInit; - - ss.client.load(); + // back to initial client state + ss.client.unload(); ss.client.assets.unload(); + ss.client.forget(); ss.client.assets.load(); + + // options and load client + options.packedAssets = true; ss.client.load(); });