1

If I configure a specific context:

var myRequire = require.config({
  context: 'foo',
  paths: {
    'jquery': 'jquery-1.11.2'
  }
});

I can then immediately use myRequire to load a module:

myRequire(['require', 'jquery'], function (require, $) { /* ... */ });

But if I move the configuration to a separate file, how do I retrieve the proper context?

Based on this answer I found that requirejs.s.contexts['foo'].require returns the same function as myRequire. That seems hacky. Another option would be to define a module for the config and return myRequire from it. What is the approved method?

1 Answer 1

1

This is working:

require.config.js:

define(function () {
    'use strict';

    var myRequire = require.config({
        context: 'foo',
        paths: {
            'jquery': 'jquery-1.11.2'
        }
    });

    return {
        myRequire: myRequire
    };
});

main.js:

// does need path and .js extension
requirejs(['/scripts/require.config.js'], function (requireConfig) {
    var myRequire = requireConfig.myRequire;

    myRequire(['require', 'jquery'], function (require, $) { /* ... */ });
});

I'm still open to better ways, and I have not tested this with r.js.

Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much what came to my mind when I saw your question last night but I was busy then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.