Requirejs: paths vs map

后端 未结 3 2007
自闭症患者
自闭症患者 2020-12-17 08:29

Trying to understand where it\'s right to use \"map\" with a wildcard vs \"paths\".

Looking at the require source (but certainly not being 100% fluent with it) it s

相关标签:
3条回答
  • 2020-12-17 08:46

    From the RequireJS Docs "In addition, the paths config is only for setting up root paths for module IDs, not for mapping one module ID to another one."

    This means "paths" is meant for mapping just the path to your resource when it is not in the default location (baseUrl). I guess this is what you were trying to do.

    On the other hand, with "map" you can have several versions of your resource (foo1, foo2...) which you can map to be loaded from different paths (i.e. you want to load foo1 from a desktop browser and foo2 which is a modification of the first one from a mobile browser).

    So, unless you have different versions of foo I would use "path" although you are right and "map" would also work in that case.

    0 讨论(0)
  • 2020-12-17 08:47

    There is also one other important difference with the map config. You define a prefix that will be used in mappings.

    For your example this would mean that foo will be mapped to stuff/foo but also foo/bar/baz/bam will be mapped to stuff/foo/bar/baz/bam.

    0 讨论(0)
  • 2020-12-17 08:50

    I have found one difference, and that's in the case of requirejs loader plugins, example Example: define(['cs!module'], function(){...} ) for CoffeeScript.

    Using the map:* part of config for declaring the plugins (and paths for dependent modules) worked in the browser. However, in Node, Requirejs would fail to locate the loader plugins unless they were under paths.

    In the end, for the sake of being able to run the same config in Node and the browser, I got rid of the map:* section, and declared everything in paths and it works just fine for me now, even if I'm still hoping to get some clarification on why.

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