RequireJS: Fooling libraries

January 01, 2014

One library can mean two different libraries, based on context.

Problem

I wanna use <https://github.com/HubSpot/vex>

but instead of calling vex.dialog.open(), I would like to name call just vex.open(). But, vex.dialog.js depends on vex.js, so just aliasing vex.dialog.js to vex.js would break the dependency.

main.js

require.config({

 paths: {

 vex: '../bower\_components/vex/js/vex.dialog',

 },

 map: {

 'vex': {'vex': '../bower\_components/vex/js/vex'}

 }

});

app.js

define([

 'vex'

], function (

 vex // `vex` here '../bower\_components/vex/js/vex.dialog'

) {

 vex.open({

 message: '...'

 });

});

vex.dialog.js

if (typeof define === 'function' && define.amd) {

 define(['jquery', 'vex'], vexDialogFactory); // `vex` here means '../bower\_components/vex/js/vex'

}