Relative ids from module defined in path #162

Closed
xcoderzach opened this Issue Jan 5, 2012 · 3 comments

Projects

None yet

4 participants

@xcoderzach

requiring modules from within a module defined in paths doesn't work properly.

for example with the configuration:

require.config({
  baseUrl: "/javascripts"
, paths: {my_module: "lib/my_module/lib/my_module" }
})
// In my_module.js 
define(["./dep"], function(dep) {

})

It tries to load /javascripts/dep rather than /javascript/lib/my_module/lib/dep.

I'm not sure if this is intended, but it would be nice if it loaded it relative to the actual url rather than the alias.

@jrburke
requirejs member

This is working as designed.

The relative ID is a relative module ID, and it is resolved relative to another ID. Since the ID for my_module.js is 'my_module' and the relative dependency is './dep', the resolved name is 'dep'.

There was a big discussion about this on the amd-implement list.

If you want to get relative ID resolution for items within a 'my_module', you can either place the relative dependencies in a 'my_module' directory that is a sibling to my_module.js, or create a my_module, place my_module.js inside that directory as 'main.js', and then consumers of the module can use 'my_module/main' to load it.

I know it can seem counterintuitive since module IDs are like paths. However, module IDs are stand-alone entities, particularly after a build when all modules have absolute IDs and no path info is delivered (in that case, what would be the absolute ID for the module as you describe in your ticket?).

@jrburke jrburke closed this Jan 7, 2012
@totty90

This might help: http://stackoverflow.com/questions/9302284/relative-paths-with-requirejs-modules-packages

require.config({
packages: [
{ name: 'packagename',
location: 'path/to/your/package/root', // default 'packagename'
main: 'scriptfileToLoad' // default 'main'
}]
... some other stuff ...
});

@adrian-moisa

Thanks @totty90 ! I finally get it, after reading the documentation about module IDs, relative paths and packages I managed to get the best of all.

Instead of:

paths: {
    'inputs': 'engine/inputs/inputs',
    ...
}

which doesn't allow loading relative modules inside module: engine/inputs/inputs.js:

I used:

packages : [
    {
        name: 'inputs',
        location : 'engine/inputs',
        main : 'inputs'
    },
    ...
]

and inputs.js:

define([
    './inputContexts', // Relative (used locally, no need to cluter main.js with this module)
    /* '../src/engine/inputs/inputContexts', // Absolute paths worked, but they are long and ugly */
    'constants' // Defined in paths (This is best suited for modules required in multiple places)
], function(
    inputContexts,
    constants
){
    ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment