What is the difference between the Requirejs PATHS and MAP properties

Asked

Viewed 77 times

3

In theory it seems that the two configuration properties of the Requirejs has as its focus the same action.

Does anyone know the differences of how both settings work?

PATHS:

require.config({
  paths: {
    'modulo': 'endereco/para/o/modulo'
  }
 });

MAP:

require.config({
  map: {
    '*': {modulo: 'endereco/para/o/modulo'}
  }
 });

1 answer

2


According to the documentation:

The estate paths allows you to map modules that are to other locations than the URL basis of the script.

One of the uses indicated in documentation is for fallback, that is, allow the use of the script from a CDN in production and from a local URL for development.

And example is:

requirejs.config({
    //To get timely, correct error triggers in IE, force a define/shim exports check.
    enforceDefine: true,
    paths: {
        jquery: [
            'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min',
            //If the CDN location fails, load from this location
            'lib/jquery'
        ]
    }
});

//Later
require(['jquery'], function ($) {
});

Use to set alternative locations for the same module.


On the other hand, the property map allows scripts (modules) different from the same project that may require a third module to use different versions of this module.

Example:

requirejs.config({
    map: {
        'newmodule': {
            'foo': 'foo1.2'
        },
        'oldmodule': {
            'foo': 'foo1.0'
        }
    }
});

According to the above configuration, when newmodule use require('foo') he will receive foo1.2.js. How much oldmodule do the same, he will receive foo1.0.js.

Use to define which version of a particular module should be imported by each module.

  • 1

    Your reply is clearer than the documentation, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.