Friday, April 19, 2013

RequireJS

The RequireJS documentation appears to suggest that you can define configurations such as the following:

requirejs.config({baseUrl: 'http://app.example.com',
                  paths: { underscore : 'static/js/external/underscore.js',
                           backbone : 'static/js/external/backbone.js'})

If you try to use this approach, you may find RequireJS appending an extra .js file.

RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs. RequireJS will automatically add it when translating the module ID to a path. With the paths config, you can set up locations of a group of scripts. All of these capabilities allow you to use smaller strings for scripts as compared to traditional <script> tags.

There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it. If a module ID has one of the following characteristics  the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document:
  • Ends in ".js".
  • Starts with a "/".
  • Contains an URL protocol, like "http:" or "https:".
Within the RequireJS code, there is a test for the question mark (?):
url += (ext || (/\?/.test(url) ? '' : '.js'));

...to avoid RequireJS add an extra .js to the end, you can do the following to avoid the addtion.JS:

requirejs.config({baseUrl: 'http://app.example.com',
                  paths: { underscore : 'static/js/external/underscore.js?r=1',
                           backbone : 'static/js/external/backbone.js?r=1'})


This pull-request still seems to fix this issue:

https://github.com/jrburke/requirejs/pull/716

No comments:

Post a Comment