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.

76 lines
1.9 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 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
  16. .getHook(this.source)
  17. .tapAsync(
  18. "DescriptionFilePlugin",
  19. (request, resolveContext, callback) => {
  20. const directory = request.path;
  21. DescriptionFileUtils.loadDescriptionFile(
  22. resolver,
  23. directory,
  24. this.filenames,
  25. resolveContext,
  26. (err, result) => {
  27. if (err) return callback(err);
  28. if (!result) {
  29. if (resolveContext.missing) {
  30. this.filenames.forEach(filename => {
  31. resolveContext.missing.add(
  32. resolver.join(directory, filename)
  33. );
  34. });
  35. }
  36. if (resolveContext.log)
  37. resolveContext.log("No description file found");
  38. return callback();
  39. }
  40. const relativePath =
  41. "." +
  42. request.path
  43. .substr(result.directory.length)
  44. .replace(/\\/g, "/");
  45. const obj = Object.assign({}, request, {
  46. descriptionFilePath: result.path,
  47. descriptionFileData: result.content,
  48. descriptionFileRoot: result.directory,
  49. relativePath: relativePath
  50. });
  51. resolver.doResolve(
  52. target,
  53. obj,
  54. "using description file: " +
  55. result.path +
  56. " (relative path: " +
  57. relativePath +
  58. ")",
  59. resolveContext,
  60. (err, result) => {
  61. if (err) return callback(err);
  62. // Don't allow other processing
  63. if (result === undefined) return callback(null, null);
  64. callback(null, result);
  65. }
  66. );
  67. }
  68. );
  69. }
  70. );
  71. }
  72. };