HarmonyExportDependencyParserPlugin.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CompatibilityPlugin = require("../CompatibilityPlugin");
  7. const WebpackError = require("../WebpackError");
  8. const { getImportAttributes } = require("../javascript/JavascriptParser");
  9. const InnerGraph = require("../optimize/InnerGraph");
  10. const ConstDependency = require("./ConstDependency");
  11. const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
  12. const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
  13. const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
  14. const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
  15. const { ExportPresenceModes } = require("./HarmonyImportDependency");
  16. const {
  17. harmonySpecifierTag
  18. } = require("./HarmonyImportDependencyParserPlugin");
  19. const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
  20. const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase");
  21. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  22. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  23. /** @typedef {import("../javascript/JavascriptParser").ClassDeclaration} ClassDeclaration */
  24. /** @typedef {import("../javascript/JavascriptParser").FunctionDeclaration} FunctionDeclaration */
  25. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  26. /** @typedef {import("./HarmonyImportDependencyParserPlugin").HarmonySettings} HarmonySettings */
  27. /** @typedef {import("../CompatibilityPlugin").CompatibilitySettings} CompatibilitySettings */
  28. const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
  29. const PLUGIN_NAME = "HarmonyExportDependencyParserPlugin";
  30. module.exports = class HarmonyExportDependencyParserPlugin {
  31. /**
  32. * @param {import("../../declarations/WebpackOptions").JavascriptParserOptions} options options
  33. */
  34. constructor(options) {
  35. this.options = options;
  36. this.exportPresenceMode = ExportPresenceModes.resolveFromOptions(
  37. options.reexportExportsPresence,
  38. options
  39. );
  40. }
  41. /**
  42. * @param {JavascriptParser} parser the parser
  43. * @returns {void}
  44. */
  45. apply(parser) {
  46. const { exportPresenceMode } = this;
  47. const getImportPhase = createGetImportPhase(this.options.deferImport);
  48. parser.hooks.export.tap(PLUGIN_NAME, (statement) => {
  49. const dep = new HarmonyExportHeaderDependency(
  50. /** @type {Range | false} */ (
  51. statement.declaration && statement.declaration.range
  52. ),
  53. /** @type {Range} */ (statement.range)
  54. );
  55. dep.loc = Object.create(
  56. /** @type {DependencyLocation} */ (statement.loc)
  57. );
  58. dep.loc.index = -1;
  59. parser.state.module.addPresentationalDependency(dep);
  60. return true;
  61. });
  62. parser.hooks.exportImport.tap(PLUGIN_NAME, (statement, source) => {
  63. parser.state.lastHarmonyImportOrder =
  64. (parser.state.lastHarmonyImportOrder || 0) + 1;
  65. const clearDep = new ConstDependency(
  66. "",
  67. /** @type {Range} */ (statement.range)
  68. );
  69. clearDep.loc = /** @type {DependencyLocation} */ (statement.loc);
  70. clearDep.loc.index = -1;
  71. parser.state.module.addPresentationalDependency(clearDep);
  72. const phase = getImportPhase(parser, statement);
  73. if (phase && ImportPhaseUtils.isDefer(phase)) {
  74. const error = new WebpackError(
  75. "Deferred re-export (`export defer * as namespace from '...'`) is not a part of the Import Defer proposal.\nUse the following code instead:\n import defer * as namespace from '...';\n export { namespace };"
  76. );
  77. error.loc = statement.loc || undefined;
  78. parser.state.current.addError(error);
  79. }
  80. const sideEffectDep = new HarmonyImportSideEffectDependency(
  81. /** @type {string} */ (source),
  82. parser.state.lastHarmonyImportOrder,
  83. phase,
  84. getImportAttributes(statement)
  85. );
  86. sideEffectDep.loc = Object.create(
  87. /** @type {DependencyLocation} */ (statement.loc)
  88. );
  89. sideEffectDep.loc.index = -1;
  90. parser.state.current.addDependency(sideEffectDep);
  91. return true;
  92. });
  93. parser.hooks.exportExpression.tap(PLUGIN_NAME, (statement, node) => {
  94. const isFunctionDeclaration = node.type === "FunctionDeclaration";
  95. const exprRange = /** @type {Range} */ (node.range);
  96. const statementRange = /** @type {Range} */ (statement.range);
  97. const comments = parser.getComments([statementRange[0], exprRange[0]]);
  98. const dep = new HarmonyExportExpressionDependency(
  99. exprRange,
  100. statementRange,
  101. comments
  102. .map((c) => {
  103. switch (c.type) {
  104. case "Block":
  105. return `/*${c.value}*/`;
  106. case "Line":
  107. return `//${c.value}\n`;
  108. }
  109. return "";
  110. })
  111. .join(""),
  112. node.type.endsWith("Declaration") &&
  113. /** @type {FunctionDeclaration | ClassDeclaration} */ (node).id
  114. ? /** @type {FunctionDeclaration | ClassDeclaration} */
  115. (node).id.name
  116. : isFunctionDeclaration
  117. ? {
  118. range: [
  119. exprRange[0],
  120. node.params.length > 0
  121. ? /** @type {Range} */ (node.params[0].range)[0]
  122. : /** @type {Range} */ (node.body.range)[0]
  123. ],
  124. prefix: `${node.async ? "async " : ""}function${
  125. node.generator ? "*" : ""
  126. } `,
  127. suffix: `(${node.params.length > 0 ? "" : ") "}`
  128. }
  129. : undefined
  130. );
  131. dep.loc = Object.create(
  132. /** @type {DependencyLocation} */ (statement.loc)
  133. );
  134. dep.loc.index = -1;
  135. parser.state.current.addDependency(dep);
  136. InnerGraph.addVariableUsage(
  137. parser,
  138. node.type.endsWith("Declaration") &&
  139. /** @type {FunctionDeclaration | ClassDeclaration} */ (node).id
  140. ? /** @type {FunctionDeclaration | ClassDeclaration} */ (node).id.name
  141. : "*default*",
  142. "default"
  143. );
  144. return true;
  145. });
  146. parser.hooks.exportSpecifier.tap(
  147. PLUGIN_NAME,
  148. (statement, id, name, idx) => {
  149. // CompatibilityPlugin may change exports name
  150. // not handle re-export or import then export situation as current CompatibilityPlugin only
  151. // rename symbol in declaration module, not change exported symbol
  152. const variable = parser.getTagData(
  153. id,
  154. CompatibilityPlugin.nestedWebpackIdentifierTag
  155. );
  156. if (variable && /** @type {CompatibilitySettings} */ (variable).name) {
  157. // CompatibilityPlugin changes exports to a new name, should updates exports name
  158. id = /** @type {CompatibilitySettings} */ (variable).name;
  159. }
  160. const settings =
  161. /** @type {HarmonySettings} */
  162. (parser.getTagData(id, harmonySpecifierTag));
  163. const harmonyNamedExports = (parser.state.harmonyNamedExports =
  164. parser.state.harmonyNamedExports || new Set());
  165. harmonyNamedExports.add(name);
  166. InnerGraph.addVariableUsage(parser, id, name);
  167. const dep = settings
  168. ? new HarmonyExportImportedSpecifierDependency(
  169. settings.source,
  170. settings.sourceOrder,
  171. settings.ids,
  172. name,
  173. harmonyNamedExports,
  174. null,
  175. exportPresenceMode,
  176. null,
  177. settings.phase,
  178. settings.attributes
  179. )
  180. : new HarmonyExportSpecifierDependency(id, name);
  181. dep.loc = Object.create(
  182. /** @type {DependencyLocation} */ (statement.loc)
  183. );
  184. dep.loc.index = idx;
  185. const isAsiSafe = !parser.isAsiPosition(
  186. /** @type {Range} */
  187. (statement.range)[0]
  188. );
  189. if (!isAsiSafe) {
  190. parser.setAsiPosition(/** @type {Range} */ (statement.range)[1]);
  191. }
  192. parser.state.current.addDependency(dep);
  193. return true;
  194. }
  195. );
  196. parser.hooks.exportImportSpecifier.tap(
  197. PLUGIN_NAME,
  198. (statement, source, id, name, idx) => {
  199. const harmonyNamedExports = (parser.state.harmonyNamedExports =
  200. parser.state.harmonyNamedExports || new Set());
  201. /** @type {InstanceType<HarmonyStarExportsList> | null} */
  202. let harmonyStarExports = null;
  203. if (name) {
  204. harmonyNamedExports.add(name);
  205. } else {
  206. harmonyStarExports = parser.state.harmonyStarExports =
  207. parser.state.harmonyStarExports || new HarmonyStarExportsList();
  208. }
  209. const attributes = getImportAttributes(statement);
  210. const dep = new HarmonyExportImportedSpecifierDependency(
  211. /** @type {string} */
  212. (source),
  213. /** @type {number} */
  214. (parser.state.lastHarmonyImportOrder),
  215. id ? [id] : [],
  216. name,
  217. harmonyNamedExports,
  218. // eslint-disable-next-line unicorn/prefer-spread
  219. harmonyStarExports && harmonyStarExports.slice(),
  220. exportPresenceMode,
  221. harmonyStarExports,
  222. getImportPhase(parser, statement),
  223. attributes
  224. );
  225. if (harmonyStarExports) {
  226. harmonyStarExports.push(dep);
  227. }
  228. dep.loc = Object.create(
  229. /** @type {DependencyLocation} */ (statement.loc)
  230. );
  231. dep.loc.index = idx;
  232. const isAsiSafe = !parser.isAsiPosition(
  233. /** @type {Range} */
  234. (statement.range)[0]
  235. );
  236. if (!isAsiSafe) {
  237. parser.setAsiPosition(/** @type {Range} */ (statement.range)[1]);
  238. }
  239. parser.state.current.addDependency(dep);
  240. return true;
  241. }
  242. );
  243. }
  244. };