node.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ResolverFactory = require("./ResolverFactory");
  7. const NodeJsInputFileSystem = require("./NodeJsInputFileSystem");
  8. const CachedInputFileSystem = require("./CachedInputFileSystem");
  9. const nodeFileSystem = new CachedInputFileSystem(
  10. new NodeJsInputFileSystem(),
  11. 4000
  12. );
  13. const nodeContext = {
  14. environments: ["node+es3+es5+process+native"]
  15. };
  16. const asyncResolver = ResolverFactory.createResolver({
  17. extensions: [".js", ".json", ".node"],
  18. fileSystem: nodeFileSystem
  19. });
  20. module.exports = function resolve(
  21. context,
  22. path,
  23. request,
  24. resolveContext,
  25. callback
  26. ) {
  27. if (typeof context === "string") {
  28. callback = resolveContext;
  29. resolveContext = request;
  30. request = path;
  31. path = context;
  32. context = nodeContext;
  33. }
  34. if (typeof callback !== "function") {
  35. callback = resolveContext;
  36. }
  37. asyncResolver.resolve(context, path, request, resolveContext, callback);
  38. };
  39. const syncResolver = ResolverFactory.createResolver({
  40. extensions: [".js", ".json", ".node"],
  41. useSyncFileSystemCalls: true,
  42. fileSystem: nodeFileSystem
  43. });
  44. module.exports.sync = function resolveSync(context, path, request) {
  45. if (typeof context === "string") {
  46. request = path;
  47. path = context;
  48. context = nodeContext;
  49. }
  50. return syncResolver.resolveSync(context, path, request);
  51. };
  52. const asyncContextResolver = ResolverFactory.createResolver({
  53. extensions: [".js", ".json", ".node"],
  54. resolveToContext: true,
  55. fileSystem: nodeFileSystem
  56. });
  57. module.exports.context = function resolveContext(
  58. context,
  59. path,
  60. request,
  61. resolveContext,
  62. callback
  63. ) {
  64. if (typeof context === "string") {
  65. callback = resolveContext;
  66. resolveContext = request;
  67. request = path;
  68. path = context;
  69. context = nodeContext;
  70. }
  71. if (typeof callback !== "function") {
  72. callback = resolveContext;
  73. }
  74. asyncContextResolver.resolve(
  75. context,
  76. path,
  77. request,
  78. resolveContext,
  79. callback
  80. );
  81. };
  82. const syncContextResolver = ResolverFactory.createResolver({
  83. extensions: [".js", ".json", ".node"],
  84. resolveToContext: true,
  85. useSyncFileSystemCalls: true,
  86. fileSystem: nodeFileSystem
  87. });
  88. module.exports.context.sync = function resolveContextSync(
  89. context,
  90. path,
  91. request
  92. ) {
  93. if (typeof context === "string") {
  94. request = path;
  95. path = context;
  96. context = nodeContext;
  97. }
  98. return syncContextResolver.resolveSync(context, path, request);
  99. };
  100. const asyncLoaderResolver = ResolverFactory.createResolver({
  101. extensions: [".js", ".json", ".node"],
  102. moduleExtensions: ["-loader"],
  103. mainFields: ["loader", "main"],
  104. fileSystem: nodeFileSystem
  105. });
  106. module.exports.loader = function resolveLoader(
  107. context,
  108. path,
  109. request,
  110. resolveContext,
  111. callback
  112. ) {
  113. if (typeof context === "string") {
  114. callback = resolveContext;
  115. resolveContext = request;
  116. request = path;
  117. path = context;
  118. context = nodeContext;
  119. }
  120. if (typeof callback !== "function") {
  121. callback = resolveContext;
  122. }
  123. asyncLoaderResolver.resolve(context, path, request, resolveContext, callback);
  124. };
  125. const syncLoaderResolver = ResolverFactory.createResolver({
  126. extensions: [".js", ".json", ".node"],
  127. moduleExtensions: ["-loader"],
  128. mainFields: ["loader", "main"],
  129. useSyncFileSystemCalls: true,
  130. fileSystem: nodeFileSystem
  131. });
  132. module.exports.loader.sync = function resolveLoaderSync(
  133. context,
  134. path,
  135. request
  136. ) {
  137. if (typeof context === "string") {
  138. request = path;
  139. path = context;
  140. context = nodeContext;
  141. }
  142. return syncLoaderResolver.resolveSync(context, path, request);
  143. };
  144. module.exports.create = function create(options) {
  145. options = Object.assign(
  146. {
  147. fileSystem: nodeFileSystem
  148. },
  149. options
  150. );
  151. const resolver = ResolverFactory.createResolver(options);
  152. return function(context, path, request, resolveContext, callback) {
  153. if (typeof context === "string") {
  154. callback = resolveContext;
  155. resolveContext = request;
  156. request = path;
  157. path = context;
  158. context = nodeContext;
  159. }
  160. if (typeof callback !== "function") {
  161. callback = resolveContext;
  162. }
  163. resolver.resolve(context, path, request, resolveContext, callback);
  164. };
  165. };
  166. module.exports.create.sync = function createSync(options) {
  167. options = Object.assign(
  168. {
  169. useSyncFileSystemCalls: true,
  170. fileSystem: nodeFileSystem
  171. },
  172. options
  173. );
  174. const resolver = ResolverFactory.createResolver(options);
  175. return function(context, path, request) {
  176. if (typeof context === "string") {
  177. request = path;
  178. path = context;
  179. context = nodeContext;
  180. }
  181. return resolver.resolveSync(context, path, request);
  182. };
  183. };
  184. // Export Resolver, FileSystems and Plugins
  185. module.exports.ResolverFactory = ResolverFactory;
  186. module.exports.NodeJsInputFileSystem = NodeJsInputFileSystem;
  187. module.exports.CachedInputFileSystem = CachedInputFileSystem;