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

ContextModuleFactory.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const path = require("path");
  8. const {
  9. Tapable,
  10. AsyncSeriesWaterfallHook,
  11. SyncWaterfallHook
  12. } = require("tapable");
  13. const ContextModule = require("./ContextModule");
  14. const ContextElementDependency = require("./dependencies/ContextElementDependency");
  15. /** @typedef {import("./Module")} Module */
  16. const EMPTY_RESOLVE_OPTIONS = {};
  17. module.exports = class ContextModuleFactory extends Tapable {
  18. constructor(resolverFactory) {
  19. super();
  20. this.hooks = {
  21. /** @type {AsyncSeriesWaterfallHook<TODO>} */
  22. beforeResolve: new AsyncSeriesWaterfallHook(["data"]),
  23. /** @type {AsyncSeriesWaterfallHook<TODO>} */
  24. afterResolve: new AsyncSeriesWaterfallHook(["data"]),
  25. /** @type {SyncWaterfallHook<string[]>} */
  26. contextModuleFiles: new SyncWaterfallHook(["files"]),
  27. /** @type {SyncWaterfallHook<TODO[]>} */
  28. alternatives: new AsyncSeriesWaterfallHook(["modules"])
  29. };
  30. this._pluginCompat.tap("ContextModuleFactory", options => {
  31. switch (options.name) {
  32. case "before-resolve":
  33. case "after-resolve":
  34. case "alternatives":
  35. options.async = true;
  36. break;
  37. }
  38. });
  39. this.resolverFactory = resolverFactory;
  40. }
  41. create(data, callback) {
  42. const context = data.context;
  43. const dependencies = data.dependencies;
  44. const resolveOptions = data.resolveOptions;
  45. const dependency = dependencies[0];
  46. this.hooks.beforeResolve.callAsync(
  47. Object.assign(
  48. {
  49. context: context,
  50. dependencies: dependencies,
  51. resolveOptions
  52. },
  53. dependency.options
  54. ),
  55. (err, beforeResolveResult) => {
  56. if (err) return callback(err);
  57. // Ignored
  58. if (!beforeResolveResult) return callback();
  59. const context = beforeResolveResult.context;
  60. const request = beforeResolveResult.request;
  61. const resolveOptions = beforeResolveResult.resolveOptions;
  62. let loaders,
  63. resource,
  64. loadersPrefix = "";
  65. const idx = request.lastIndexOf("!");
  66. if (idx >= 0) {
  67. let loadersRequest = request.substr(0, idx + 1);
  68. let i;
  69. for (
  70. i = 0;
  71. i < loadersRequest.length && loadersRequest[i] === "!";
  72. i++
  73. ) {
  74. loadersPrefix += "!";
  75. }
  76. loadersRequest = loadersRequest
  77. .substr(i)
  78. .replace(/!+$/, "")
  79. .replace(/!!+/g, "!");
  80. if (loadersRequest === "") {
  81. loaders = [];
  82. } else {
  83. loaders = loadersRequest.split("!");
  84. }
  85. resource = request.substr(idx + 1);
  86. } else {
  87. loaders = [];
  88. resource = request;
  89. }
  90. const contextResolver = this.resolverFactory.get(
  91. "context",
  92. resolveOptions || EMPTY_RESOLVE_OPTIONS
  93. );
  94. const loaderResolver = this.resolverFactory.get(
  95. "loader",
  96. EMPTY_RESOLVE_OPTIONS
  97. );
  98. asyncLib.parallel(
  99. [
  100. callback => {
  101. contextResolver.resolve(
  102. {},
  103. context,
  104. resource,
  105. {},
  106. (err, result) => {
  107. if (err) return callback(err);
  108. callback(null, result);
  109. }
  110. );
  111. },
  112. callback => {
  113. asyncLib.map(
  114. loaders,
  115. (loader, callback) => {
  116. loaderResolver.resolve(
  117. {},
  118. context,
  119. loader,
  120. {},
  121. (err, result) => {
  122. if (err) return callback(err);
  123. callback(null, result);
  124. }
  125. );
  126. },
  127. callback
  128. );
  129. }
  130. ],
  131. (err, result) => {
  132. if (err) return callback(err);
  133. this.hooks.afterResolve.callAsync(
  134. Object.assign(
  135. {
  136. addon:
  137. loadersPrefix +
  138. result[1].join("!") +
  139. (result[1].length > 0 ? "!" : ""),
  140. resource: result[0],
  141. resolveDependencies: this.resolveDependencies.bind(this)
  142. },
  143. beforeResolveResult
  144. ),
  145. (err, result) => {
  146. if (err) return callback(err);
  147. // Ignored
  148. if (!result) return callback();
  149. return callback(
  150. null,
  151. new ContextModule(result.resolveDependencies, result)
  152. );
  153. }
  154. );
  155. }
  156. );
  157. }
  158. );
  159. }
  160. resolveDependencies(fs, options, callback) {
  161. const cmf = this;
  162. let resource = options.resource;
  163. let resourceQuery = options.resourceQuery;
  164. let recursive = options.recursive;
  165. let regExp = options.regExp;
  166. let include = options.include;
  167. let exclude = options.exclude;
  168. if (!regExp || !resource) return callback(null, []);
  169. const addDirectory = (directory, callback) => {
  170. fs.readdir(directory, (err, files) => {
  171. if (err) return callback(err);
  172. files = cmf.hooks.contextModuleFiles.call(files);
  173. if (!files || files.length === 0) return callback(null, []);
  174. asyncLib.map(
  175. files.filter(p => p.indexOf(".") !== 0),
  176. (segment, callback) => {
  177. const subResource = path.join(directory, segment);
  178. if (!exclude || !subResource.match(exclude)) {
  179. fs.stat(subResource, (err, stat) => {
  180. if (err) {
  181. if (err.code === "ENOENT") {
  182. // ENOENT is ok here because the file may have been deleted between
  183. // the readdir and stat calls.
  184. return callback();
  185. } else {
  186. return callback(err);
  187. }
  188. }
  189. if (stat.isDirectory()) {
  190. if (!recursive) return callback();
  191. addDirectory.call(this, subResource, callback);
  192. } else if (
  193. stat.isFile() &&
  194. (!include || subResource.match(include))
  195. ) {
  196. const obj = {
  197. context: resource,
  198. request:
  199. "." +
  200. subResource.substr(resource.length).replace(/\\/g, "/")
  201. };
  202. this.hooks.alternatives.callAsync(
  203. [obj],
  204. (err, alternatives) => {
  205. if (err) return callback(err);
  206. alternatives = alternatives
  207. .filter(obj => regExp.test(obj.request))
  208. .map(obj => {
  209. const dep = new ContextElementDependency(
  210. obj.request + resourceQuery,
  211. obj.request
  212. );
  213. dep.optional = true;
  214. return dep;
  215. });
  216. callback(null, alternatives);
  217. }
  218. );
  219. } else {
  220. callback();
  221. }
  222. });
  223. } else {
  224. callback();
  225. }
  226. },
  227. (err, result) => {
  228. if (err) return callback(err);
  229. if (!result) return callback(null, []);
  230. callback(
  231. null,
  232. result.filter(Boolean).reduce((a, i) => a.concat(i), [])
  233. );
  234. }
  235. );
  236. });
  237. };
  238. addDirectory(resource, callback);
  239. }
  240. };