a zip code crypto-currency system good for red ONLY

ResolverFactory.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var Resolver = require("./Resolver");
  6. var SyncAsyncFileSystemDecorator = require("./SyncAsyncFileSystemDecorator");
  7. var ParsePlugin = require("./ParsePlugin");
  8. var DescriptionFilePlugin = require("./DescriptionFilePlugin");
  9. var NextPlugin = require("./NextPlugin");
  10. var TryNextPlugin = require("./TryNextPlugin");
  11. var ModuleKindPlugin = require("./ModuleKindPlugin");
  12. var FileKindPlugin = require("./FileKindPlugin");
  13. var JoinRequestPlugin = require("./JoinRequestPlugin");
  14. var ModulesInHierachicDirectoriesPlugin = require("./ModulesInHierachicDirectoriesPlugin");
  15. var ModulesInRootPlugin = require("./ModulesInRootPlugin");
  16. var AliasPlugin = require("./AliasPlugin");
  17. var AliasFieldPlugin = require("./AliasFieldPlugin");
  18. var ConcordExtensionsPlugin = require("./ConcordExtensionsPlugin");
  19. var ConcordMainPlugin = require("./ConcordMainPlugin");
  20. var ConcordModulesPlugin = require("./ConcordModulesPlugin");
  21. var DirectoryExistsPlugin = require("./DirectoryExistsPlugin");
  22. var FileExistsPlugin = require("./FileExistsPlugin");
  23. var SymlinkPlugin = require("./SymlinkPlugin");
  24. var MainFieldPlugin = require("./MainFieldPlugin");
  25. var UseFilePlugin = require("./UseFilePlugin");
  26. var AppendPlugin = require("./AppendPlugin");
  27. var ResultPlugin = require("./ResultPlugin");
  28. var ModuleAppendPlugin = require("./ModuleAppendPlugin");
  29. var UnsafeCachePlugin = require("./UnsafeCachePlugin");
  30. exports.createResolver = function(options) {
  31. //// OPTIONS ////
  32. // A list of directories to resolve modules from, can be absolute path or folder name
  33. var modules = options.modules || ["node_modules"];
  34. // A list of description files to read from
  35. var descriptionFiles = options.descriptionFiles || ["package.json"];
  36. // A list of additional resolve plugins which should be applied
  37. // The slice is there to create a copy, because otherwise pushing into plugins
  38. // changes the original options.plugins array, causing duplicate plugins
  39. var plugins = (options.plugins && options.plugins.slice()) || [];
  40. // A list of main fields in description files
  41. var mainFields = options.mainFields || ["main"];
  42. // A list of alias fields in description files
  43. var aliasFields = options.aliasFields || [];
  44. // A list of main files in directories
  45. var mainFiles = options.mainFiles || ["index"];
  46. // A list of extensions which should be tried for files
  47. var extensions = options.extensions || [".js", ".json", ".node"];
  48. // Enforce that a extension from extensions must be used
  49. var enforceExtension = options.enforceExtension || false;
  50. // A list of module extensions which should be tried for modules
  51. var moduleExtensions = options.moduleExtensions || [];
  52. // Enforce that a extension from moduleExtensions must be used
  53. var enforceModuleExtension = options.enforceModuleExtension || false;
  54. // A list of module alias configurations or an object which maps key to value
  55. var alias = options.alias || [];
  56. // Resolve symlinks to their symlinked location
  57. var symlinks = typeof options.symlinks !== "undefined" ? options.symlinks : true;
  58. // Resolve to a context instead of a file
  59. var resolveToContext = options.resolveToContext || false;
  60. // Use this cache object to unsafely cache the successful requests
  61. var unsafeCache = options.unsafeCache || false;
  62. // Whether or not the unsafeCache should include request context as part of the cache key.
  63. var cacheWithContext = typeof options.cacheWithContext !== "undefined" ? options.cacheWithContext : true;
  64. // A function which decides whether a request should be cached or not.
  65. // an object is passed with `path` and `request` properties.
  66. var cachePredicate = options.cachePredicate || function() {
  67. return true;
  68. };
  69. // The file system which should be used
  70. var fileSystem = options.fileSystem;
  71. // Use only the sync variants of the file system calls
  72. var useSyncFileSystemCalls = options.useSyncFileSystemCalls;
  73. // A prepared Resolver to which the plugins are attached
  74. var resolver = options.resolver;
  75. //// options processing ////
  76. if(!resolver) {
  77. resolver = new Resolver(useSyncFileSystemCalls ? new SyncAsyncFileSystemDecorator(fileSystem) : fileSystem);
  78. }
  79. extensions = [].concat(extensions);
  80. moduleExtensions = [].concat(moduleExtensions);
  81. modules = mergeFilteredToArray([].concat(modules), function(item) {
  82. return !isAbsolutePath(item);
  83. });
  84. mainFields = mainFields.map(function(item) {
  85. if(typeof item === "string") {
  86. item = {
  87. name: item,
  88. forceRelative: true
  89. };
  90. }
  91. return item;
  92. });
  93. if(typeof alias === "object" && !Array.isArray(alias)) {
  94. alias = Object.keys(alias).map(function(key) {
  95. var onlyModule = false;
  96. var obj = alias[key];
  97. if(/\$$/.test(key)) {
  98. onlyModule = true;
  99. key = key.substr(0, key.length - 1);
  100. }
  101. if(typeof obj === "string") {
  102. obj = {
  103. alias: obj
  104. };
  105. }
  106. obj = Object.assign({
  107. name: key,
  108. onlyModule: onlyModule
  109. }, obj);
  110. return obj;
  111. });
  112. }
  113. if(unsafeCache && typeof unsafeCache !== "object") {
  114. unsafeCache = {};
  115. }
  116. //// pipeline ////
  117. // resolve
  118. if(unsafeCache) {
  119. plugins.push(new UnsafeCachePlugin("resolve", cachePredicate, unsafeCache, cacheWithContext, "new-resolve"));
  120. plugins.push(new ParsePlugin("new-resolve", "parsed-resolve"));
  121. } else {
  122. plugins.push(new ParsePlugin("resolve", "parsed-resolve"));
  123. }
  124. // parsed-resolve
  125. plugins.push(new DescriptionFilePlugin("parsed-resolve", descriptionFiles, "described-resolve"));
  126. plugins.push(new NextPlugin("after-parsed-resolve", "described-resolve"));
  127. // described-resolve
  128. alias.forEach(function(item) {
  129. plugins.push(new AliasPlugin("described-resolve", item, "resolve"));
  130. });
  131. plugins.push(new ConcordModulesPlugin("described-resolve", {}, "resolve"));
  132. aliasFields.forEach(function(item) {
  133. plugins.push(new AliasFieldPlugin("described-resolve", item, "resolve"));
  134. });
  135. plugins.push(new ModuleKindPlugin("after-described-resolve", "raw-module"));
  136. plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));
  137. // raw-module
  138. moduleExtensions.forEach(function(item) {
  139. plugins.push(new ModuleAppendPlugin("raw-module", item, "module"));
  140. });
  141. if(!enforceModuleExtension)
  142. plugins.push(new TryNextPlugin("raw-module", null, "module"));
  143. // module
  144. modules.forEach(function(item) {
  145. if(Array.isArray(item))
  146. plugins.push(new ModulesInHierachicDirectoriesPlugin("module", item, "resolve"));
  147. else
  148. plugins.push(new ModulesInRootPlugin("module", item, "resolve"));
  149. });
  150. // relative
  151. plugins.push(new DescriptionFilePlugin("relative", descriptionFiles, "described-relative"));
  152. plugins.push(new NextPlugin("after-relative", "described-relative"));
  153. // described-relative
  154. plugins.push(new FileKindPlugin("described-relative", "raw-file"));
  155. plugins.push(new TryNextPlugin("described-relative", "as directory", "directory"));
  156. // directory
  157. plugins.push(new DirectoryExistsPlugin("directory", "existing-directory"));
  158. if(resolveToContext) {
  159. // existing-directory
  160. plugins.push(new NextPlugin("existing-directory", "resolved"));
  161. } else {
  162. // existing-directory
  163. plugins.push(new ConcordMainPlugin("existing-directory", {}, "resolve"));
  164. mainFields.forEach(function(item) {
  165. plugins.push(new MainFieldPlugin("existing-directory", item, "resolve"));
  166. });
  167. mainFiles.forEach(function(item) {
  168. plugins.push(new UseFilePlugin("existing-directory", item, "undescribed-raw-file"));
  169. });
  170. // undescribed-raw-file
  171. plugins.push(new DescriptionFilePlugin("undescribed-raw-file", descriptionFiles, "raw-file"));
  172. plugins.push(new NextPlugin("after-undescribed-raw-file", "raw-file"));
  173. // raw-file
  174. if(!enforceExtension)
  175. plugins.push(new TryNextPlugin("raw-file", "no extension", "file"));
  176. plugins.push(new ConcordExtensionsPlugin("raw-file", {}, "file"));
  177. extensions.forEach(function(item) {
  178. plugins.push(new AppendPlugin("raw-file", item, "file"));
  179. });
  180. // file
  181. alias.forEach(function(item) {
  182. plugins.push(new AliasPlugin("file", item, "resolve"));
  183. });
  184. plugins.push(new ConcordModulesPlugin("file", {}, "resolve"));
  185. aliasFields.forEach(function(item) {
  186. plugins.push(new AliasFieldPlugin("file", item, "resolve"));
  187. });
  188. if(symlinks)
  189. plugins.push(new SymlinkPlugin("file", "relative"));
  190. plugins.push(new FileExistsPlugin("file", "existing-file"));
  191. // existing-file
  192. plugins.push(new NextPlugin("existing-file", "resolved"));
  193. }
  194. // resolved
  195. plugins.push(new ResultPlugin("resolved"));
  196. //// RESOLVER ////
  197. plugins.forEach(function(plugin) {
  198. resolver.apply(plugin);
  199. });
  200. return resolver;
  201. };
  202. function mergeFilteredToArray(array, filter) {
  203. return array.reduce(function(array, item) {
  204. if(filter(item)) {
  205. var lastElement = array[array.length - 1];
  206. if(Array.isArray(lastElement)) {
  207. lastElement.push(item);
  208. } else {
  209. array.push([item]);
  210. }
  211. return array;
  212. } else {
  213. array.push(item);
  214. return array;
  215. }
  216. }, []);
  217. }
  218. function isAbsolutePath(path) {
  219. return /^[A-Z]:|^\//.test(path);
  220. }