RequireJS relative paths

前端 未结 2 1602
清歌不尽
清歌不尽 2020-12-23 10:15

I\'m new to RequireJS. I\'m writing a number of custom bindings in Knockout.js and want to split them up using modules.

The layout of my code at the moment is:

相关标签:
2条回答
  • 2020-12-23 10:32

    Relative IDs are resolved relative to the module ID within which the ID is resolved. See AMD spec's module id format section.

    There are two ways to frame a relative dependency ID into a correct context/scope:

    Define call

    Define call is the start/definition of "module." All dependencies asked for within define() call are scoped to be within/relative to that module's ID. Example:

    // does not matter what the file name is.
    define(
        'hand/named/module'
        , ['./child']
        , factoryFunction
    )
    

    or

    // inside of 'hand/named/module.js' file.
    define(
        ['./child']
        , factoryFunction
    )
    

    In both of the above cases, ./child is resolved against the module ID defined by the define() call. The module id in both cases is hand/named/module and the ./child is resolved to hand/named/child (+ '.js' obviously, when time comes to get it)

    "Scoped" require

    You can change the scope of require call from global to local by overriding it. You actually don't need to override / keep the name require, it's the meaning of what it does changes. The require functionality becomes "local" to a particular module.

    // inside 'hand/named/module.js' file
    define(
        ['require']
        , function(myLocalRequire){
            require('./child', function(){
                // outcome A
            })
            myLocalRequire('./child', function(){
                // outcome B
            })
        }
    )
    

    There in outcome A you continue to use "global" require - the one attached to parent scope. Your ./child resolves to baseURL + '/child'

    The outcome B is locally-scoped, tied to module id hand/named/module so, ./child is resolved to hand/named/child

    What @CristiPufu recommended is to override the global require variable with local object that will be local only to the scope of that function:

    // inside 'hand/named/module.js' file
    define(
        ['require']
        , function(require){
            return function(){
                // here we have access only to "local" require,
                // since in the function declaration you decided to
                // override the 'require' variable with new object.
                // All code outside of this function will use global require.
                require('./child', function(){
                    // outcome B
                })
            }
        }
    )
    

    My preference is to put all relative resources inside define call. Makes them explicit and meeningfull as it's clear what they are relative to.

    0 讨论(0)
  • 2020-12-23 10:40

    Use "packages" in require config. Here the valid answer for you question topic

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

    Inside of package you will be able to use relative paths.

    0 讨论(0)
提交回复
热议问题