ConcordModulesPlugin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.getHook(this.source).tapAsync("ConcordModulesPlugin", (request, resolveContext, callback) => {
  18. const innerRequest = getInnerRequest(resolver, request);
  19. if(!innerRequest) return callback();
  20. const concordField = DescriptionFileUtils.getField(request.descriptionFileData, "concord");
  21. if(!concordField) return callback();
  22. const data = concord.matchModule(request.context, concordField, innerRequest);
  23. if(data === innerRequest) return callback();
  24. if(data === undefined) return callback();
  25. if(data === false) {
  26. const ignoreObj = Object.assign({}, request, {
  27. path: false
  28. });
  29. return callback(null, ignoreObj);
  30. }
  31. const obj = Object.assign({}, request, {
  32. path: request.descriptionFileRoot,
  33. request: data
  34. });
  35. resolver.doResolve(target, obj, "aliased from description file " + request.descriptionFilePath + " with mapping '" + innerRequest + "' to '" + data + "'", resolveContext, (err, result) => {
  36. if(err) return callback(err);
  37. // Don't allow other aliasing or raw request
  38. if(result === undefined) return callback(null, null);
  39. callback(null, result);
  40. });
  41. });
  42. }
  43. };