Using requireJS, I am trying to specify a path for my data-main that is different from the baseUrl. It seems that requireJS is ignoring whatever I type before the file name, and always look for the file in the baseUrl folder.
I have the following folder structure :
index.html
scripts/
lib/
require.js
test/
main2.js
config.js
Contents of index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script data-main="test/main2" src="scripts/lib/require.js"></script>
<script src="scripts/config.js"></script>
</head>
<body></body>
</html>
Contents of config.js :
requirejs.config({
baseUrl: "scripts"
});
And I am getting a 404 error for : GET [...]/scripts/main2.js , even though it should be looking for [...]/scripts/test/main2.js. If I remove the config.js file and use data-main="scripts/test/main2" it works, but I would like to be able to specify a baseUrl for my project.
Any ideas ?
Edit : following the answer by Waxen :
Note that I am using requirejs 2.1.8
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>
<meta charset="utf-8" />
<title>Test</title>
<script src="scripts/config.js"></script>
<script data-main="test/main2" src="scripts/lib/require.js"></script>
</head>
<body></body>
</html>
Contents of config.js :
var require = {
baseUrl: "scripts"
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With