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.

68 lines
1.8 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 getInnerRequest = require("./getInnerRequest");
  9. module.exports = class ConcordModulesPlugin {
  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("ConcordModulesPlugin", (request, resolveContext, callback) => {
  20. const innerRequest = getInnerRequest(resolver, request);
  21. if (!innerRequest) return callback();
  22. const concordField = DescriptionFileUtils.getField(
  23. request.descriptionFileData,
  24. "concord"
  25. );
  26. if (!concordField) return callback();
  27. const data = concord.matchModule(
  28. request.context,
  29. concordField,
  30. innerRequest
  31. );
  32. if (data === innerRequest) return callback();
  33. if (data === undefined) return callback();
  34. if (data === false) {
  35. const ignoreObj = Object.assign({}, request, {
  36. path: false
  37. });
  38. return callback(null, ignoreObj);
  39. }
  40. const obj = Object.assign({}, request, {
  41. path: request.descriptionFileRoot,
  42. request: data
  43. });
  44. resolver.doResolve(
  45. target,
  46. obj,
  47. "aliased from description file " +
  48. request.descriptionFilePath +
  49. " with mapping '" +
  50. innerRequest +
  51. "' to '" +
  52. data +
  53. "'",
  54. resolveContext,
  55. (err, result) => {
  56. if (err) return callback(err);
  57. // Don't allow other aliasing or raw request
  58. if (result === undefined) return callback(null, null);
  59. callback(null, result);
  60. }
  61. );
  62. });
  63. }
  64. };