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:
Solutions attempted
baseUrl
and paths
. For example baseUrl: libs
, and paths: { "app" : "../app" }
, etc.deps
and callback
instead of using require
below config
.context
, matching baseUrl
.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.
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.
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