I'm using RequireJS to load some modules.
I'm serving static assets using a mirror CDN. The problem is that modules are loaded relatively to the website domain and not to the data-main file, so that modules are loaded this way:
my-website.com/
↳ cdn.com/assets/js/require-main.min.js
↳ my-website.com/assets/js/helper-module.js
↳ my-website.com/assets/js/utility-module.js
Is there a way to load modules relatively to the data-main file, in order to serve modules using the CDN?
my-website.com/
↳ cdn.com/assets/js/require-main.min.js
↳ cdn.com/assets/js/helper-module.js
↳ cdn.com/assets/js/utility-module.js
I can't hard-code the CDN domain anywhere since it is not always the same.
This is a sample of require-main.js
requirejs.config({
baseUrl: "../js/",
waitSeconds: 15,
paths: {
helper: "helper-module",
...
}
});
and this is the rjs.optimize function I use to minify all the assets and combine some modules
rjs.optimize({
appDir: './assets/js/',
baseUrl: ".",
mainConfigFile: './assets/js/require-main.js',
dir: './public/assets/js/',
preserveLicenseComments: true,
optimize: 'uglify2',
findNestedDependencies: true,
logLevel: 0,
uglify2: {
mangle: false
},
modules: [
{
name: 'require-main'
...
}
],
removeCombined: true,
writeBuildTxt: false
});
In the production (when you desire to serve using CDN), supply the baseUrl to be the CDN domain domain. requirejs optimizer outputs the configurations realtive to the appDir. If you are loading files from the CDN then you may have to change the baseUrl value to be the CDN URL.
Be sure to read this too if your modules depend on third party AMD modules http://requirejs.org/docs/optimization.html#empty
As it seems you are using bundles, use the bundlesConfigOutFile property to write the bundled configuration into a file, so that you need not write the configuration manually. For more : https://github.com/requirejs/r.js/blob/master/build/example.build.js
You can change the baseUrl value in the r.js callback.