Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(new NodeJsInputFileSystem(), 4000);
  10. const nodeContext = {
  11. environments: [
  12. "node+es3+es5+process+native"
  13. ]
  14. };
  15. const asyncResolver = ResolverFactory.createResolver({
  16. extensions: [".js", ".json", ".node"],
  17. fileSystem: nodeFileSystem
  18. });
  19. module.exports = function resolve(context, path, request, resolveContext, callback) {
  20. if(typeof context === "string") {
  21. callback = resolveContext;
  22. resolveContext = request;
  23. request = path;
  24. path = context;
  25. context = nodeContext;
  26. }
  27. if(typeof callback !== "function") {
  28. callback = resolveContext;
  29. }
  30. asyncResolver.resolve(context, path, request, resolveContext, callback);
  31. };
  32. const syncResolver = ResolverFactory.createResolver({
  33. extensions: [".js", ".json", ".node"],
  34. useSyncFileSystemCalls: true,
  35. fileSystem: nodeFileSystem
  36. });
  37. module.exports.sync = function resolveSync(context, path, request) {
  38. if(typeof context === "string") {
  39. request = path;
  40. path = context;
  41. context = nodeContext;
  42. }
  43. return syncResolver.resolveSync(context, path, request);
  44. };
  45. const asyncContextResolver = ResolverFactory.createResolver({
  46. extensions: [".js", ".json", ".node"],
  47. resolveToContext: true,
  48. fileSystem: nodeFileSystem
  49. });
  50. module.exports.context = function resolveContext(context, path, request, resolveContext, callback) {
  51. if(typeof context === "string") {
  52. callback = resolveContext;
  53. resolveContext = request;
  54. request = path;
  55. path = context;
  56. context = nodeContext;
  57. }
  58. if(typeof callback !== "function") {
  59. callback = resolveContext;
  60. }
  61. asyncContextResolver.resolve(context, path, request, resolveContext, callback);
  62. };
  63. const syncContextResolver = ResolverFactory.createResolver({
  64. extensions: [".js", ".json", ".node"],
  65. resolveToContext: true,
  66. useSyncFileSystemCalls: true,
  67. fileSystem: nodeFileSystem
  68. });
  69. module.exports.context.sync = function resolveContextSync(context, path, request) {
  70. if(typeof context === "string") {
  71. request = path;
  72. path = context;
  73. context = nodeContext;
  74. }
  75. return syncContextResolver.resolveSync(context, path, request);
  76. };
  77. const asyncLoaderResolver = ResolverFactory.createResolver({
  78. extensions: [".js", ".json", ".node"],
  79. moduleExtensions: ["-loader"],
  80. mainFields: ["loader", "main"],
  81. fileSystem: nodeFileSystem
  82. });
  83. module.exports.loader = function resolveLoader(context, path, request, resolveContext, callback) {
  84. if(typeof context === "string") {
  85. callback = resolveContext;
  86. resolveContext = request;
  87. request = path;
  88. path = context;
  89. context = nodeContext;
  90. }
  91. if(typeof callback !== "function") {
  92. callback = resolveContext;
  93. }
  94. asyncLoaderResolver.resolve(context, path, request, resolveContext, callback);
  95. };
  96. const syncLoaderResolver = ResolverFactory.createResolver({
  97. extensions: [".js", ".json", ".node"],
  98. moduleExtensions: ["-loader"],
  99. mainFields: ["loader", "main"],
  100. useSyncFileSystemCalls: true,
  101. fileSystem: nodeFileSystem
  102. });
  103. module.exports.loader.sync = function resolveLoaderSync(context, path, request) {
  104. if(typeof context === "string") {
  105. request = path;
  106. path = context;
  107. context = nodeContext;
  108. }
  109. return syncLoaderResolver.resolveSync(context, path, request);
  110. };
  111. module.exports.create = function create(options) {
  112. options = Object.assign({
  113. fileSystem: nodeFileSystem
  114. }, options);
  115. const resolver = ResolverFactory.createResolver(options);
  116. return function(context, path, request, resolveContext, callback) {
  117. if(typeof context === "string") {
  118. callback = resolveContext;
  119. resolveContext = request;
  120. request = path;
  121. path = context;
  122. context = nodeContext;
  123. }
  124. if(typeof callback !== "function") {
  125. callback = resolveContext;
  126. }
  127. resolver.resolve(context, path, request, resolveContext, callback);
  128. };
  129. };
  130. module.exports.create.sync = function createSync(options) {
  131. options = Object.assign({
  132. useSyncFileSystemCalls: true,
  133. fileSystem: nodeFileSystem
  134. }, options);
  135. const resolver = ResolverFactory.createResolver(options);
  136. return function(context, path, request) {
  137. if(typeof context === "string") {
  138. request = path;
  139. path = context;
  140. context = nodeContext;
  141. }
  142. return resolver.resolveSync(context, path, request);
  143. };
  144. };
  145. // Export Resolver, FileSystems and Plugins
  146. module.exports.ResolverFactory = ResolverFactory;
  147. module.exports.NodeJsInputFileSystem = NodeJsInputFileSystem;
  148. module.exports.CachedInputFileSystem = CachedInputFileSystem;