Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS does not follow relative path for data-main with baseUrl set

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 :

  • Even if I use "scripts/test/main2", "/scripts/test/main2", or "whateverIWant/main2" in my data-main, it oddly always looks for "scripts/main2.js"

Note that I am using requirejs 2.1.8

like image 920
personne3000 Avatar asked Oct 30 '13 12:10

personne3000


1 Answers

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"
};
like image 138
Waxen Avatar answered Oct 17 '22 06:10

Waxen