You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const concord = require("./concord");
  7. const DescriptionFileUtils = require("./DescriptionFileUtils");
  8. const forEachBail = require("./forEachBail");
  9. module.exports = class ConcordExtensionsPlugin {
  10. constructor(source, options, target) {
  11. this.source = source;
  12. this.options = options;
  13. this.target = target;
  14. }
  15. apply(resolver) {
  16. const target = resolver.ensureHook(this.target);
  17. resolver
  18. .getHook(this.source)
  19. .tapAsync(
  20. "ConcordExtensionsPlugin",
  21. (request, resolveContext, callback) => {
  22. const concordField = DescriptionFileUtils.getField(
  23. request.descriptionFileData,
  24. "concord"
  25. );
  26. if (!concordField) return callback();
  27. const extensions = concord.getExtensions(
  28. request.context,
  29. concordField
  30. );
  31. if (!extensions) return callback();
  32. forEachBail(
  33. extensions,
  34. (appending, callback) => {
  35. const obj = Object.assign({}, request, {
  36. path: request.path + appending,
  37. relativePath:
  38. request.relativePath && request.relativePath + appending
  39. });
  40. resolver.doResolve(
  41. target,
  42. obj,
  43. "concord extension: " + appending,
  44. resolveContext,
  45. callback
  46. );
  47. },
  48. (err, result) => {
  49. if (err) return callback(err);
  50. // Don't allow other processing
  51. if (result === undefined) return callback(null, null);
  52. callback(null, result);
  53. }
  54. );
  55. }
  56. );
  57. }
  58. };