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.

44 lines
1.2 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 path = require("path");
  7. const concord = require("./concord");
  8. const DescriptionFileUtils = require("./DescriptionFileUtils");
  9. module.exports = class ConcordMainPlugin {
  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("ConcordMainPlugin", (request, resolveContext, callback) => {
  20. if (request.path !== request.descriptionFileRoot) return callback();
  21. const concordField = DescriptionFileUtils.getField(
  22. request.descriptionFileData,
  23. "concord"
  24. );
  25. if (!concordField) return callback();
  26. const mainModule = concord.getMain(request.context, concordField);
  27. if (!mainModule) return callback();
  28. const obj = Object.assign({}, request, {
  29. request: mainModule
  30. });
  31. const filename = path.basename(request.descriptionFilePath);
  32. return resolver.doResolve(
  33. target,
  34. obj,
  35. "use " + mainModule + " from " + filename,
  36. resolveContext,
  37. callback
  38. );
  39. });
  40. }
  41. };