Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requirejs module alias/remap

I'm currently integrating foreign code into our application. Part of the process, I must substitute one of their requirejs modules with ours.

Obviously I can't modify their code, otherwise I'd have to do the change at every update. What I can do is modify the main.js (data-main of requirejs).

Here is, roughly, what they have:

requirejs.config({
    packages: [
        'beerpong'
    ]
});

So they have this beerpong package, with some modules in there. Among these modules, there is the beer.js file. It can be required with a require('beerpong/beer').

Aside from this, I have my files, in a separate folder, say vodkapong/beersubstitute. What I would like is that, whenever someone require('beerpong/beer'), that requirejs actually serves him my vodkapong/beersubstitute instead.

tl;dr: How can I remap an existing module to use my module instead?

PS: Sadly, we're not actually writing a beerpong game... One day maybe!

like image 800
aspyct Avatar asked Sep 19 '13 10:09

aspyct


2 Answers

You could use the map option. It can remap an AMD module for a specific module or for all modules with the "*" name. An example would be:

require.config({
    map: {
        "*": {
            "beerpong/beer": "vodkapong/beersubstitute"
        }
    },
    ...
});
like image 58
Nikos Paraskevopoulos Avatar answered Oct 06 '22 21:10

Nikos Paraskevopoulos


You can do something like this -

require.config({
    paths: {
        jquery_ui_scrollbar: 'libs/jquery/jquery.custom-scrollbar'
    }
});
require(['dependency1', 'dependency2'], function (dep1, dep2) {
   // code goes here
});

And in your define call you can do this -

define(['jquery_ui_scrollbar'], function(scrollbar) {});
like image 44
halkujabra Avatar answered Oct 06 '22 22:10

halkujabra