Skip to content

Commit

Permalink
added support for shaker defined bootstrap and seed
Browse files Browse the repository at this point in the history
  • Loading branch information
diervo committed Dec 9, 2012
1 parent 90d36bb commit 98c2ac3
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 30 deletions.
3 changes: 2 additions & 1 deletion addons/ac/shaker.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ YUI.add('mojito-shaker-addon', function(Y, NAME) {
return shakerMeta;
},
// Add here the assets at the app level
// They have to be the first on the assets queue to preserver the order.
// They have to be the first on the assets queue to preserve the order.
_augmentAppAssets: function (ac) {
var instance = ac.command.instance,
action = instance.action || ac.command.action || 'index',
Expand Down Expand Up @@ -60,6 +60,7 @@ YUI.add('mojito-shaker-addon', function(Y, NAME) {
assets.topShaker = assets.topShaker || {js: [], css: [], blob:[]};
assets.bottomShaker = assets.bottomShaker || {js: [], css: [], blob: []};

//attach the assets
assets.topShaker.css = shakerBundle.css;
assets.bottomShaker.js = core.concat(shakerBundle.js);
}
Expand Down
68 changes: 48 additions & 20 deletions addons/rs/shaker.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@
/*jslint anon:true, sloppy:true, nomen:true*/
/*global YUI*/


/**
* @module ResourceStoreAddon
*/

/**
* @class RSAddonUrl
* @extension ResourceStore.server
*/
YUI.add('addon-rs-shaker', function(Y, NAME) {

var libpath = require('path');
var libpath = require('path'),
libfs = require('fs');

function RSAddonShaker() {
RSAddonShaker.superclass.constructor.apply(this, arguments);
Expand All @@ -39,7 +31,9 @@ YUI.add('addon-rs-shaker', function(Y, NAME) {
this.appConfig = config.host.getStaticAppConfig() || {};
this.shakerConfig = this.appConfig.shaker || {};

var yuiRS = this.rs.yui;
var yuiRS = this.rs.yui,
store = this.rs,
shakerConfig = this.shakerConfig;

if (!this.initilized) {
//first read the shaker metadata
Expand All @@ -50,28 +44,27 @@ YUI.add('addon-rs-shaker', function(Y, NAME) {
Y.log('Preloading store', 'info','mojito-store');
} else {
Y.log('Metadata not found.','warn','Shaker');
return;
}
}

/*
* HOOKS AREA!:
* AOP HOOKS:
* We need to hook some events on the store,
* but we will have to do different hooks depending if we are on build time or in runtime
* The reason is that there are some hook that are not needeed on runtime or viceversa
*/
if (true || shakerConfig.optimizeBootstrap) {
this.beforeHostMethod('makeResourceVersions', this.makeResourceVersions, this);
}

/*
* Either on build time or runtime we need to change the urls...
* but only when comboCDN is enabled.
*/
if (this.shakerConfig.comboCDN) {
if (shakerConfig.comboCDN) {
this.beforeHostMethod('resolveResourceVersions', this.resolveResourceVersions, this);
}

// This hooks are for runtime
if (!process.shakerCompile) {
//alter seed
if (this.shakerConfig.comboCDN) {
if (shakerConfig.comboCDN) {
Y.Do.after(this.alterAppSeedFiles, yuiRS, 'getAppSeedFiles', this);
}
//alter bootstrap config
Expand All @@ -86,6 +79,41 @@ YUI.add('addon-rs-shaker', function(Y, NAME) {
this.rs = null;
},
/*
* We need to add the synthetic bootstrap items
*/
makeResourceVersions: function () {
var store = this.rs;
yuiRS = store.yui;
this.addOptimizedBootstrap(store, yuiRS);
},
addOptimizedBootstrap: function (store, yuiRS) {
var relativePath = libpath.join(__dirname, '../../lib/bootstrap/'),
bootstrapResources = ['yui-fake-inline', 'yui-override', 'yui-use-hook'];

Y.Array.each(bootstrapResources, function (item) {
var content = libfs.readFileSync(relativePath + item + '.js','utf8'),
res = {
source: {},
mojit: 'shared',
type: 'yui-module',
subtype: 'synthetic',
name: item,
affinity: 'client',
selector: '*',
yui: {
name: item
}
};
res.id = [res.type, res.subtype, res.name].join('-');
res.source.pkg = store.getAppPkgMeta();
res.source.fs = store.makeResourceFSMeta(__dirname, 'app', '../../lib/bootstrap/', item + '.js', true);
// adding res to cache
yuiRS.appModulesRess[item] = res;
yuiRS.resContents[item] = content;
store.addResourceVersion(res);
}, this);
},
/*
* When comboLoad is active we need to change the seed to point to the CDN...
* We rely on the mapping we have on the Shaker metadata
*/
Expand Down Expand Up @@ -135,7 +163,7 @@ YUI.add('addon-rs-shaker', function(Y, NAME) {
ress = this.rs.getResourceVersions({mojit: mojit});
for (r = 0; r < ress.length; r += 1) {
res = ress[r];
//CHECK ABOUT THE VIEWS HERE...
//Change the url
if (res.yui && cdnUrls[res.url]) {
res.url = cdnUrls[res.url];
}
Expand Down
7 changes: 1 addition & 6 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ ShakerCore.prototype.getAppModuleResources = function () {
var ress,
m,
mojit,
mojits,
mojits= [],
store = this.store,
context = this.getStoreConfigs().context,
yuiModules = store.yui.getYUIURLResources(),
Expand Down Expand Up @@ -452,11 +452,6 @@ ShakerCore.prototype.getAppModuleResources = function () {
continue;
}
if (res.yui && res.yui.name) {
if (moduleRess[res.yui.name]) {
Y.log('YUI Modules should have unique name per affinity. ' +
'Module [' + res.yui.name + '] has both common and ' +
'client affinity.', 'warn', 'getAppModulesResources');
}
moduleRess[res.yui.name] = res;
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/shaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var path = require('path'),
HB = require('yui/handlebars').Handlebars,
mime = require('mime'),
ShakerCore = require('./core').ShakerCore,
//logger = require('./core').logger,
YUI = require('yui').YUI,
Y = require('yui').YUI({useSync: true});

Expand Down Expand Up @@ -81,7 +80,6 @@ ResourceProcessor.prototype = {
return result;
}
};
// Move class above eventually to another file...
//---------------------

/**
Expand All @@ -105,12 +103,14 @@ function Shaker (options) {
this.root = options.root || process.cwd();
this.logger = new libutils.SimpleLogger(options);
this._context = options.context || {};
//when we are in local mode we need to delete previews Shaker runs
process.shakerCompile = true;

//when we are in local mode we need to delete previews Shaker runs
this.removeCompiledAssets(this.root);

this.core = new ShakerCore(options);
this.rp = new ResourceProcessor();

//getting the merged config defined in app.json and in shaker.json
this._shakerConfig = this.expandShakerConfig(this._context);

Expand Down Expand Up @@ -154,9 +154,11 @@ Shaker.prototype = {
shakerMeta = this.core.run();

//this.generateBoostrap(shakerConfig, callback);
//console.log(Y.Object.keys(this.core.getAppModuleResources()));

this.processMetadata(shakerMeta, shakerConfig, function (err, processedMetadata) {
if (err) {
console.log(err);
callback('Build process aborted!');
return;
}
Expand All @@ -177,6 +179,7 @@ Shaker.prototype = {
},

generateBoostrap: function (context, shakerConfig, callback) {
//TODO!!!
this.core.store.yui.getResourceContent({name:'loader-app'}, function (err, data) {
console.log(data.toString());
callback();
Expand Down
8 changes: 8 additions & 0 deletions test/rs/fixtures/test_bootstrap/application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"settings": ["master"],
"shaker": {
"optimizeBootstrap": true
}
}
]
39 changes: 39 additions & 0 deletions test/rs/test_bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This test should be executed in the file's path.
*/

var YUITest = require('yuitest').YUITest,
Shaker = require('../../lib/core.js').ShakerCore,
libfs = require('fs');
libpath = require('path'),
sc = {};

var Assert = YUITest.Assert;
var suite = new YUITest.TestSuite({
name: "Test bootstrap",
setUp : function () {
var relRoot = 'fixtures/test_bootstrap',
root = libpath.join(__dirname, relRoot);

sc = new ShakerCore({root: root});
}
});

//console.log(shaker.logger.dump(result));

suite.add( new YUITest.TestCase({
name: "Unit Test",
//console.log(shaker.logger.dump(result));
'test config bootstrap is true': function () {
var shakerConfig = sc.getAppShakerConfigByContext({});
Assert.isTrue(shakerConfig.optimizeBootstrap);
},
'test inlie file augmentation': function () {
var app = sc.getAppModuleResources();
},
testFoo : function (){
Assert.isTrue(true);
}
}));

YUITest.TestRunner.add(suite);

0 comments on commit 98c2ac3

Please sign in to comment.