ExportsFieldPlugin.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const DescriptionFileUtils = require("./DescriptionFileUtils");
  7. const forEachBail = require("./forEachBail");
  8. const { processExportsField } = require("./util/entrypoints");
  9. const { parseIdentifier } = require("./util/identifier");
  10. const {
  11. deprecatedInvalidSegmentRegEx,
  12. invalidSegmentRegEx,
  13. } = require("./util/path");
  14. /** @typedef {import("./Resolver")} Resolver */
  15. /** @typedef {import("./Resolver").JsonObject} JsonObject */
  16. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  17. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  18. /** @typedef {import("./util/entrypoints").ExportsField} ExportsField */
  19. /** @typedef {import("./util/entrypoints").FieldProcessor} FieldProcessor */
  20. module.exports = class ExportsFieldPlugin {
  21. /**
  22. * @param {string | ResolveStepHook} source source
  23. * @param {Set<string>} conditionNames condition names
  24. * @param {string | string[]} fieldNamePath name path
  25. * @param {string | ResolveStepHook} target target
  26. */
  27. constructor(source, conditionNames, fieldNamePath, target) {
  28. this.source = source;
  29. this.target = target;
  30. this.conditionNames = conditionNames;
  31. this.fieldName = fieldNamePath;
  32. // `null` is cached for description files that have no exports field,
  33. // so subsequent resolves against the same package.json skip the
  34. // `DescriptionFileUtils.getField` walk entirely.
  35. /** @type {WeakMap<JsonObject, FieldProcessor | null>} */
  36. this._fieldProcessorCache = new WeakMap();
  37. }
  38. /**
  39. * @param {Resolver} resolver the resolver
  40. * @returns {void}
  41. */
  42. apply(resolver) {
  43. const target = resolver.ensureHook(this.target);
  44. resolver
  45. .getHook(this.source)
  46. .tapAsync("ExportsFieldPlugin", (request, resolveContext, callback) => {
  47. // When there is no description file, abort
  48. if (!request.descriptionFileData) return callback();
  49. if (
  50. // When the description file is inherited from parent, abort
  51. // (There is no description file inside of this package)
  52. request.relativePath !== "." ||
  53. request.request === undefined
  54. ) {
  55. return callback();
  56. }
  57. const { descriptionFileData } = request;
  58. const remainingRequest =
  59. request.query || request.fragment
  60. ? (request.request === "." ? "./" : request.request) +
  61. request.query +
  62. request.fragment
  63. : request.request;
  64. /** @type {string[]} */
  65. let paths;
  66. /** @type {string | null} */
  67. let usedField;
  68. try {
  69. // Look up the cached processor first. On a cache hit we
  70. // avoid re-walking the description file for the exports
  71. // field — and `null` is cached for description files that
  72. // have no exports field at all, so those skip the read
  73. // entirely. `processExportsField` can throw on a malformed
  74. // `exports` map (e.g. a key without a leading `.`), so
  75. // building the processor must stay inside this try/catch.
  76. let fieldProcessor =
  77. this._fieldProcessorCache.get(descriptionFileData);
  78. if (
  79. fieldProcessor === undefined &&
  80. !this._fieldProcessorCache.has(descriptionFileData)
  81. ) {
  82. const exportsField =
  83. /** @type {ExportsField | null | undefined} */
  84. (
  85. DescriptionFileUtils.getField(
  86. descriptionFileData,
  87. this.fieldName,
  88. )
  89. );
  90. fieldProcessor = exportsField
  91. ? processExportsField(exportsField)
  92. : null;
  93. this._fieldProcessorCache.set(descriptionFileData, fieldProcessor);
  94. }
  95. if (!fieldProcessor) return callback();
  96. if (request.directory) {
  97. return callback(
  98. new Error(
  99. `Resolving to directories is not possible with the exports field (request was ${remainingRequest}/)`,
  100. ),
  101. );
  102. }
  103. [paths, usedField] = fieldProcessor(
  104. remainingRequest,
  105. this.conditionNames,
  106. );
  107. } catch (/** @type {unknown} */ err) {
  108. if (resolveContext.log) {
  109. resolveContext.log(
  110. `Exports field in ${request.descriptionFilePath} can't be processed: ${err}`,
  111. );
  112. }
  113. return callback(/** @type {Error} */ (err));
  114. }
  115. if (paths.length === 0) {
  116. const conditions = [...this.conditionNames];
  117. const conditionsStr =
  118. conditions.length === 1
  119. ? `the condition "${conditions[0]}"`
  120. : `the conditions ${JSON.stringify(conditions)}`;
  121. return callback(
  122. new Error(
  123. `"${remainingRequest}" is not exported under ${conditionsStr} from package ${request.descriptionFileRoot} (see exports field in ${request.descriptionFilePath})`,
  124. ),
  125. );
  126. }
  127. forEachBail(
  128. paths,
  129. /**
  130. * @param {string} path path
  131. * @param {(err?: null | Error, result?: null | ResolveRequest) => void} callback callback
  132. * @param {number} i index
  133. * @returns {void}
  134. */
  135. (path, callback, i) => {
  136. const parsedIdentifier = parseIdentifier(path);
  137. if (!parsedIdentifier) return callback();
  138. const [relativePath, query, fragment] = parsedIdentifier;
  139. if (!relativePath.startsWith("./")) {
  140. if (paths.length === i) {
  141. return callback(
  142. new Error(
  143. `Invalid "exports" target "${path}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"`,
  144. ),
  145. );
  146. }
  147. return callback();
  148. }
  149. const withoutDotSlash = relativePath.slice(2);
  150. if (
  151. invalidSegmentRegEx.test(withoutDotSlash) &&
  152. deprecatedInvalidSegmentRegEx.test(withoutDotSlash)
  153. ) {
  154. if (paths.length === i) {
  155. return callback(
  156. new Error(
  157. `Invalid "exports" target "${path}" defined for "${usedField}" in the package config ${request.descriptionFilePath}, targets must start with "./"`,
  158. ),
  159. );
  160. }
  161. return callback();
  162. }
  163. /** @type {ResolveRequest} */
  164. const obj = {
  165. ...request,
  166. request: undefined,
  167. path: resolver.join(
  168. /** @type {string} */ (request.descriptionFileRoot),
  169. relativePath,
  170. ),
  171. relativePath,
  172. query,
  173. fragment,
  174. };
  175. resolver.doResolve(
  176. target,
  177. obj,
  178. `using exports field: ${path}`,
  179. resolveContext,
  180. (err, result) => {
  181. if (err) return callback(err);
  182. // Don't allow to continue - https://github.com/webpack/enhanced-resolve/issues/400
  183. if (result === undefined) return callback(null, null);
  184. callback(null, result);
  185. },
  186. );
  187. },
  188. /**
  189. * @param {(null | Error)=} err error
  190. * @param {(null | ResolveRequest)=} result result
  191. * @returns {void}
  192. */
  193. (err, result) => {
  194. if (err) return callback(err);
  195. // When an exports field match was found but the target file doesn't exist,
  196. // return an error to prevent fallback to parent node_modules directories.
  197. // Per the Node.js ESM spec, a matched exports entry that fails to resolve
  198. // is a hard error, not a signal to continue searching up the directory tree.
  199. // See: https://github.com/webpack/enhanced-resolve/issues/399
  200. if (!result) {
  201. return callback(
  202. new Error(
  203. `Package path ${remainingRequest} is exported from package ${request.descriptionFileRoot}, but no valid target file was found (see exports field in ${request.descriptionFilePath})`,
  204. ),
  205. );
  206. }
  207. callback(null, result);
  208. },
  209. );
  210. });
  211. }
  212. };