ConcordExtensionsPlugin.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. };