Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is require.js ignoring config baseUrl and paths

I am having a problem where almost-identical require.js config calls appear to work differently, in one case failing to set baseUrl and path.

Folder structure

 - /js
 - - app/
 - - libs/
 - - - hogan.js
 - - - backbone
 - - - - backbone.js
 - - - - etc.
 - - - boostrap
 - - - jquery
 - - - - jquery.js
 - - - - etc.
 - - - require
 - - - - require.js
 - - - - etc.
 - - templates/
 - - require.config.js

Script tag

<script data-main="/resources/js/require.config" src="/resources/js/libs/require/require.js"></script>

Require configuration

require.config(
    {
        baseUrl: "/resources/js/libs",
        shim: {
            "underscore": {
                exports: "_"
            },
            "backbone": {
                deps: [
                    "json2",
                    "underscore",
                    "jquery"
                ],
                exports: "Backbone"
            }
        },
        paths: {
            "app": "/resources/js/app",
            "backbone": "backbone/backbone",
            "jquery": "jquery/jquery",
            "templates": "/resources/js/templates",
            "templateEngine": "hogan"
        }
    }
);

require(
    [
        "app/router"
    ],
    function( AppRouter ){
        AppRouter.execute();
    } 
);

In both cases, a script tag gets appended that shows the full contents of require.config.js, starting with:

<script type="text/javascript" charset="utf-8" data-requirecontext="_" data-requiremodule="require.config" src="/resources/js/require.config.js">

Definition in app/router

define(
    [
        'backbone',
        'templateEngine'
    ],
// etc
);

However where the problem occurs, app/router is never loaded, only the following (no 404, nothing):

GET require.js 200 OK 180ms     
GET require.config.js 200 OK 69ms

If I enter require(["app/router"]); into the Firebug console I get the following:

"NetworkError: 404 Not Found - /resources/js/backbone.js
"NetworkError: 404 Not Found - /resources/js/templateEngine.js"

When what I expect to see is:

  • /resources/js/libs/backbone/backbone.js
  • /resources/js/libs/hogan.js

Solutions attempted

  • I have attempted to use various combinations of relative and absolute paths for baseUrl and paths. For example baseUrl: libs, and paths: { "app" : "../app" }, etc.
  • I tried deps and callback instead of using require below config.
  • I have added the property context, matching baseUrl.
  • I have moved require.config.js into the libs directory (editing baseUrl and paths as appropriate; still get the same issue of ignored config, only it tries to load /resources/js/libs/backbone.js instead, for example).

Note

This folder structure and config file does work on my localhost, where I remove /resources/ from the paths as shown above. Instead all-relative paths (none starting with /) are used for baseUrl and paths. But on the actual server that is not an option.

like image 701
morewry Avatar asked Jun 25 '12 02:06

morewry


1 Answers

Solution was to not have any . (periods) in the configuration file's filename. After renaming the file to require-config.js everything worked as expected. Other files are fine with dots in the name, just not the main configuration file.

like image 160
morewry Avatar answered Oct 17 '22 09:10

morewry