Skip to content

Instantly share code, notes, and snippets.

@jrburke
Created January 18, 2012 00:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrburke/1630091 to your computer and use it in GitHub Desktop.
Save jrburke/1630091 to your computer and use it in GitHub Desktop.
Rough draft of common config understood by loaders

Attempt to list some possible standard AMD loader configuration values.

An AMD loader is not required to implement all of these configuration values, but if it does provide a capability that is satisfied by these configuration values, it should use these configuration values and structures.

baseUrl

String: Indicates the root used for ID-to-path resolutions. Relative paths are relative to the current working directory. In web browsers, the current working directory is the directory containing the web page running the script.

Example:

{
    baseUrl: './foo/bar'
}

paths

Object. A property in the object is an absolute module ID prefix, and the value can either be a String or Object that indicates properties to give to module IDs that start with the ID prefix.

Values can be either:

  • String: a path value to use for the module ID prefix. If it is a relative path, it it relative to baseUrl. It can be an absolute path, like /top/level/dir or ``//top/level/dirorhttp://some.domain.com/top/level/dir`.
  • Object: an object that can have the following properties:
    • location: String. Optional. a path value to use for the module ID prefix. Behaves the same as if a String is used for that paths value.
    • map: Object. Optional. See [details under the map section.

packages

Array of package configuration (packageConfig) objects. Package configuration is for traditional CommonJS packages, which has different path lookup rules than the default ID-to-path lookup rules used by a loader.

Default lookup rules are ,baseUrl + 'module/id' + .js, where paths config can be used to map part of 'module/id' to another path.

Package lookup rules use a special lookup rule when 'packageName' is used. For the "main module" of a package, instead of it being baseUrl + 'packageName' + '.js', it is:

baseUrl + (packageConfig.location || 'packageName') + '/' + (packageConfig.main || 'main') + '.js'

For 'packageName/otherId', the rule is similar to paths, but packageConfig is used instead:

baseUrl + (packageConfig.location || 'packageName') + '/' + 'otherId' + '.js'

The package configuration object can either be:

  • String: The first segment of an absolute module ID, which indicates the package name. So, for a module ID of some/thing/here, the first segment of the module ID is some. In that example some is the package name, and the value used for this config value.

  • Object: a configuration object that can have the following properties:

    • name: String. first segment of an absolute module ID (see String section immediately above).
    • location: String. Optional.
    • main: The module ID to use inside the package for the "main module".
    • map: Object. Optional. See [details under the map section.

    packages: [ { name: 'dojo', location: 'dojo/1.7.1', main:'main' } ]

map

Object. This property is available under a paths or packages config value. It specifies an internal map to use for dependencies under that paths/packages config. The property name is a module prefix. Only the first segment of a module prefix is allowed. The property value is a string that mentions which top level module ID prefix to use instead of the property name.

Example:

Example:

{
    paths: {
        'some/thing': {
            map: {
                'c': 'c-1'
            }
        }
    }
}

For modules whose absolute ID starts with 'some/thing', if they have a dependency on anything whose absolute ID starts with 'c/', treat it as an ID that starts with 'c-1/' instead.

config

A configuration object available to modules that match the absolute module ID listed in the config object. Modules with a matching module ID can access the configuration object via module.config, which is an Object value. The Object value is mutable.

Example:

{
    config: {
        'some/module/id': {
            limit: 40
        }
    }
}

For the module that resolves to the absolute module ID of 'some/module/id', module.config.limit === 40

@gigafied
Copy link

While it is nice to be able to remap entire paths, it would be good to be able to specify something like:

paths: {
   'module/group': 'moduleGroup.min.js'
}

or

paths: {
   'module/group/*': 'moduleGroup.min'
}

This way, if I do:

require("module/group/A", function(A){;}

It resolves to moduleGroup.min.js and not moduleGroup.min/A.js

Maybe the map property can solve this by doing something like:

{
    paths: {
        'module/group' : {
            map: {
                '*': 'moduleGroup.min'
            }
        }
    }
}

@jrburke
Copy link
Author

jrburke commented Feb 28, 2012

Updated the paths value to include this:

If it is a relative path, it it relative to baseUrl. It can be an absolute path, like /top/level/dir or ``//top/level/dirorhttp://some.domain.com/top/level/dir`.

@sshmyg
Copy link

sshmyg commented Aug 27, 2013

Can I use something like this
config: {
'*': {
limit: 40
}
}

to pass this param to every module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment