HarmonyExportImportedSpecifierDependency.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ConditionalInitFragment = require("../ConditionalInitFragment");
  7. const Dependency = require("../Dependency");
  8. const { UsageState } = require("../ExportsInfo");
  9. const HarmonyLinkingError = require("../HarmonyLinkingError");
  10. const InitFragment = require("../InitFragment");
  11. const RuntimeGlobals = require("../RuntimeGlobals");
  12. const Template = require("../Template");
  13. const {
  14. getMakeDeferredNamespaceModeFromExportsType
  15. } = require("../runtime/MakeDeferredNamespaceObjectRuntime");
  16. const { countIterable } = require("../util/IterableHelpers");
  17. const { combine, first } = require("../util/SetHelpers");
  18. const makeSerializable = require("../util/makeSerializable");
  19. const propertyAccess = require("../util/propertyAccess");
  20. const { propertyName } = require("../util/propertyName");
  21. const {
  22. filterRuntime,
  23. getRuntimeKey,
  24. keyToRuntime
  25. } = require("../util/runtime");
  26. const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
  27. const HarmonyImportDependency = require("./HarmonyImportDependency");
  28. const processExportInfo = require("./processExportInfo");
  29. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  30. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  31. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  32. /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
  33. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  34. /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
  35. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  36. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  37. /** @typedef {import("../ExportsInfo")} ExportsInfo */
  38. /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
  39. /** @typedef {import("../ExportsInfo").UsedName} UsedName */
  40. /** @typedef {import("../Generator").GenerateContext} GenerateContext */
  41. /** @typedef {import("../Module")} Module */
  42. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  43. /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
  44. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  45. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  46. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  47. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  48. /** @typedef {import("../WebpackError")} WebpackError */
  49. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  50. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  51. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  52. /** @typedef {import("../util/Hash")} Hash */
  53. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  54. /** @typedef {import("./HarmonyImportDependency").ExportPresenceMode} ExportPresenceMode */
  55. /** @typedef {import("./processExportInfo").ReferencedExports} ReferencedExports */
  56. /** @typedef {"missing"|"unused"|"empty-star"|"reexport-dynamic-default"|"reexport-named-default"|"reexport-namespace-object"|"reexport-fake-namespace-object"|"reexport-undefined"|"normal-reexport"|"dynamic-reexport"} ExportModeType */
  57. const { ExportPresenceModes } = HarmonyImportDependency;
  58. const idsSymbol = Symbol("HarmonyExportImportedSpecifierDependency.ids");
  59. class NormalReexportItem {
  60. /**
  61. * @param {string} name export name
  62. * @param {string[]} ids reexported ids from other module
  63. * @param {ExportInfo} exportInfo export info from other module
  64. * @param {boolean} checked true, if it should be checked at runtime if this export exists
  65. * @param {boolean} hidden true, if it is hidden behind another active export in the same module
  66. */
  67. constructor(name, ids, exportInfo, checked, hidden) {
  68. this.name = name;
  69. this.ids = ids;
  70. this.exportInfo = exportInfo;
  71. this.checked = checked;
  72. this.hidden = hidden;
  73. }
  74. }
  75. /** @typedef {Set<string>} ExportModeIgnored */
  76. /** @typedef {Set<string>} ExportModeHidden */
  77. class ExportMode {
  78. /**
  79. * @param {ExportModeType} type type of the mode
  80. */
  81. constructor(type) {
  82. /** @type {ExportModeType} */
  83. this.type = type;
  84. // for "normal-reexport":
  85. /** @type {NormalReexportItem[] | null} */
  86. this.items = null;
  87. // for "reexport-named-default" | "reexport-fake-namespace-object" | "reexport-namespace-object"
  88. /** @type {string | null} */
  89. this.name = null;
  90. /** @type {ExportInfo | null} */
  91. this.partialNamespaceExportInfo = null;
  92. // for "dynamic-reexport":
  93. /** @type {ExportModeIgnored | null} */
  94. this.ignored = null;
  95. // for "dynamic-reexport" | "empty-star":
  96. /** @type {ExportModeHidden | undefined | null} */
  97. this.hidden = null;
  98. // for "missing":
  99. /** @type {string | null} */
  100. this.userRequest = null;
  101. // for "reexport-fake-namespace-object":
  102. /** @type {number} */
  103. this.fakeType = 0;
  104. }
  105. }
  106. /** @typedef {string[]} Names */
  107. /** @typedef {number[]} DependencyIndices */
  108. /**
  109. * @param {ModuleGraph} moduleGraph module graph
  110. * @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency>} dependencies dependencies
  111. * @param {TODO=} additionalDependency additional dependency
  112. * @returns {{ names: Names, dependencyIndices: DependencyIndices }} result
  113. */
  114. const determineExportAssignments = (
  115. moduleGraph,
  116. dependencies,
  117. additionalDependency
  118. ) => {
  119. /** @type {Set<string>} */
  120. const names = new Set();
  121. /** @type {number[]} */
  122. const dependencyIndices = [];
  123. if (additionalDependency) {
  124. dependencies = [...dependencies, additionalDependency];
  125. }
  126. for (const dep of dependencies) {
  127. const i = dependencyIndices.length;
  128. dependencyIndices[i] = names.size;
  129. const otherImportedModule = moduleGraph.getModule(dep);
  130. if (otherImportedModule) {
  131. const exportsInfo = moduleGraph.getExportsInfo(otherImportedModule);
  132. for (const exportInfo of exportsInfo.exports) {
  133. if (
  134. exportInfo.provided === true &&
  135. exportInfo.name !== "default" &&
  136. !names.has(exportInfo.name)
  137. ) {
  138. names.add(exportInfo.name);
  139. dependencyIndices[i] = names.size;
  140. }
  141. }
  142. }
  143. }
  144. dependencyIndices.push(names.size);
  145. return { names: [...names], dependencyIndices };
  146. };
  147. /**
  148. * @param {object} options options
  149. * @param {Names} options.names names
  150. * @param {DependencyIndices} options.dependencyIndices dependency indices
  151. * @param {string} name name
  152. * @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency>} dependencies dependencies
  153. * @returns {HarmonyExportImportedSpecifierDependency | undefined} found dependency or nothing
  154. */
  155. const findDependencyForName = (
  156. { names, dependencyIndices },
  157. name,
  158. dependencies
  159. ) => {
  160. const dependenciesIt = dependencies[Symbol.iterator]();
  161. const dependencyIndicesIt = dependencyIndices[Symbol.iterator]();
  162. let dependenciesItResult = dependenciesIt.next();
  163. let dependencyIndicesItResult = dependencyIndicesIt.next();
  164. if (dependencyIndicesItResult.done) return;
  165. for (let i = 0; i < names.length; i++) {
  166. while (i >= dependencyIndicesItResult.value) {
  167. dependenciesItResult = dependenciesIt.next();
  168. dependencyIndicesItResult = dependencyIndicesIt.next();
  169. if (dependencyIndicesItResult.done) return;
  170. }
  171. if (names[i] === name) return dependenciesItResult.value;
  172. }
  173. return undefined;
  174. };
  175. /**
  176. * @param {ModuleGraph} moduleGraph the module graph
  177. * @param {HarmonyExportImportedSpecifierDependency} dep the dependency
  178. * @param {string} runtimeKey the runtime key
  179. * @returns {ExportMode} the export mode
  180. */
  181. const getMode = (moduleGraph, dep, runtimeKey) => {
  182. const importedModule = moduleGraph.getModule(dep);
  183. if (!importedModule) {
  184. const mode = new ExportMode("missing");
  185. mode.userRequest = dep.userRequest;
  186. return mode;
  187. }
  188. const name = dep.name;
  189. const runtime = keyToRuntime(runtimeKey);
  190. const parentModule = /** @type {Module} */ (moduleGraph.getParentModule(dep));
  191. const exportsInfo = moduleGraph.getExportsInfo(parentModule);
  192. if (
  193. name
  194. ? exportsInfo.getUsed(name, runtime) === UsageState.Unused
  195. : exportsInfo.isUsed(runtime) === false
  196. ) {
  197. const mode = new ExportMode("unused");
  198. mode.name = name || "*";
  199. return mode;
  200. }
  201. const importedExportsType = importedModule.getExportsType(
  202. moduleGraph,
  203. /** @type {BuildMeta} */
  204. (parentModule.buildMeta).strictHarmonyModule
  205. );
  206. const ids = dep.getIds(moduleGraph);
  207. // Special handling for reexporting the default export
  208. // from non-namespace modules
  209. if (name && ids.length > 0 && ids[0] === "default") {
  210. switch (importedExportsType) {
  211. case "dynamic": {
  212. const mode = new ExportMode("reexport-dynamic-default");
  213. mode.name = name;
  214. return mode;
  215. }
  216. case "default-only":
  217. case "default-with-named": {
  218. const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
  219. const mode = new ExportMode("reexport-named-default");
  220. mode.name = name;
  221. mode.partialNamespaceExportInfo = exportInfo;
  222. return mode;
  223. }
  224. }
  225. }
  226. // reexporting with a fixed name
  227. if (name) {
  228. let mode;
  229. const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
  230. if (ids.length > 0) {
  231. // export { name as name }
  232. switch (importedExportsType) {
  233. case "default-only":
  234. mode = new ExportMode("reexport-undefined");
  235. mode.name = name;
  236. break;
  237. default:
  238. mode = new ExportMode("normal-reexport");
  239. mode.items = [
  240. new NormalReexportItem(name, ids, exportInfo, false, false)
  241. ];
  242. break;
  243. }
  244. } else {
  245. // export * as name
  246. switch (importedExportsType) {
  247. case "default-only":
  248. mode = new ExportMode("reexport-fake-namespace-object");
  249. mode.name = name;
  250. mode.partialNamespaceExportInfo = exportInfo;
  251. mode.fakeType = 0;
  252. break;
  253. case "default-with-named":
  254. mode = new ExportMode("reexport-fake-namespace-object");
  255. mode.name = name;
  256. mode.partialNamespaceExportInfo = exportInfo;
  257. mode.fakeType = 2;
  258. break;
  259. case "dynamic":
  260. default:
  261. mode = new ExportMode("reexport-namespace-object");
  262. mode.name = name;
  263. mode.partialNamespaceExportInfo = exportInfo;
  264. }
  265. }
  266. return mode;
  267. }
  268. // Star reexporting
  269. const { ignoredExports, exports, checked, hidden } = dep.getStarReexports(
  270. moduleGraph,
  271. runtime,
  272. exportsInfo,
  273. importedModule
  274. );
  275. if (!exports) {
  276. // We have too few info about the modules
  277. // Delegate the logic to the runtime code
  278. const mode = new ExportMode("dynamic-reexport");
  279. mode.ignored = ignoredExports;
  280. mode.hidden = hidden;
  281. return mode;
  282. }
  283. if (exports.size === 0) {
  284. const mode = new ExportMode("empty-star");
  285. mode.hidden = hidden;
  286. return mode;
  287. }
  288. const mode = new ExportMode("normal-reexport");
  289. mode.items = Array.from(
  290. exports,
  291. (exportName) =>
  292. new NormalReexportItem(
  293. exportName,
  294. [exportName],
  295. exportsInfo.getReadOnlyExportInfo(exportName),
  296. /** @type {Set<string>} */
  297. (checked).has(exportName),
  298. false
  299. )
  300. );
  301. if (hidden !== undefined) {
  302. for (const exportName of hidden) {
  303. mode.items.push(
  304. new NormalReexportItem(
  305. exportName,
  306. [exportName],
  307. exportsInfo.getReadOnlyExportInfo(exportName),
  308. false,
  309. true
  310. )
  311. );
  312. }
  313. }
  314. return mode;
  315. };
  316. /** @typedef {string[]} Ids */
  317. /** @typedef {Set<string>} Exports */
  318. /** @typedef {Set<string>} Checked */
  319. /** @typedef {Set<string>} Hidden */
  320. /** @typedef {Set<string>} IgnoredExports */
  321. class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
  322. /**
  323. * @param {string} request the request string
  324. * @param {number} sourceOrder the order in the original source file
  325. * @param {Ids} ids the requested export name of the imported module
  326. * @param {string | null} name the export name of for this module
  327. * @param {Set<string>} activeExports other named exports in the module
  328. * @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | null} otherStarExports other star exports in the module before this import
  329. * @param {ExportPresenceMode} exportPresenceMode mode of checking export names
  330. * @param {HarmonyStarExportsList | null} allStarExports all star exports in the module
  331. * @param {ImportAttributes=} attributes import attributes
  332. * @param {boolean=} defer is defer phase
  333. */
  334. constructor(
  335. request,
  336. sourceOrder,
  337. ids,
  338. name,
  339. activeExports,
  340. otherStarExports,
  341. exportPresenceMode,
  342. allStarExports,
  343. attributes,
  344. defer
  345. ) {
  346. super(request, sourceOrder, attributes, defer);
  347. this.ids = ids;
  348. this.name = name;
  349. this.activeExports = activeExports;
  350. this.otherStarExports = otherStarExports;
  351. this.exportPresenceMode = exportPresenceMode;
  352. this.allStarExports = allStarExports;
  353. }
  354. /**
  355. * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
  356. */
  357. couldAffectReferencingModule() {
  358. return Dependency.TRANSITIVE;
  359. }
  360. // TODO webpack 6 remove
  361. get id() {
  362. throw new Error("id was renamed to ids and type changed to string[]");
  363. }
  364. // TODO webpack 6 remove
  365. getId() {
  366. throw new Error("id was renamed to ids and type changed to string[]");
  367. }
  368. // TODO webpack 6 remove
  369. setId() {
  370. throw new Error("id was renamed to ids and type changed to string[]");
  371. }
  372. get type() {
  373. return "harmony export imported specifier";
  374. }
  375. /**
  376. * @param {ModuleGraph} moduleGraph the module graph
  377. * @returns {Ids} the imported id
  378. */
  379. getIds(moduleGraph) {
  380. return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
  381. }
  382. /**
  383. * @param {ModuleGraph} moduleGraph the module graph
  384. * @param {Ids} ids the imported ids
  385. * @returns {void}
  386. */
  387. setIds(moduleGraph, ids) {
  388. moduleGraph.getMeta(this)[idsSymbol] = ids;
  389. }
  390. /**
  391. * @param {ModuleGraph} moduleGraph the module graph
  392. * @param {RuntimeSpec} runtime the runtime
  393. * @returns {ExportMode} the export mode
  394. */
  395. getMode(moduleGraph, runtime) {
  396. return moduleGraph.dependencyCacheProvide(
  397. this,
  398. getRuntimeKey(runtime),
  399. getMode
  400. );
  401. }
  402. /**
  403. * @param {ModuleGraph} moduleGraph the module graph
  404. * @param {RuntimeSpec} runtime the runtime
  405. * @param {ExportsInfo} exportsInfo exports info about the current module (optional)
  406. * @param {Module} importedModule the imported module (optional)
  407. * @returns {{exports?: Exports, checked?: Checked, ignoredExports: IgnoredExports, hidden?: Hidden}} information
  408. */
  409. getStarReexports(
  410. moduleGraph,
  411. runtime,
  412. exportsInfo = moduleGraph.getExportsInfo(
  413. /** @type {Module} */ (moduleGraph.getParentModule(this))
  414. ),
  415. importedModule = /** @type {Module} */ (moduleGraph.getModule(this))
  416. ) {
  417. const importedExportsInfo = moduleGraph.getExportsInfo(importedModule);
  418. const noExtraExports =
  419. importedExportsInfo.otherExportsInfo.provided === false;
  420. const noExtraImports =
  421. exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused;
  422. const ignoredExports = new Set(["default", ...this.activeExports]);
  423. let hiddenExports;
  424. const otherStarExports =
  425. this._discoverActiveExportsFromOtherStarExports(moduleGraph);
  426. if (otherStarExports !== undefined) {
  427. hiddenExports = new Set();
  428. for (let i = 0; i < otherStarExports.namesSlice; i++) {
  429. hiddenExports.add(otherStarExports.names[i]);
  430. }
  431. for (const e of ignoredExports) hiddenExports.delete(e);
  432. }
  433. if (!noExtraExports && !noExtraImports) {
  434. return {
  435. ignoredExports,
  436. hidden: hiddenExports
  437. };
  438. }
  439. /** @type {Exports} */
  440. const exports = new Set();
  441. /** @type {Checked} */
  442. const checked = new Set();
  443. /** @type {Hidden | undefined} */
  444. const hidden = hiddenExports !== undefined ? new Set() : undefined;
  445. if (noExtraImports) {
  446. for (const exportInfo of exportsInfo.orderedExports) {
  447. const name = exportInfo.name;
  448. if (ignoredExports.has(name)) continue;
  449. if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
  450. const importedExportInfo =
  451. importedExportsInfo.getReadOnlyExportInfo(name);
  452. if (importedExportInfo.provided === false) continue;
  453. if (hiddenExports !== undefined && hiddenExports.has(name)) {
  454. /** @type {Set<string>} */
  455. (hidden).add(name);
  456. continue;
  457. }
  458. exports.add(name);
  459. if (importedExportInfo.provided === true) continue;
  460. checked.add(name);
  461. }
  462. } else if (noExtraExports) {
  463. for (const importedExportInfo of importedExportsInfo.orderedExports) {
  464. const name = importedExportInfo.name;
  465. if (ignoredExports.has(name)) continue;
  466. if (importedExportInfo.provided === false) continue;
  467. const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
  468. if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
  469. if (hiddenExports !== undefined && hiddenExports.has(name)) {
  470. /** @type {ExportModeHidden} */
  471. (hidden).add(name);
  472. continue;
  473. }
  474. exports.add(name);
  475. if (importedExportInfo.provided === true) continue;
  476. checked.add(name);
  477. }
  478. }
  479. return { ignoredExports, exports, checked, hidden };
  480. }
  481. /**
  482. * @param {ModuleGraph} moduleGraph module graph
  483. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  484. */
  485. getCondition(moduleGraph) {
  486. return (connection, runtime) => {
  487. const mode = this.getMode(moduleGraph, runtime);
  488. return mode.type !== "unused" && mode.type !== "empty-star";
  489. };
  490. }
  491. /**
  492. * @param {ModuleGraph} moduleGraph the module graph
  493. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  494. */
  495. getModuleEvaluationSideEffectsState(moduleGraph) {
  496. return false;
  497. }
  498. /**
  499. * Returns list of exports referenced by this dependency
  500. * @param {ModuleGraph} moduleGraph module graph
  501. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  502. * @returns {(string[] | ReferencedExport)[]} referenced exports
  503. */
  504. getReferencedExports(moduleGraph, runtime) {
  505. const mode = this.getMode(moduleGraph, runtime);
  506. switch (mode.type) {
  507. case "missing":
  508. case "unused":
  509. case "empty-star":
  510. case "reexport-undefined":
  511. return Dependency.NO_EXPORTS_REFERENCED;
  512. case "reexport-dynamic-default":
  513. return Dependency.EXPORTS_OBJECT_REFERENCED;
  514. case "reexport-named-default": {
  515. if (!mode.partialNamespaceExportInfo) {
  516. return Dependency.EXPORTS_OBJECT_REFERENCED;
  517. }
  518. /** @type {ReferencedExports} */
  519. const referencedExports = [];
  520. processExportInfo(
  521. runtime,
  522. referencedExports,
  523. [],
  524. /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo)
  525. );
  526. return referencedExports;
  527. }
  528. case "reexport-namespace-object":
  529. case "reexport-fake-namespace-object": {
  530. if (!mode.partialNamespaceExportInfo) {
  531. return Dependency.EXPORTS_OBJECT_REFERENCED;
  532. }
  533. /** @type {ReferencedExports} */
  534. const referencedExports = [];
  535. processExportInfo(
  536. runtime,
  537. referencedExports,
  538. [],
  539. /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo),
  540. mode.type === "reexport-fake-namespace-object"
  541. );
  542. return referencedExports;
  543. }
  544. case "dynamic-reexport":
  545. return Dependency.EXPORTS_OBJECT_REFERENCED;
  546. case "normal-reexport": {
  547. /** @type {ReferencedExports} */
  548. const referencedExports = [];
  549. for (const {
  550. ids,
  551. exportInfo,
  552. hidden
  553. } of /** @type {NormalReexportItem[]} */ (mode.items)) {
  554. if (hidden) continue;
  555. processExportInfo(runtime, referencedExports, ids, exportInfo, false);
  556. }
  557. return referencedExports;
  558. }
  559. default:
  560. throw new Error(`Unknown mode ${mode.type}`);
  561. }
  562. }
  563. /**
  564. * @param {ModuleGraph} moduleGraph the module graph
  565. * @returns {{ names: Names, namesSlice: number, dependencyIndices: DependencyIndices, dependencyIndex: number } | undefined} exported names and their origin dependency
  566. */
  567. _discoverActiveExportsFromOtherStarExports(moduleGraph) {
  568. if (!this.otherStarExports) return;
  569. const i =
  570. "length" in this.otherStarExports
  571. ? this.otherStarExports.length
  572. : countIterable(this.otherStarExports);
  573. if (i === 0) return;
  574. if (this.allStarExports) {
  575. const { names, dependencyIndices } = moduleGraph.cached(
  576. determineExportAssignments,
  577. this.allStarExports.dependencies
  578. );
  579. return {
  580. names,
  581. namesSlice: dependencyIndices[i - 1],
  582. dependencyIndices,
  583. dependencyIndex: i
  584. };
  585. }
  586. const { names, dependencyIndices } = moduleGraph.cached(
  587. determineExportAssignments,
  588. /** @type {HarmonyExportImportedSpecifierDependency[]} */
  589. (this.otherStarExports),
  590. this
  591. );
  592. return {
  593. names,
  594. namesSlice: dependencyIndices[i - 1],
  595. dependencyIndices,
  596. dependencyIndex: i
  597. };
  598. }
  599. /**
  600. * Returns the exported names
  601. * @param {ModuleGraph} moduleGraph module graph
  602. * @returns {ExportsSpec | undefined} export names
  603. */
  604. getExports(moduleGraph) {
  605. const mode = this.getMode(moduleGraph, undefined);
  606. switch (mode.type) {
  607. case "missing":
  608. return;
  609. case "dynamic-reexport": {
  610. const from =
  611. /** @type {ModuleGraphConnection} */
  612. (moduleGraph.getConnection(this));
  613. return {
  614. exports: true,
  615. from,
  616. canMangle: false,
  617. excludeExports: mode.hidden
  618. ? combine(
  619. /** @type {ExportModeIgnored} */ (mode.ignored),
  620. mode.hidden
  621. )
  622. : /** @type {ExportModeIgnored} */ (mode.ignored),
  623. hideExports: mode.hidden,
  624. dependencies: [from.module]
  625. };
  626. }
  627. case "empty-star":
  628. return {
  629. exports: [],
  630. hideExports: mode.hidden,
  631. dependencies: [/** @type {Module} */ (moduleGraph.getModule(this))]
  632. };
  633. // falls through
  634. case "normal-reexport": {
  635. const from =
  636. /** @type {ModuleGraphConnection} */
  637. (moduleGraph.getConnection(this));
  638. return {
  639. exports: Array.from(
  640. /** @type {NormalReexportItem[]} */ (mode.items),
  641. (item) => ({
  642. name: item.name,
  643. from,
  644. export: item.ids,
  645. hidden: item.hidden
  646. })
  647. ),
  648. priority: 1,
  649. dependencies: [from.module]
  650. };
  651. }
  652. case "reexport-dynamic-default": {
  653. const from =
  654. /** @type {ModuleGraphConnection} */
  655. (moduleGraph.getConnection(this));
  656. return {
  657. exports: [
  658. {
  659. name: /** @type {string} */ (mode.name),
  660. from,
  661. export: ["default"]
  662. }
  663. ],
  664. priority: 1,
  665. dependencies: [from.module]
  666. };
  667. }
  668. case "reexport-undefined":
  669. return {
  670. exports: [/** @type {string} */ (mode.name)],
  671. dependencies: [/** @type {Module} */ (moduleGraph.getModule(this))]
  672. };
  673. case "reexport-fake-namespace-object": {
  674. const from =
  675. /** @type {ModuleGraphConnection} */
  676. (moduleGraph.getConnection(this));
  677. return {
  678. exports: [
  679. {
  680. name: /** @type {string} */ (mode.name),
  681. from,
  682. export: null,
  683. exports: [
  684. {
  685. name: "default",
  686. canMangle: false,
  687. from,
  688. export: null
  689. }
  690. ]
  691. }
  692. ],
  693. priority: 1,
  694. dependencies: [from.module]
  695. };
  696. }
  697. case "reexport-namespace-object": {
  698. const from =
  699. /** @type {ModuleGraphConnection} */
  700. (moduleGraph.getConnection(this));
  701. return {
  702. exports: [
  703. {
  704. name: /** @type {string} */ (mode.name),
  705. from,
  706. export: null
  707. }
  708. ],
  709. priority: 1,
  710. dependencies: [from.module]
  711. };
  712. }
  713. case "reexport-named-default": {
  714. const from =
  715. /** @type {ModuleGraphConnection} */
  716. (moduleGraph.getConnection(this));
  717. return {
  718. exports: [
  719. {
  720. name: /** @type {string} */ (mode.name),
  721. from,
  722. export: ["default"]
  723. }
  724. ],
  725. priority: 1,
  726. dependencies: [from.module]
  727. };
  728. }
  729. default:
  730. throw new Error(`Unknown mode ${mode.type}`);
  731. }
  732. }
  733. /**
  734. * @param {ModuleGraph} moduleGraph module graph
  735. * @returns {ExportPresenceMode} effective mode
  736. */
  737. _getEffectiveExportPresenceLevel(moduleGraph) {
  738. if (this.exportPresenceMode !== ExportPresenceModes.AUTO) {
  739. return this.exportPresenceMode;
  740. }
  741. const module = /** @type {Module} */ (moduleGraph.getParentModule(this));
  742. return /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule
  743. ? ExportPresenceModes.ERROR
  744. : ExportPresenceModes.WARN;
  745. }
  746. /**
  747. * Returns warnings
  748. * @param {ModuleGraph} moduleGraph module graph
  749. * @returns {WebpackError[] | null | undefined} warnings
  750. */
  751. getWarnings(moduleGraph) {
  752. const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
  753. if (exportsPresence === ExportPresenceModes.WARN) {
  754. return this._getErrors(moduleGraph);
  755. }
  756. return null;
  757. }
  758. /**
  759. * Returns errors
  760. * @param {ModuleGraph} moduleGraph module graph
  761. * @returns {WebpackError[] | null | undefined} errors
  762. */
  763. getErrors(moduleGraph) {
  764. const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
  765. if (exportsPresence === ExportPresenceModes.ERROR) {
  766. return this._getErrors(moduleGraph);
  767. }
  768. return null;
  769. }
  770. /**
  771. * @param {ModuleGraph} moduleGraph module graph
  772. * @returns {WebpackError[] | undefined} errors
  773. */
  774. _getErrors(moduleGraph) {
  775. const ids = this.getIds(moduleGraph);
  776. let errors = this.getLinkingErrors(
  777. moduleGraph,
  778. ids,
  779. `(reexported as '${this.name}')`
  780. );
  781. if (ids.length === 0 && this.name === null) {
  782. const potentialConflicts =
  783. this._discoverActiveExportsFromOtherStarExports(moduleGraph);
  784. if (potentialConflicts && potentialConflicts.namesSlice > 0) {
  785. const ownNames = new Set(
  786. potentialConflicts.names.slice(
  787. potentialConflicts.namesSlice,
  788. potentialConflicts.dependencyIndices[
  789. potentialConflicts.dependencyIndex
  790. ]
  791. )
  792. );
  793. const importedModule = moduleGraph.getModule(this);
  794. if (importedModule) {
  795. const exportsInfo = moduleGraph.getExportsInfo(importedModule);
  796. /** @type {Map<string, string[]>} */
  797. const conflicts = new Map();
  798. for (const exportInfo of exportsInfo.orderedExports) {
  799. if (exportInfo.provided !== true) continue;
  800. if (exportInfo.name === "default") continue;
  801. if (this.activeExports.has(exportInfo.name)) continue;
  802. if (ownNames.has(exportInfo.name)) continue;
  803. const conflictingDependency = findDependencyForName(
  804. potentialConflicts,
  805. exportInfo.name,
  806. this.allStarExports
  807. ? this.allStarExports.dependencies
  808. : [
  809. .../** @type {ReadonlyArray<HarmonyExportImportedSpecifierDependency>} */
  810. (this.otherStarExports),
  811. this
  812. ]
  813. );
  814. if (!conflictingDependency) continue;
  815. const target = exportInfo.getTerminalBinding(moduleGraph);
  816. if (!target) continue;
  817. const conflictingModule =
  818. /** @type {Module} */
  819. (moduleGraph.getModule(conflictingDependency));
  820. if (conflictingModule === importedModule) continue;
  821. const conflictingExportInfo = moduleGraph.getExportInfo(
  822. conflictingModule,
  823. exportInfo.name
  824. );
  825. const conflictingTarget =
  826. conflictingExportInfo.getTerminalBinding(moduleGraph);
  827. if (!conflictingTarget) continue;
  828. if (target === conflictingTarget) continue;
  829. const list = conflicts.get(conflictingDependency.request);
  830. if (list === undefined) {
  831. conflicts.set(conflictingDependency.request, [exportInfo.name]);
  832. } else {
  833. list.push(exportInfo.name);
  834. }
  835. }
  836. for (const [request, exports] of conflicts) {
  837. if (!errors) errors = [];
  838. errors.push(
  839. new HarmonyLinkingError(
  840. `The requested module '${
  841. this.request
  842. }' contains conflicting star exports for the ${
  843. exports.length > 1 ? "names" : "name"
  844. } ${exports
  845. .map((e) => `'${e}'`)
  846. .join(", ")} with the previous requested module '${request}'`
  847. )
  848. );
  849. }
  850. }
  851. }
  852. }
  853. return errors;
  854. }
  855. /**
  856. * @param {ObjectSerializerContext} context context
  857. */
  858. serialize(context) {
  859. const { write, setCircularReference } = context;
  860. setCircularReference(this);
  861. write(this.ids);
  862. write(this.name);
  863. write(this.activeExports);
  864. write(this.otherStarExports);
  865. write(this.exportPresenceMode);
  866. write(this.allStarExports);
  867. super.serialize(context);
  868. }
  869. /**
  870. * @param {ObjectDeserializerContext} context context
  871. */
  872. deserialize(context) {
  873. const { read, setCircularReference } = context;
  874. setCircularReference(this);
  875. this.ids = read();
  876. this.name = read();
  877. this.activeExports = read();
  878. this.otherStarExports = read();
  879. this.exportPresenceMode = read();
  880. this.allStarExports = read();
  881. super.deserialize(context);
  882. }
  883. }
  884. makeSerializable(
  885. HarmonyExportImportedSpecifierDependency,
  886. "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency"
  887. );
  888. module.exports = HarmonyExportImportedSpecifierDependency;
  889. HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends (
  890. HarmonyImportDependency.Template
  891. ) {
  892. /**
  893. * @param {Dependency} dependency the dependency for which the template should be applied
  894. * @param {ReplaceSource} source the current replace source which can be modified
  895. * @param {DependencyTemplateContext} templateContext the context object
  896. * @returns {void}
  897. */
  898. apply(dependency, source, templateContext) {
  899. const { moduleGraph, runtime, concatenationScope } = templateContext;
  900. const dep = /** @type {HarmonyExportImportedSpecifierDependency} */ (
  901. dependency
  902. );
  903. const mode = dep.getMode(moduleGraph, runtime);
  904. if (concatenationScope) {
  905. switch (mode.type) {
  906. case "reexport-undefined":
  907. concatenationScope.registerRawExport(
  908. /** @type {NonNullable<ExportMode["name"]>} */ (mode.name),
  909. "/* reexport non-default export from non-harmony */ undefined"
  910. );
  911. }
  912. return;
  913. }
  914. if (mode.type !== "unused" && mode.type !== "empty-star") {
  915. super.apply(dependency, source, templateContext);
  916. this._addExportFragments(
  917. templateContext.initFragments,
  918. dep,
  919. mode,
  920. templateContext.module,
  921. moduleGraph,
  922. templateContext.chunkGraph,
  923. runtime,
  924. templateContext.runtimeTemplate,
  925. templateContext.runtimeRequirements
  926. );
  927. }
  928. }
  929. /**
  930. * @param {InitFragment<GenerateContext>[]} initFragments target array for init fragments
  931. * @param {HarmonyExportImportedSpecifierDependency} dep dependency
  932. * @param {ExportMode} mode the export mode
  933. * @param {Module} module the current module
  934. * @param {ModuleGraph} moduleGraph the module graph
  935. * @param {ChunkGraph} chunkGraph the chunk graph
  936. * @param {RuntimeSpec} runtime the runtime
  937. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  938. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  939. * @returns {void}
  940. */
  941. _addExportFragments(
  942. initFragments,
  943. dep,
  944. mode,
  945. module,
  946. moduleGraph,
  947. chunkGraph,
  948. runtime,
  949. runtimeTemplate,
  950. runtimeRequirements
  951. ) {
  952. const importedModule = /** @type {Module} */ (moduleGraph.getModule(dep));
  953. const importVar = dep.getImportVar(moduleGraph);
  954. if (
  955. (mode.type === "reexport-namespace-object" ||
  956. mode.type === "reexport-fake-namespace-object") &&
  957. dep.defer &&
  958. !moduleGraph.isAsync(importedModule)
  959. ) {
  960. initFragments.push(
  961. ...this.getReexportDeferredNamespaceObjectFragments(
  962. importedModule,
  963. chunkGraph,
  964. moduleGraph
  965. .getExportsInfo(module)
  966. .getUsedName(mode.name ? mode.name : [], runtime),
  967. importVar,
  968. importedModule.getExportsType(
  969. moduleGraph,
  970. module.buildMeta && module.buildMeta.strictHarmonyModule
  971. ),
  972. runtimeRequirements
  973. )
  974. );
  975. return;
  976. }
  977. switch (mode.type) {
  978. case "missing":
  979. case "empty-star":
  980. initFragments.push(
  981. new InitFragment(
  982. "/* empty/unused harmony star reexport */\n",
  983. InitFragment.STAGE_HARMONY_EXPORTS,
  984. 1
  985. )
  986. );
  987. break;
  988. case "unused":
  989. initFragments.push(
  990. new InitFragment(
  991. `${Template.toNormalComment(
  992. `unused harmony reexport ${mode.name}`
  993. )}\n`,
  994. InitFragment.STAGE_HARMONY_EXPORTS,
  995. 1
  996. )
  997. );
  998. break;
  999. case "reexport-dynamic-default":
  1000. initFragments.push(
  1001. this.getReexportFragment(
  1002. module,
  1003. "reexport default from dynamic",
  1004. moduleGraph
  1005. .getExportsInfo(module)
  1006. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1007. importVar,
  1008. null,
  1009. runtimeRequirements
  1010. )
  1011. );
  1012. break;
  1013. case "reexport-fake-namespace-object":
  1014. initFragments.push(
  1015. ...this.getReexportFakeNamespaceObjectFragments(
  1016. module,
  1017. moduleGraph
  1018. .getExportsInfo(module)
  1019. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1020. importVar,
  1021. mode.fakeType,
  1022. runtimeRequirements
  1023. )
  1024. );
  1025. break;
  1026. case "reexport-undefined":
  1027. initFragments.push(
  1028. this.getReexportFragment(
  1029. module,
  1030. "reexport non-default export from non-harmony",
  1031. moduleGraph
  1032. .getExportsInfo(module)
  1033. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1034. "undefined",
  1035. "",
  1036. runtimeRequirements
  1037. )
  1038. );
  1039. break;
  1040. case "reexport-named-default":
  1041. initFragments.push(
  1042. this.getReexportFragment(
  1043. module,
  1044. "reexport default export from named module",
  1045. moduleGraph
  1046. .getExportsInfo(module)
  1047. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1048. importVar,
  1049. "",
  1050. runtimeRequirements
  1051. )
  1052. );
  1053. break;
  1054. case "reexport-namespace-object":
  1055. initFragments.push(
  1056. this.getReexportFragment(
  1057. module,
  1058. "reexport module object",
  1059. moduleGraph
  1060. .getExportsInfo(module)
  1061. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1062. importVar,
  1063. "",
  1064. runtimeRequirements
  1065. )
  1066. );
  1067. break;
  1068. case "normal-reexport":
  1069. for (const {
  1070. name,
  1071. ids,
  1072. checked,
  1073. hidden
  1074. } of /** @type {NormalReexportItem[]} */ (mode.items)) {
  1075. if (hidden) continue;
  1076. if (checked) {
  1077. const connection = moduleGraph.getConnection(dep);
  1078. const key = `harmony reexport (checked) ${importVar} ${name}`;
  1079. const runtimeCondition = dep.weak
  1080. ? false
  1081. : connection
  1082. ? filterRuntime(runtime, (r) => connection.isTargetActive(r))
  1083. : true;
  1084. initFragments.push(
  1085. new ConditionalInitFragment(
  1086. `/* harmony reexport (checked) */ ${this.getConditionalReexportStatement(
  1087. module,
  1088. name,
  1089. importVar,
  1090. ids,
  1091. runtimeRequirements
  1092. )}`,
  1093. moduleGraph.isAsync(importedModule)
  1094. ? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
  1095. : InitFragment.STAGE_HARMONY_IMPORTS,
  1096. dep.sourceOrder,
  1097. key,
  1098. runtimeCondition
  1099. )
  1100. );
  1101. } else {
  1102. initFragments.push(
  1103. this.getReexportFragment(
  1104. module,
  1105. "reexport safe",
  1106. moduleGraph.getExportsInfo(module).getUsedName(name, runtime),
  1107. importVar,
  1108. moduleGraph
  1109. .getExportsInfo(importedModule)
  1110. .getUsedName(ids, runtime),
  1111. runtimeRequirements
  1112. )
  1113. );
  1114. }
  1115. }
  1116. break;
  1117. case "dynamic-reexport": {
  1118. const ignored = mode.hidden
  1119. ? combine(
  1120. /** @type {ExportModeIgnored} */
  1121. (mode.ignored),
  1122. mode.hidden
  1123. )
  1124. : /** @type {ExportModeIgnored} */ (mode.ignored);
  1125. const modern =
  1126. runtimeTemplate.supportsConst() &&
  1127. runtimeTemplate.supportsArrowFunction();
  1128. let content =
  1129. "/* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {};\n" +
  1130. `/* harmony reexport (unknown) */ for(${
  1131. modern ? "const" : "var"
  1132. } __WEBPACK_IMPORT_KEY__ in ${importVar}) `;
  1133. // Filter out exports which are defined by other exports
  1134. // and filter out default export because it cannot be reexported with *
  1135. if (ignored.size > 1) {
  1136. content += `if(${JSON.stringify([
  1137. ...ignored
  1138. ])}.indexOf(__WEBPACK_IMPORT_KEY__) < 0) `;
  1139. } else if (ignored.size === 1) {
  1140. content += `if(__WEBPACK_IMPORT_KEY__ !== ${JSON.stringify(
  1141. first(ignored)
  1142. )}) `;
  1143. }
  1144. content += "__WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = ";
  1145. content += modern
  1146. ? `() => ${importVar}[__WEBPACK_IMPORT_KEY__]`
  1147. : `function(key) { return ${importVar}[key]; }.bind(0, __WEBPACK_IMPORT_KEY__)`;
  1148. runtimeRequirements.add(RuntimeGlobals.exports);
  1149. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1150. const exportsName = module.exportsArgument;
  1151. initFragments.push(
  1152. new InitFragment(
  1153. `${content}\n/* harmony reexport (unknown) */ ${RuntimeGlobals.definePropertyGetters}(${exportsName}, __WEBPACK_REEXPORT_OBJECT__);\n`,
  1154. moduleGraph.isAsync(importedModule)
  1155. ? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
  1156. : InitFragment.STAGE_HARMONY_IMPORTS,
  1157. dep.sourceOrder
  1158. )
  1159. );
  1160. break;
  1161. }
  1162. default:
  1163. throw new Error(`Unknown mode ${mode.type}`);
  1164. }
  1165. }
  1166. /**
  1167. * @param {Module} module the current module
  1168. * @param {string} comment comment
  1169. * @param {UsedName} key key
  1170. * @param {string} name name
  1171. * @param {string | string[] | null | false} valueKey value key
  1172. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1173. * @returns {HarmonyExportInitFragment} harmony export init fragment
  1174. */
  1175. getReexportFragment(
  1176. module,
  1177. comment,
  1178. key,
  1179. name,
  1180. valueKey,
  1181. runtimeRequirements
  1182. ) {
  1183. const returnValue = this.getReturnValue(name, valueKey);
  1184. runtimeRequirements.add(RuntimeGlobals.exports);
  1185. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1186. const map = new Map();
  1187. map.set(key, `/* ${comment} */ ${returnValue}`);
  1188. return new HarmonyExportInitFragment(module.exportsArgument, map);
  1189. }
  1190. /**
  1191. * @param {Module} module module
  1192. * @param {string | string[] | false} key key
  1193. * @param {string} name name
  1194. * @param {number} fakeType fake type
  1195. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1196. * @returns {[InitFragment<GenerateContext>, HarmonyExportInitFragment]} init fragments
  1197. */
  1198. getReexportFakeNamespaceObjectFragments(
  1199. module,
  1200. key,
  1201. name,
  1202. fakeType,
  1203. runtimeRequirements
  1204. ) {
  1205. runtimeRequirements.add(RuntimeGlobals.exports);
  1206. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1207. runtimeRequirements.add(RuntimeGlobals.createFakeNamespaceObject);
  1208. const map = new Map();
  1209. map.set(
  1210. key,
  1211. `/* reexport fake namespace object from non-harmony */ ${name}_namespace_cache || (${name}_namespace_cache = ${
  1212. RuntimeGlobals.createFakeNamespaceObject
  1213. }(${name}${fakeType ? `, ${fakeType}` : ""}))`
  1214. );
  1215. return [
  1216. new InitFragment(
  1217. `var ${name}_namespace_cache;\n`,
  1218. InitFragment.STAGE_CONSTANTS,
  1219. -1,
  1220. `${name}_namespace_cache`
  1221. ),
  1222. new HarmonyExportInitFragment(module.exportsArgument, map)
  1223. ];
  1224. }
  1225. /**
  1226. * @param {Module} module module
  1227. * @param {ChunkGraph} chunkGraph chunkGraph
  1228. * @param {string | false | string[]} key key
  1229. * @param {string} name name
  1230. * @param {import("../Module").ExportsType} exportsType exportsType
  1231. * @param {Set<string>} runtimeRequirements runtimeRequirements
  1232. * @returns {InitFragment<GenerateContext>[]} fragments
  1233. */
  1234. getReexportDeferredNamespaceObjectFragments(
  1235. module,
  1236. chunkGraph,
  1237. key,
  1238. name,
  1239. exportsType,
  1240. runtimeRequirements
  1241. ) {
  1242. runtimeRequirements.add(RuntimeGlobals.exports);
  1243. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1244. runtimeRequirements.add(RuntimeGlobals.makeDeferredNamespaceObject);
  1245. const map = new Map();
  1246. const moduleId = JSON.stringify(chunkGraph.getModuleId(module));
  1247. const mode = getMakeDeferredNamespaceModeFromExportsType(exportsType);
  1248. map.set(
  1249. key,
  1250. `/* reexport deferred namespace object */ ${name}_deferred_namespace_cache || (${name}_deferred_namespace_cache = ${RuntimeGlobals.makeDeferredNamespaceObject}(${moduleId}, ${mode}))`
  1251. );
  1252. return [
  1253. new InitFragment(
  1254. `var ${name}_deferred_namespace_cache;\n`,
  1255. InitFragment.STAGE_CONSTANTS,
  1256. -1,
  1257. `${name}_deferred_namespace_cache`
  1258. ),
  1259. new HarmonyExportInitFragment(module.exportsArgument, map)
  1260. ];
  1261. }
  1262. /**
  1263. * @param {Module} module module
  1264. * @param {string} key key
  1265. * @param {string} name name
  1266. * @param {string | string[] | false} valueKey value key
  1267. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1268. * @returns {string} result
  1269. */
  1270. getConditionalReexportStatement(
  1271. module,
  1272. key,
  1273. name,
  1274. valueKey,
  1275. runtimeRequirements
  1276. ) {
  1277. if (valueKey === false) {
  1278. return "/* unused export */\n";
  1279. }
  1280. const exportsName = module.exportsArgument;
  1281. const returnValue = this.getReturnValue(name, valueKey);
  1282. runtimeRequirements.add(RuntimeGlobals.exports);
  1283. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1284. runtimeRequirements.add(RuntimeGlobals.hasOwnProperty);
  1285. return `if(${RuntimeGlobals.hasOwnProperty}(${name}, ${JSON.stringify(
  1286. valueKey[0]
  1287. )})) ${
  1288. RuntimeGlobals.definePropertyGetters
  1289. }(${exportsName}, { ${propertyName(
  1290. key
  1291. )}: function() { return ${returnValue}; } });\n`;
  1292. }
  1293. /**
  1294. * @param {string} name name
  1295. * @param {null | false | string | string[]} valueKey value key
  1296. * @returns {string | undefined} value
  1297. */
  1298. getReturnValue(name, valueKey) {
  1299. if (valueKey === null) {
  1300. return `${name}_default.a`;
  1301. }
  1302. if (valueKey === "") {
  1303. return name;
  1304. }
  1305. if (valueKey === false) {
  1306. return "/* unused export */ undefined";
  1307. }
  1308. return `${name}${propertyAccess(valueKey)}`;
  1309. }
  1310. };
  1311. class HarmonyStarExportsList {
  1312. constructor() {
  1313. /** @type {HarmonyExportImportedSpecifierDependency[]} */
  1314. this.dependencies = [];
  1315. }
  1316. /**
  1317. * @param {HarmonyExportImportedSpecifierDependency} dep dependency
  1318. * @returns {void}
  1319. */
  1320. push(dep) {
  1321. this.dependencies.push(dep);
  1322. }
  1323. slice() {
  1324. return [...this.dependencies];
  1325. }
  1326. /**
  1327. * @param {ObjectSerializerContext} context context
  1328. */
  1329. serialize({ write, setCircularReference }) {
  1330. setCircularReference(this);
  1331. write(this.dependencies);
  1332. }
  1333. /**
  1334. * @param {ObjectDeserializerContext} context context
  1335. */
  1336. deserialize({ read, setCircularReference }) {
  1337. setCircularReference(this);
  1338. this.dependencies = read();
  1339. }
  1340. }
  1341. makeSerializable(
  1342. HarmonyStarExportsList,
  1343. "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency",
  1344. "HarmonyStarExportsList"
  1345. );
  1346. module.exports.HarmonyStarExportsList = HarmonyStarExportsList;