node.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var ResolverFactory = require("./ResolverFactory");
  6. var NodeJsInputFileSystem = require("./NodeJsInputFileSystem");
  7. var CachedInputFileSystem = require("./CachedInputFileSystem");
  8. var nodeFileSystem = new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000);
  9. var nodeContext = {
  10. environments: [
  11. "node+es3+es5+process+native"
  12. ]
  13. };
  14. var asyncResolver = ResolverFactory.createResolver({
  15. extensions: [".js", ".json", ".node"],
  16. fileSystem: nodeFileSystem
  17. });
  18. module.exports = function resolve(context, path, request, callback) {
  19. if(typeof context === "string") {
  20. callback = request;
  21. request = path;
  22. path = context;
  23. context = nodeContext;
  24. }
  25. asyncResolver.resolve(context, path, request, callback);
  26. };
  27. var syncResolver = ResolverFactory.createResolver({
  28. extensions: [".js", ".json", ".node"],
  29. useSyncFileSystemCalls: true,
  30. fileSystem: nodeFileSystem
  31. });
  32. module.exports.sync = function resolveSync(context, path, request) {
  33. if(typeof context === "string") {
  34. request = path;
  35. path = context;
  36. context = nodeContext;
  37. }
  38. return syncResolver.resolveSync(context, path, request);
  39. };
  40. var asyncContextResolver = ResolverFactory.createResolver({
  41. extensions: [".js", ".json", ".node"],
  42. resolveToContext: true,
  43. fileSystem: nodeFileSystem
  44. });
  45. module.exports.context = function resolveContext(context, path, request, callback) {
  46. if(typeof context === "string") {
  47. callback = request;
  48. request = path;
  49. path = context;
  50. context = nodeContext;
  51. }
  52. asyncContextResolver.resolve(context, path, request, callback);
  53. };
  54. var syncContextResolver = ResolverFactory.createResolver({
  55. extensions: [".js", ".json", ".node"],
  56. resolveToContext: true,
  57. useSyncFileSystemCalls: true,
  58. fileSystem: nodeFileSystem
  59. });
  60. module.exports.context.sync = function resolveContextSync(context, path, request) {
  61. if(typeof context === "string") {
  62. request = path;
  63. path = context;
  64. context = nodeContext;
  65. }
  66. return syncContextResolver.resolveSync(context, path, request);
  67. };
  68. var asyncLoaderResolver = ResolverFactory.createResolver({
  69. extensions: [".js", ".json", ".node"],
  70. moduleExtensions: ["-loader"],
  71. mainFields: ["loader", "main"],
  72. fileSystem: nodeFileSystem
  73. });
  74. module.exports.loader = function resolveLoader(context, path, request, callback) {
  75. if(typeof context === "string") {
  76. callback = request;
  77. request = path;
  78. path = context;
  79. context = nodeContext;
  80. }
  81. asyncLoaderResolver.resolve(context, path, request, callback);
  82. };
  83. var syncLoaderResolver = ResolverFactory.createResolver({
  84. extensions: [".js", ".json", ".node"],
  85. moduleExtensions: ["-loader"],
  86. mainFields: ["loader", "main"],
  87. useSyncFileSystemCalls: true,
  88. fileSystem: nodeFileSystem
  89. });
  90. module.exports.loader.sync = function resolveLoaderSync(context, path, request) {
  91. if(typeof context === "string") {
  92. request = path;
  93. path = context;
  94. context = nodeContext;
  95. }
  96. return syncLoaderResolver.resolveSync(context, path, request);
  97. };
  98. module.exports.create = function create(options) {
  99. options = Object.assign({
  100. fileSystem: nodeFileSystem
  101. }, options);
  102. var resolver = ResolverFactory.createResolver(options);
  103. return function(context, path, request, callback) {
  104. if(typeof context === "string") {
  105. callback = request;
  106. request = path;
  107. path = context;
  108. context = nodeContext;
  109. }
  110. resolver.resolve(context, path, request, callback);
  111. };
  112. };
  113. module.exports.create.sync = function createSync(options) {
  114. options = Object.assign({
  115. useSyncFileSystemCalls: true,
  116. fileSystem: nodeFileSystem
  117. }, options);
  118. var resolver = ResolverFactory.createResolver(options);
  119. return function(context, path, request) {
  120. if(typeof context === "string") {
  121. request = path;
  122. path = context;
  123. context = nodeContext;
  124. }
  125. return resolver.resolveSync(context, path, request);
  126. };
  127. };
  128. // Export Resolver, FileSystems and Plugins
  129. module.exports.ResolverFactory = ResolverFactory;
  130. module.exports.NodeJsInputFileSystem = NodeJsInputFileSystem;
  131. module.exports.CachedInputFileSystem = CachedInputFileSystem;