Requirejs Does Not Follow Relative Path For Data-main With Baseurl Set
Solution 1:
This isn't working how you want it to because you're calling require with a data-main before you're setting the baseURL. I'm not sure why it's trying to go to scripts/main2.js though; I would expect it to attempt to load test/main2.js rather than scripts/main2.js. However, that's beside the point.
What you need to do is make sure that your baseURL is available to require before it tries to load you data-main. This can be accomplished by including your config first and using the syntax from the second example here: http://requirejs.org/docs/api.html#config.
Contents of index.html :
<!DOCTYPE html><html><head><metacharset="utf-8" /><title>Test</title><scriptsrc="scripts/config.js"></script><scriptdata-main="test/main2"src="scripts/lib/require.js"></script></head><body></body></html>
Contents of config.js :
varrequire = {
baseUrl: "scripts"
};
Solution 2:
I read this thread and couldn't quite understand why using the data-main attribute to point to a configuration js file wouldn't be the same as specifying the configuration before loading Require, as the answer to this thread suggests.
In my experiments, I learned that setting values using a data-main config js file might work (then, again, it might not). To those new to Require and AMDs and asynchronisity in general, the "might work" notion has to do with the fact that asynchronous operations run when then can--and not in any predictable order.
Having established that, there is a very important point made in the current version of the RequireJS documentation that eluded me until now:
You may also call require.config from your data-main Entry Point, but be aware that the data-main script is loaded asynchronously. Avoid other entry point scripts which wrongly assume that data-main and its require.config will always execute prior to their script loading.
For further information, see: http://requirejs.org/docs/api.html#data-main
Being new to RequireJS, I was mildly appalled to learn this--and I wasted a lot of time trying to debug path-access problems. At this stage it is unclear to me why anyone would use data-main (especially to define a baseUrl) since this reference will only randomly work. Instead the solution suggested by this thread (in which you set the baseUrl in a script tag that is NOT asynchronous, will work as expected and will reliably set the RequireJS configuration before kicking-off RequireJS.
Post a Comment for "Requirejs Does Not Follow Relative Path For Data-main With Baseurl Set"