ExportsFieldPlugin.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const DescriptionFileUtils = require("./DescriptionFileUtils");
  8. const forEachBail = require("./forEachBail");
  9. const { processExportsField } = require("./util/entrypoints");
  10. const { parseIdentifier } = require("./util/identifier");
  11. const { checkImportsExportsFieldTarget } = require("./util/path");
  12. /** @typedef {import("./Resolver")} Resolver */
  13. /** @typedef {import("./Resolver").JsonObject} JsonObject */
  14. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  15. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  16. /** @typedef {import("./util/entrypoints").ExportsField} ExportsField */
  17. /** @typedef {import("./util/entrypoints").FieldProcessor} FieldProcessor */
  18. module.exports = class ExportsFieldPlugin {
  19. /**
  20. * @param {string | ResolveStepHook} source source
  21. * @param {Set<string>} conditionNames condition names
  22. * @param {string | string[]} fieldNamePath name path
  23. * @param {string | ResolveStepHook} target target
  24. */
  25. constructor(source, conditionNames, fieldNamePath, target) {
  26. this.source = source;
  27. this.target = target;
  28. this.conditionNames = conditionNames;
  29. this.fieldName = fieldNamePath;
  30. /** @type {WeakMap<JsonObject, FieldProcessor>} */
  31. this.fieldProcessorCache = new WeakMap();
  32. }
  33. /**
  34. * @param {Resolver} resolver the resolver
  35. * @returns {void}
  36. */
  37. apply(resolver) {
  38. const target = resolver.ensureHook(this.target);
  39. resolver
  40. .getHook(this.source)
  41. .tapAsync("ExportsFieldPlugin", (request, resolveContext, callback) => {
  42. // When there is no description file, abort
  43. if (!request.descriptionFilePath) return callback();
  44. if (
  45. // When the description file is inherited from parent, abort
  46. // (There is no description file inside of this package)
  47. request.relativePath !== "." ||
  48. request.request === undefined
  49. )
  50. return callback();
  51. const remainingRequest =
  52. request.query || request.fragment
  53. ? (request.request === "." ? "./" : request.request) +
  54. request.query +
  55. request.fragment
  56. : request.request;
  57. const exportsField =
  58. /** @type {ExportsField|null|undefined} */
  59. (
  60. DescriptionFileUtils.getField(
  61. /** @type {JsonObject} */ (request.descriptionFileData),
  62. this.fieldName
  63. )
  64. );
  65. if (!exportsField) return callback();
  66. if (request.directory) {
  67. return callback(
  68. new Error(
  69. `Resolving to directories is not possible with the exports field (request was ${remainingRequest}/)`
  70. )
  71. );
  72. }
  73. /** @type {string[]} */
  74. let paths;
  75. try {
  76. // We attach the cache to the description file instead of the exportsField value
  77. // because we use a WeakMap and the exportsField could be a string too.
  78. // Description file is always an object when exports field can be accessed.
  79. let fieldProcessor = this.fieldProcessorCache.get(
  80. /** @type {JsonObject} */ (request.descriptionFileData)
  81. );
  82. if (fieldProcessor === undefined) {
  83. fieldProcessor = processExportsField(exportsField);
  84. this.fieldProcessorCache.set(
  85. /** @type {JsonObject} */ (request.descriptionFileData),
  86. fieldProcessor
  87. );
  88. }
  89. paths = fieldProcessor(remainingRequest, this.conditionNames);
  90. } catch (/** @type {unknown} */ err) {
  91. if (resolveContext.log) {
  92. resolveContext.log(
  93. `Exports field in ${request.descriptionFilePath} can't be processed: ${err}`
  94. );
  95. }
  96. return callback(/** @type {Error} */ (err));
  97. }
  98. if (paths.length === 0) {
  99. return callback(
  100. new Error(
  101. `Package path ${remainingRequest} is not exported from package ${request.descriptionFileRoot} (see exports field in ${request.descriptionFilePath})`
  102. )
  103. );
  104. }
  105. forEachBail(
  106. paths,
  107. /**
  108. * @param {string} p path
  109. * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
  110. * @returns {void}
  111. */
  112. (p, callback) => {
  113. const parsedIdentifier = parseIdentifier(p);
  114. if (!parsedIdentifier) return callback();
  115. const [relativePath, query, fragment] = parsedIdentifier;
  116. const error = checkImportsExportsFieldTarget(relativePath);
  117. if (error) {
  118. return callback(error);
  119. }
  120. /** @type {ResolveRequest} */
  121. const obj = {
  122. ...request,
  123. request: undefined,
  124. path: path.join(
  125. /** @type {string} */ (request.descriptionFileRoot),
  126. relativePath
  127. ),
  128. relativePath,
  129. query,
  130. fragment
  131. };
  132. resolver.doResolve(
  133. target,
  134. obj,
  135. "using exports field: " + p,
  136. resolveContext,
  137. callback
  138. );
  139. },
  140. /**
  141. * @param {null|Error} [err] error
  142. * @param {null|ResolveRequest} [result] result
  143. * @returns {void}
  144. */
  145. (err, result) => callback(err, result || null)
  146. );
  147. });
  148. }
  149. };