14

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!

4

2 回答 2

31

您可以使用地图选项。它可以为特定模块或具有"*"名称的所有模块重新映射 AMD 模块。一个例子是:

require.config({
    map: {
        "*": {
            "beerpong/beer": "vodkapong/beersubstitute"
        }
    },
    ...
});
于 2013-09-19T11:32:43.870 回答
1

你可以做这样的事情 -

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

在您的定义电话中,您可以这样做 -

define(['jquery_ui_scrollbar'], function(scrollbar) {});
于 2013-09-19T10:53:42.100 回答