0

Using require.js and r.js, lets say I have a module, "myModule.js".

The r.js docs state that they support integration with has.js such that

has: {
     feature: true
}

transforms something like:

if (has('feature')) {
     alert('feature detected!');
} else {
     alert('feature not supported')
}

to:

alert('feature detected!');

However, how can I go about creating two versions of the same module? One version for those browsers that support the feature and another version for those browsers that do not support the feature.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
gxc
  • 4,946
  • 6
  • 37
  • 55

1 Answers1

1

Let's say your entry point to your application is named app. You could create a module app-has-foo where the feature foo is true and app-no-foo where it is false. You have to use create so that r.js knows it has to create the modules. You use override to override those options that belong outside the modules option.

({
    baseUrl: ...,
    mainConfigFile: ...,
    dir: "out",
    modules: [
        {
            name: "app-has-foo",
            create: true,
            include: ["app"],
            override: {
                has: {
                    foo: true
                }
            }
        },
        {
            name: "app-no-foo",
            create: true,
            include: ["app"],
            override: {
                has: {
                    foo: false
                }
            }
        }
    ]
});
Louis
  • 146,715
  • 28
  • 274
  • 320
  • I think that's pretty much what I need...however, I haven't been able to verify bc adding the override property to any of my module options (even if I leave it empty) causes a `Error: RangeError: Maximum call stack size exceeded`. Any thoughts on what could be causing that? – gxc Mar 05 '15 at 17:41
  • I cannot reproduce the error here. Is the error happening while running `r.js`? – Louis Mar 05 '15 at 18:01
  • Looks like the error was coming from this guy: https://github.com/millermedeiros/requirejs-hogan-plugin. I'll dig into that separately. Thanks for the help! – gxc Mar 05 '15 at 20:42