ImportsFieldPlugin.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 { processImportsField } = 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").FieldProcessor} FieldProcessor */
  17. /** @typedef {import("./util/entrypoints").ImportsField} ImportsField */
  18. const dotCode = ".".charCodeAt(0);
  19. module.exports = class ImportsFieldPlugin {
  20. /**
  21. * @param {string | ResolveStepHook} source source
  22. * @param {Set<string>} conditionNames condition names
  23. * @param {string | string[]} fieldNamePath name path
  24. * @param {string | ResolveStepHook} targetFile target file
  25. * @param {string | ResolveStepHook} targetPackage target package
  26. */
  27. constructor(
  28. source,
  29. conditionNames,
  30. fieldNamePath,
  31. targetFile,
  32. targetPackage
  33. ) {
  34. this.source = source;
  35. this.targetFile = targetFile;
  36. this.targetPackage = targetPackage;
  37. this.conditionNames = conditionNames;
  38. this.fieldName = fieldNamePath;
  39. /** @type {WeakMap<JsonObject, FieldProcessor>} */
  40. this.fieldProcessorCache = new WeakMap();
  41. }
  42. /**
  43. * @param {Resolver} resolver the resolver
  44. * @returns {void}
  45. */
  46. apply(resolver) {
  47. const targetFile = resolver.ensureHook(this.targetFile);
  48. const targetPackage = resolver.ensureHook(this.targetPackage);
  49. resolver
  50. .getHook(this.source)
  51. .tapAsync("ImportsFieldPlugin", (request, resolveContext, callback) => {
  52. // When there is no description file, abort
  53. if (!request.descriptionFilePath || request.request === undefined) {
  54. return callback();
  55. }
  56. const remainingRequest =
  57. request.request + request.query + request.fragment;
  58. const importsField =
  59. /** @type {ImportsField|null|undefined} */
  60. (
  61. DescriptionFileUtils.getField(
  62. /** @type {JsonObject} */ (request.descriptionFileData),
  63. this.fieldName
  64. )
  65. );
  66. if (!importsField) return callback();
  67. if (request.directory) {
  68. return callback(
  69. new Error(
  70. `Resolving to directories is not possible with the imports field (request was ${remainingRequest}/)`
  71. )
  72. );
  73. }
  74. /** @type {string[]} */
  75. let paths;
  76. try {
  77. // We attach the cache to the description file instead of the importsField value
  78. // because we use a WeakMap and the importsField could be a string too.
  79. // Description file is always an object when exports field can be accessed.
  80. let fieldProcessor = this.fieldProcessorCache.get(
  81. /** @type {JsonObject} */ (request.descriptionFileData)
  82. );
  83. if (fieldProcessor === undefined) {
  84. fieldProcessor = processImportsField(importsField);
  85. this.fieldProcessorCache.set(
  86. /** @type {JsonObject} */ (request.descriptionFileData),
  87. fieldProcessor
  88. );
  89. }
  90. paths = fieldProcessor(remainingRequest, this.conditionNames);
  91. } catch (/** @type {unknown} */ err) {
  92. if (resolveContext.log) {
  93. resolveContext.log(
  94. `Imports field in ${request.descriptionFilePath} can't be processed: ${err}`
  95. );
  96. }
  97. return callback(/** @type {Error} */ (err));
  98. }
  99. if (paths.length === 0) {
  100. return callback(
  101. new Error(
  102. `Package import ${remainingRequest} is not imported from package ${request.descriptionFileRoot} (see imports field in ${request.descriptionFilePath})`
  103. )
  104. );
  105. }
  106. forEachBail(
  107. paths,
  108. /**
  109. * @param {string} p path
  110. * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
  111. * @returns {void}
  112. */
  113. (p, callback) => {
  114. const parsedIdentifier = parseIdentifier(p);
  115. if (!parsedIdentifier) return callback();
  116. const [path_, query, fragment] = parsedIdentifier;
  117. const error = checkImportsExportsFieldTarget(path_);
  118. if (error) {
  119. return callback(error);
  120. }
  121. switch (path_.charCodeAt(0)) {
  122. // should be relative
  123. case dotCode: {
  124. /** @type {ResolveRequest} */
  125. const obj = {
  126. ...request,
  127. request: undefined,
  128. path: path.join(
  129. /** @type {string} */ (request.descriptionFileRoot),
  130. path_
  131. ),
  132. relativePath: path_,
  133. query,
  134. fragment
  135. };
  136. resolver.doResolve(
  137. targetFile,
  138. obj,
  139. "using imports field: " + p,
  140. resolveContext,
  141. callback
  142. );
  143. break;
  144. }
  145. // package resolving
  146. default: {
  147. /** @type {ResolveRequest} */
  148. const obj = {
  149. ...request,
  150. request: path_,
  151. relativePath: path_,
  152. fullySpecified: true,
  153. query,
  154. fragment
  155. };
  156. resolver.doResolve(
  157. targetPackage,
  158. obj,
  159. "using imports field: " + p,
  160. resolveContext,
  161. callback
  162. );
  163. }
  164. }
  165. },
  166. /**
  167. * @param {null|Error} [err] error
  168. * @param {null|ResolveRequest} [result] result
  169. * @returns {void}
  170. */
  171. (err, result) => callback(err, result || null)
  172. );
  173. });
  174. }
  175. };