Skip to content

Instantly share code, notes, and snippets.

@dcherman
Created May 2, 2013 17:40
Show Gist options
  • Save dcherman/5503922 to your computer and use it in GitHub Desktop.
Save dcherman/5503922 to your computer and use it in GitHub Desktop.
(function() {
// Override RequireJS's global error handler
requirejs.onError = (function(errback) {
return function require$customOnError(err) {
console.log('RequireJS error!\nType: ' + err.requireType + '\nModules: ' + JSON.stringify(err.requireModules));
// Delegate the rest of the errors
if (typeof errback === 'function') {
errback(err);
}
else {
throw err;
}
};
})(requirejs.onError);
// Configure the bare minimum
require.config({
// Necessary to support craptacular IE
enforceDefine: true,
// `paths` config is relative to the `baseUrl`, and never includes a
// ".js" extension since the `paths` config could be for a directory.
//
// NOTE:
// All of the following are the only non-static data/configuration(s) to be served from Website
// If they can't be cached at all (e.g. via a session-based cache key), then we could also
// combine them into a single resource per page (i.e. 'config.page' would contain the others).
// They will be requested from the Website domain rather than being relative to the `baseUrl`
// because their paths start with a '/' (not that they still do NOT end with '.js', though).
paths: {
'config.routing': '/new-magically-generated-js/config.routing',
'config.session': '/new-magically-generated-js/config.session',
'config.user': '/new-magically-generated-js/config.user',
'config.page': '/new-magically-generated-js/config.page'
}
});
// Grab the routing data and product data from Website
define(
['config.session'],
function(sessionConfig) {
// Configure more
require.config({
// By default (from now on), load modules from the StaticContent server instead.
// NOTE: This is actually done automatically if including the `data-main` attribute on
// this script file's script tag element on the page, so explicitly setting it here
// is not even necessary!
paths: {
'config.platform': 'Platform/config.platform',
'config.product': 'Products/' + sessionConfig.product.name + '/config.product'
}
});
// Chain load the additional configuration
require(
['config.platform', 'config.product'],
function(platformConfig, productConfig) {
// Merge in the Platform's RequireJS configuration, if any
if (platformConfig.requirejs) {
require.config(platformConfig.requirejs);
}
// Merge in the Product's RequireJS configuration, if any
if (productConfig.requirejs) {
require.config(productConfig.requirejs);
}
// Generically find this page's name, i.e. `pageName` === 'new.html'
var pageName = (function(urlPath) {
return urlPath.substring(urlPath.lastIndexOf('/') + 1);
})(window.location.pathname);
// Finally: Generically bootstrap this page based on its name!
// NOTE: We can adapt this technique to be based on the page's full path,
// a configuration value in "config.page.js", etc.
require(['Products/' + sessionConfig.product.name + '/pages/' + pageName]);
}
);
}
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment