DescriptionFilePlugin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. module.exports = class DescriptionFilePlugin {
  8. constructor(source, filenames, target) {
  9. this.source = source;
  10. this.filenames = [].concat(filenames);
  11. this.target = target;
  12. }
  13. apply(resolver) {
  14. const target = resolver.ensureHook(this.target);
  15. resolver.getHook(this.source).tapAsync("DescriptionFilePlugin", (request, resolveContext, callback) => {
  16. const directory = request.path;
  17. DescriptionFileUtils.loadDescriptionFile(resolver, directory, this.filenames, resolveContext, (err, result) => {
  18. if(err) return callback(err);
  19. if(!result) {
  20. if(resolveContext.missing) {
  21. this.filenames.forEach((filename) => {
  22. resolveContext.missing.add(resolver.join(directory, filename));
  23. });
  24. }
  25. if(resolveContext.log) resolveContext.log("No description file found");
  26. return callback();
  27. }
  28. const relativePath = "." + request.path.substr(result.directory.length).replace(/\\/g, "/");
  29. const obj = Object.assign({}, request, {
  30. descriptionFilePath: result.path,
  31. descriptionFileData: result.content,
  32. descriptionFileRoot: result.directory,
  33. relativePath: relativePath
  34. });
  35. resolver.doResolve(target, obj, "using description file: " + result.path + " (relative path: " + relativePath + ")", resolveContext, (err, result) => {
  36. if(err) return callback(err);
  37. // Don't allow other processing
  38. if(result === undefined) return callback(null, null);
  39. callback(null, result);
  40. });
  41. });
  42. });
  43. }
  44. };