jrburke/requirejs — pull request #1179 by squareplanetdesign (2014-08-02 16:18:05+00:00)
Title: Added transformUrl config option to allow an arbitrary tranform of all urls.

We have a problem in our system where we need to modify the file names passed from requirejs to our application server.

The proposed transformUrl would provide the needed service.

While a plugin could have been used, it would require annotating all the require() and define() calls which didn't make sense to us. With this solution, we can make the necessary transforms at run-time allowing both the runtime and the build to make these transforms.

Several use cases are possible with this solution:

1) Requesting debug and release files dynamically at run-time.

var debug = true;
requirejs.config({
transformUrl: function(url, module) {
var PATTERN = /\.js$/;
if (PATTERN.test(url) && debug === true) {
return url.replace(PATTERN, '.debug.js');
}
}
});

2) Adding version build numbers to the requested files so you can use long cache times and still get updates when there are version updates.

var version = 'v123';
requirejs.config({
transformUrl: function(url, module) {
var PATTERN = /\.js$/;
if (PATTERN.test(url)) {
return url.replace(PATTERN, '.' + version + '.js');
}
}
});

3) Requesting localized versions of these resources at run-time.

var locale = "en-US";
requirejs.config({
transformUrl: function(url, module) {
var PATTERN = /\.js$/;
if (PATTERN.test(url)) {
return url.replace(PATTERN, locale + '.js');
}
}
});

4) When building, package up various versions based on say a locale extension on the file.