Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AliasFieldPlugin.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DescriptionFileUtils = require("./DescriptionFileUtils");
  7. const getInnerRequest = require("./getInnerRequest");
  8. module.exports = class AliasFieldPlugin {
  9. constructor(source, field, target) {
  10. this.source = source;
  11. this.field = field;
  12. this.target = target;
  13. }
  14. apply(resolver) {
  15. const target = resolver.ensureHook(this.target);
  16. resolver.getHook(this.source).tapAsync("AliasFieldPlugin", (request, resolveContext, callback) => {
  17. if(!request.descriptionFileData) return callback();
  18. const innerRequest = getInnerRequest(resolver, request);
  19. if(!innerRequest) return callback();
  20. const fieldData = DescriptionFileUtils.getField(request.descriptionFileData, this.field);
  21. if(typeof fieldData !== "object") {
  22. if(resolveContext.log) resolveContext.log("Field '" + this.field + "' doesn't contain a valid alias configuration");
  23. return callback();
  24. }
  25. const data1 = fieldData[innerRequest];
  26. const data2 = fieldData[innerRequest.replace(/^\.\//, "")];
  27. const data = typeof data1 !== "undefined" ? data1 : data2;
  28. if(data === innerRequest) return callback();
  29. if(data === undefined) return callback();
  30. if(data === false) {
  31. const ignoreObj = Object.assign({}, request, {
  32. path: false
  33. });
  34. return callback(null, ignoreObj);
  35. }
  36. const obj = Object.assign({}, request, {
  37. path: request.descriptionFileRoot,
  38. request: data
  39. });
  40. resolver.doResolve(target, obj, "aliased from description file " + request.descriptionFilePath + " with mapping '" + innerRequest + "' to '" + data + "'", resolveContext, (err, result) => {
  41. if(err) return callback(err);
  42. // Don't allow other aliasing or raw request
  43. if(result === undefined) return callback(null, null);
  44. callback(null, result);
  45. });
  46. });
  47. }
  48. };