HarmonyExportImportedSpecifierDependency.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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=} deferEvaluation defer evaluation
  333. */
  334. constructor(
  335. request,
  336. sourceOrder,
  337. ids,
  338. name,
  339. activeExports,
  340. otherStarExports,
  341. exportPresenceMode,
  342. allStarExports,
  343. attributes,
  344. deferEvaluation
  345. ) {
  346. super(request, sourceOrder, attributes);
  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. this.defer = deferEvaluation;
  354. }
  355. /**
  356. * @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
  357. */
  358. couldAffectReferencingModule() {
  359. return Dependency.TRANSITIVE;
  360. }
  361. // TODO webpack 6 remove
  362. get id() {
  363. throw new Error("id was renamed to ids and type changed to string[]");
  364. }
  365. // TODO webpack 6 remove
  366. getId() {
  367. throw new Error("id was renamed to ids and type changed to string[]");
  368. }
  369. // TODO webpack 6 remove
  370. setId() {
  371. throw new Error("id was renamed to ids and type changed to string[]");
  372. }
  373. get type() {
  374. return "harmony export imported specifier";
  375. }
  376. /**
  377. * @param {ModuleGraph} moduleGraph the module graph
  378. * @returns {Ids} the imported id
  379. */
  380. getIds(moduleGraph) {
  381. return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
  382. }
  383. /**
  384. * @param {ModuleGraph} moduleGraph the module graph
  385. * @param {Ids} ids the imported ids
  386. * @returns {void}
  387. */
  388. setIds(moduleGraph, ids) {
  389. moduleGraph.getMeta(this)[idsSymbol] = ids;
  390. }
  391. /**
  392. * @param {ModuleGraph} moduleGraph the module graph
  393. * @param {RuntimeSpec} runtime the runtime
  394. * @returns {ExportMode} the export mode
  395. */
  396. getMode(moduleGraph, runtime) {
  397. return moduleGraph.dependencyCacheProvide(
  398. this,
  399. getRuntimeKey(runtime),
  400. getMode
  401. );
  402. }
  403. /**
  404. * @param {ModuleGraph} moduleGraph the module graph
  405. * @param {RuntimeSpec} runtime the runtime
  406. * @param {ExportsInfo} exportsInfo exports info about the current module (optional)
  407. * @param {Module} importedModule the imported module (optional)
  408. * @returns {{exports?: Exports, checked?: Checked, ignoredExports: IgnoredExports, hidden?: Hidden}} information
  409. */
  410. getStarReexports(
  411. moduleGraph,
  412. runtime,
  413. exportsInfo = moduleGraph.getExportsInfo(
  414. /** @type {Module} */ (moduleGraph.getParentModule(this))
  415. ),
  416. importedModule = /** @type {Module} */ (moduleGraph.getModule(this))
  417. ) {
  418. const importedExportsInfo = moduleGraph.getExportsInfo(importedModule);
  419. const noExtraExports =
  420. importedExportsInfo.otherExportsInfo.provided === false;
  421. const noExtraImports =
  422. exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused;
  423. const ignoredExports = new Set(["default", ...this.activeExports]);
  424. let hiddenExports;
  425. const otherStarExports =
  426. this._discoverActiveExportsFromOtherStarExports(moduleGraph);
  427. if (otherStarExports !== undefined) {
  428. hiddenExports = new Set();
  429. for (let i = 0; i < otherStarExports.namesSlice; i++) {
  430. hiddenExports.add(otherStarExports.names[i]);
  431. }
  432. for (const e of ignoredExports) hiddenExports.delete(e);
  433. }
  434. if (!noExtraExports && !noExtraImports) {
  435. return {
  436. ignoredExports,
  437. hidden: hiddenExports
  438. };
  439. }
  440. /** @type {Exports} */
  441. const exports = new Set();
  442. /** @type {Checked} */
  443. const checked = new Set();
  444. /** @type {Hidden | undefined} */
  445. const hidden = hiddenExports !== undefined ? new Set() : undefined;
  446. if (noExtraImports) {
  447. for (const exportInfo of exportsInfo.orderedExports) {
  448. const name = exportInfo.name;
  449. if (ignoredExports.has(name)) continue;
  450. if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
  451. const importedExportInfo =
  452. importedExportsInfo.getReadOnlyExportInfo(name);
  453. if (importedExportInfo.provided === false) continue;
  454. if (hiddenExports !== undefined && hiddenExports.has(name)) {
  455. /** @type {Set<string>} */
  456. (hidden).add(name);
  457. continue;
  458. }
  459. exports.add(name);
  460. if (importedExportInfo.provided === true) continue;
  461. checked.add(name);
  462. }
  463. } else if (noExtraExports) {
  464. for (const importedExportInfo of importedExportsInfo.orderedExports) {
  465. const name = importedExportInfo.name;
  466. if (ignoredExports.has(name)) continue;
  467. if (importedExportInfo.provided === false) continue;
  468. const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
  469. if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
  470. if (hiddenExports !== undefined && hiddenExports.has(name)) {
  471. /** @type {ExportModeHidden} */
  472. (hidden).add(name);
  473. continue;
  474. }
  475. exports.add(name);
  476. if (importedExportInfo.provided === true) continue;
  477. checked.add(name);
  478. }
  479. }
  480. return { ignoredExports, exports, checked, hidden };
  481. }
  482. /**
  483. * @param {ModuleGraph} moduleGraph module graph
  484. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  485. */
  486. getCondition(moduleGraph) {
  487. return (connection, runtime) => {
  488. const mode = this.getMode(moduleGraph, runtime);
  489. return mode.type !== "unused" && mode.type !== "empty-star";
  490. };
  491. }
  492. /**
  493. * @param {ModuleGraph} moduleGraph the module graph
  494. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  495. */
  496. getModuleEvaluationSideEffectsState(moduleGraph) {
  497. return false;
  498. }
  499. /**
  500. * Returns list of exports referenced by this dependency
  501. * @param {ModuleGraph} moduleGraph module graph
  502. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  503. * @returns {(string[] | ReferencedExport)[]} referenced exports
  504. */
  505. getReferencedExports(moduleGraph, runtime) {
  506. const mode = this.getMode(moduleGraph, runtime);
  507. switch (mode.type) {
  508. case "missing":
  509. case "unused":
  510. case "empty-star":
  511. case "reexport-undefined":
  512. return Dependency.NO_EXPORTS_REFERENCED;
  513. case "reexport-dynamic-default":
  514. return Dependency.EXPORTS_OBJECT_REFERENCED;
  515. case "reexport-named-default": {
  516. if (!mode.partialNamespaceExportInfo) {
  517. return Dependency.EXPORTS_OBJECT_REFERENCED;
  518. }
  519. /** @type {ReferencedExports} */
  520. const referencedExports = [];
  521. processExportInfo(
  522. runtime,
  523. referencedExports,
  524. [],
  525. /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo)
  526. );
  527. return referencedExports;
  528. }
  529. case "reexport-namespace-object":
  530. case "reexport-fake-namespace-object": {
  531. if (!mode.partialNamespaceExportInfo) {
  532. return Dependency.EXPORTS_OBJECT_REFERENCED;
  533. }
  534. /** @type {ReferencedExports} */
  535. const referencedExports = [];
  536. processExportInfo(
  537. runtime,
  538. referencedExports,
  539. [],
  540. /** @type {ExportInfo} */ (mode.partialNamespaceExportInfo),
  541. mode.type === "reexport-fake-namespace-object"
  542. );
  543. return referencedExports;
  544. }
  545. case "dynamic-reexport":
  546. return Dependency.EXPORTS_OBJECT_REFERENCED;
  547. case "normal-reexport": {
  548. /** @type {ReferencedExports} */
  549. const referencedExports = [];
  550. for (const {
  551. ids,
  552. exportInfo,
  553. hidden
  554. } of /** @type {NormalReexportItem[]} */ (mode.items)) {
  555. if (hidden) continue;
  556. processExportInfo(runtime, referencedExports, ids, exportInfo, false);
  557. }
  558. return referencedExports;
  559. }
  560. default:
  561. throw new Error(`Unknown mode ${mode.type}`);
  562. }
  563. }
  564. /**
  565. * @param {ModuleGraph} moduleGraph the module graph
  566. * @returns {{ names: Names, namesSlice: number, dependencyIndices: DependencyIndices, dependencyIndex: number } | undefined} exported names and their origin dependency
  567. */
  568. _discoverActiveExportsFromOtherStarExports(moduleGraph) {
  569. if (!this.otherStarExports) return;
  570. const i =
  571. "length" in this.otherStarExports
  572. ? this.otherStarExports.length
  573. : countIterable(this.otherStarExports);
  574. if (i === 0) return;
  575. if (this.allStarExports) {
  576. const { names, dependencyIndices } = moduleGraph.cached(
  577. determineExportAssignments,
  578. this.allStarExports.dependencies
  579. );
  580. return {
  581. names,
  582. namesSlice: dependencyIndices[i - 1],
  583. dependencyIndices,
  584. dependencyIndex: i
  585. };
  586. }
  587. const { names, dependencyIndices } = moduleGraph.cached(
  588. determineExportAssignments,
  589. /** @type {HarmonyExportImportedSpecifierDependency[]} */
  590. (this.otherStarExports),
  591. this
  592. );
  593. return {
  594. names,
  595. namesSlice: dependencyIndices[i - 1],
  596. dependencyIndices,
  597. dependencyIndex: i
  598. };
  599. }
  600. /**
  601. * Returns the exported names
  602. * @param {ModuleGraph} moduleGraph module graph
  603. * @returns {ExportsSpec | undefined} export names
  604. */
  605. getExports(moduleGraph) {
  606. const mode = this.getMode(moduleGraph, undefined);
  607. switch (mode.type) {
  608. case "missing":
  609. return;
  610. case "dynamic-reexport": {
  611. const from =
  612. /** @type {ModuleGraphConnection} */
  613. (moduleGraph.getConnection(this));
  614. return {
  615. exports: true,
  616. from,
  617. canMangle: false,
  618. excludeExports: mode.hidden
  619. ? combine(
  620. /** @type {ExportModeIgnored} */ (mode.ignored),
  621. mode.hidden
  622. )
  623. : /** @type {ExportModeIgnored} */ (mode.ignored),
  624. hideExports: mode.hidden,
  625. dependencies: [from.module]
  626. };
  627. }
  628. case "empty-star":
  629. return {
  630. exports: [],
  631. hideExports: mode.hidden,
  632. dependencies: [/** @type {Module} */ (moduleGraph.getModule(this))]
  633. };
  634. // falls through
  635. case "normal-reexport": {
  636. const from =
  637. /** @type {ModuleGraphConnection} */
  638. (moduleGraph.getConnection(this));
  639. return {
  640. exports: Array.from(
  641. /** @type {NormalReexportItem[]} */ (mode.items),
  642. item => ({
  643. name: item.name,
  644. from,
  645. export: item.ids,
  646. hidden: item.hidden
  647. })
  648. ),
  649. priority: 1,
  650. dependencies: [from.module]
  651. };
  652. }
  653. case "reexport-dynamic-default": {
  654. const from =
  655. /** @type {ModuleGraphConnection} */
  656. (moduleGraph.getConnection(this));
  657. return {
  658. exports: [
  659. {
  660. name: /** @type {string} */ (mode.name),
  661. from,
  662. export: ["default"]
  663. }
  664. ],
  665. priority: 1,
  666. dependencies: [from.module]
  667. };
  668. }
  669. case "reexport-undefined":
  670. return {
  671. exports: [/** @type {string} */ (mode.name)],
  672. dependencies: [/** @type {Module} */ (moduleGraph.getModule(this))]
  673. };
  674. case "reexport-fake-namespace-object": {
  675. const from =
  676. /** @type {ModuleGraphConnection} */
  677. (moduleGraph.getConnection(this));
  678. return {
  679. exports: [
  680. {
  681. name: /** @type {string} */ (mode.name),
  682. from,
  683. export: null,
  684. exports: [
  685. {
  686. name: "default",
  687. canMangle: false,
  688. from,
  689. export: null
  690. }
  691. ]
  692. }
  693. ],
  694. priority: 1,
  695. dependencies: [from.module]
  696. };
  697. }
  698. case "reexport-namespace-object": {
  699. const from =
  700. /** @type {ModuleGraphConnection} */
  701. (moduleGraph.getConnection(this));
  702. return {
  703. exports: [
  704. {
  705. name: /** @type {string} */ (mode.name),
  706. from,
  707. export: null
  708. }
  709. ],
  710. priority: 1,
  711. dependencies: [from.module]
  712. };
  713. }
  714. case "reexport-named-default": {
  715. const from =
  716. /** @type {ModuleGraphConnection} */
  717. (moduleGraph.getConnection(this));
  718. return {
  719. exports: [
  720. {
  721. name: /** @type {string} */ (mode.name),
  722. from,
  723. export: ["default"]
  724. }
  725. ],
  726. priority: 1,
  727. dependencies: [from.module]
  728. };
  729. }
  730. default:
  731. throw new Error(`Unknown mode ${mode.type}`);
  732. }
  733. }
  734. /**
  735. * @param {ModuleGraph} moduleGraph module graph
  736. * @returns {ExportPresenceMode} effective mode
  737. */
  738. _getEffectiveExportPresenceLevel(moduleGraph) {
  739. if (this.exportPresenceMode !== ExportPresenceModes.AUTO) {
  740. return this.exportPresenceMode;
  741. }
  742. const module = /** @type {Module} */ (moduleGraph.getParentModule(this));
  743. return /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule
  744. ? ExportPresenceModes.ERROR
  745. : ExportPresenceModes.WARN;
  746. }
  747. /**
  748. * Returns warnings
  749. * @param {ModuleGraph} moduleGraph module graph
  750. * @returns {WebpackError[] | null | undefined} warnings
  751. */
  752. getWarnings(moduleGraph) {
  753. const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
  754. if (exportsPresence === ExportPresenceModes.WARN) {
  755. return this._getErrors(moduleGraph);
  756. }
  757. return null;
  758. }
  759. /**
  760. * Returns errors
  761. * @param {ModuleGraph} moduleGraph module graph
  762. * @returns {WebpackError[] | null | undefined} errors
  763. */
  764. getErrors(moduleGraph) {
  765. const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
  766. if (exportsPresence === ExportPresenceModes.ERROR) {
  767. return this._getErrors(moduleGraph);
  768. }
  769. return null;
  770. }
  771. /**
  772. * @param {ModuleGraph} moduleGraph module graph
  773. * @returns {WebpackError[] | undefined} errors
  774. */
  775. _getErrors(moduleGraph) {
  776. const ids = this.getIds(moduleGraph);
  777. let errors = this.getLinkingErrors(
  778. moduleGraph,
  779. ids,
  780. `(reexported as '${this.name}')`
  781. );
  782. if (ids.length === 0 && this.name === null) {
  783. const potentialConflicts =
  784. this._discoverActiveExportsFromOtherStarExports(moduleGraph);
  785. if (potentialConflicts && potentialConflicts.namesSlice > 0) {
  786. const ownNames = new Set(
  787. potentialConflicts.names.slice(
  788. potentialConflicts.namesSlice,
  789. potentialConflicts.dependencyIndices[
  790. potentialConflicts.dependencyIndex
  791. ]
  792. )
  793. );
  794. const importedModule = moduleGraph.getModule(this);
  795. if (importedModule) {
  796. const exportsInfo = moduleGraph.getExportsInfo(importedModule);
  797. /** @type {Map<string, string[]>} */
  798. const conflicts = new Map();
  799. for (const exportInfo of exportsInfo.orderedExports) {
  800. if (exportInfo.provided !== true) continue;
  801. if (exportInfo.name === "default") continue;
  802. if (this.activeExports.has(exportInfo.name)) continue;
  803. if (ownNames.has(exportInfo.name)) continue;
  804. const conflictingDependency = findDependencyForName(
  805. potentialConflicts,
  806. exportInfo.name,
  807. this.allStarExports
  808. ? this.allStarExports.dependencies
  809. : [
  810. .../** @type {ReadonlyArray<HarmonyExportImportedSpecifierDependency>} */
  811. (this.otherStarExports),
  812. this
  813. ]
  814. );
  815. if (!conflictingDependency) continue;
  816. const target = exportInfo.getTerminalBinding(moduleGraph);
  817. if (!target) continue;
  818. const conflictingModule =
  819. /** @type {Module} */
  820. (moduleGraph.getModule(conflictingDependency));
  821. if (conflictingModule === importedModule) continue;
  822. const conflictingExportInfo = moduleGraph.getExportInfo(
  823. conflictingModule,
  824. exportInfo.name
  825. );
  826. const conflictingTarget =
  827. conflictingExportInfo.getTerminalBinding(moduleGraph);
  828. if (!conflictingTarget) continue;
  829. if (target === conflictingTarget) continue;
  830. const list = conflicts.get(conflictingDependency.request);
  831. if (list === undefined) {
  832. conflicts.set(conflictingDependency.request, [exportInfo.name]);
  833. } else {
  834. list.push(exportInfo.name);
  835. }
  836. }
  837. for (const [request, exports] of conflicts) {
  838. if (!errors) errors = [];
  839. errors.push(
  840. new HarmonyLinkingError(
  841. `The requested module '${
  842. this.request
  843. }' contains conflicting star exports for the ${
  844. exports.length > 1 ? "names" : "name"
  845. } ${exports
  846. .map(e => `'${e}'`)
  847. .join(", ")} with the previous requested module '${request}'`
  848. )
  849. );
  850. }
  851. }
  852. }
  853. }
  854. return errors;
  855. }
  856. /**
  857. * @param {ObjectSerializerContext} context context
  858. */
  859. serialize(context) {
  860. const { write, setCircularReference } = context;
  861. setCircularReference(this);
  862. write(this.ids);
  863. write(this.name);
  864. write(this.activeExports);
  865. write(this.otherStarExports);
  866. write(this.exportPresenceMode);
  867. write(this.allStarExports);
  868. super.serialize(context);
  869. }
  870. /**
  871. * @param {ObjectDeserializerContext} context context
  872. */
  873. deserialize(context) {
  874. const { read, setCircularReference } = context;
  875. setCircularReference(this);
  876. this.ids = read();
  877. this.name = read();
  878. this.activeExports = read();
  879. this.otherStarExports = read();
  880. this.exportPresenceMode = read();
  881. this.allStarExports = read();
  882. super.deserialize(context);
  883. }
  884. }
  885. makeSerializable(
  886. HarmonyExportImportedSpecifierDependency,
  887. "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency"
  888. );
  889. module.exports = HarmonyExportImportedSpecifierDependency;
  890. HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends (
  891. HarmonyImportDependency.Template
  892. ) {
  893. /**
  894. * @param {Dependency} dependency the dependency for which the template should be applied
  895. * @param {ReplaceSource} source the current replace source which can be modified
  896. * @param {DependencyTemplateContext} templateContext the context object
  897. * @returns {void}
  898. */
  899. apply(dependency, source, templateContext) {
  900. const { moduleGraph, runtime, concatenationScope } = templateContext;
  901. const dep = /** @type {HarmonyExportImportedSpecifierDependency} */ (
  902. dependency
  903. );
  904. const mode = dep.getMode(moduleGraph, runtime);
  905. if (concatenationScope) {
  906. switch (mode.type) {
  907. case "reexport-undefined":
  908. concatenationScope.registerRawExport(
  909. /** @type {NonNullable<ExportMode["name"]>} */ (mode.name),
  910. "/* reexport non-default export from non-harmony */ undefined"
  911. );
  912. }
  913. return;
  914. }
  915. if (mode.type !== "unused" && mode.type !== "empty-star") {
  916. super.apply(dependency, source, templateContext);
  917. this._addExportFragments(
  918. templateContext.initFragments,
  919. dep,
  920. mode,
  921. templateContext.module,
  922. moduleGraph,
  923. templateContext.chunkGraph,
  924. runtime,
  925. templateContext.runtimeTemplate,
  926. templateContext.runtimeRequirements
  927. );
  928. }
  929. }
  930. /**
  931. * @param {InitFragment<GenerateContext>[]} initFragments target array for init fragments
  932. * @param {HarmonyExportImportedSpecifierDependency} dep dependency
  933. * @param {ExportMode} mode the export mode
  934. * @param {Module} module the current module
  935. * @param {ModuleGraph} moduleGraph the module graph
  936. * @param {ChunkGraph} chunkGraph the chunk graph
  937. * @param {RuntimeSpec} runtime the runtime
  938. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  939. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  940. * @returns {void}
  941. */
  942. _addExportFragments(
  943. initFragments,
  944. dep,
  945. mode,
  946. module,
  947. moduleGraph,
  948. chunkGraph,
  949. runtime,
  950. runtimeTemplate,
  951. runtimeRequirements
  952. ) {
  953. const importedModule = /** @type {Module} */ (moduleGraph.getModule(dep));
  954. const importVar = dep.getImportVar(moduleGraph);
  955. if (
  956. (mode.type === "reexport-namespace-object" ||
  957. mode.type === "reexport-fake-namespace-object") &&
  958. dep.defer &&
  959. !moduleGraph.isAsync(importedModule)
  960. ) {
  961. initFragments.push(
  962. ...this.getReexportDeferredNamespaceObjectFragments(
  963. importedModule,
  964. chunkGraph,
  965. moduleGraph
  966. .getExportsInfo(module)
  967. .getUsedName(mode.name ? mode.name : [], runtime),
  968. importVar,
  969. importedModule.getExportsType(
  970. moduleGraph,
  971. module.buildMeta && module.buildMeta.strictHarmonyModule
  972. ),
  973. runtimeRequirements
  974. )
  975. );
  976. return;
  977. }
  978. switch (mode.type) {
  979. case "missing":
  980. case "empty-star":
  981. initFragments.push(
  982. new InitFragment(
  983. "/* empty/unused harmony star reexport */\n",
  984. InitFragment.STAGE_HARMONY_EXPORTS,
  985. 1
  986. )
  987. );
  988. break;
  989. case "unused":
  990. initFragments.push(
  991. new InitFragment(
  992. `${Template.toNormalComment(
  993. `unused harmony reexport ${mode.name}`
  994. )}\n`,
  995. InitFragment.STAGE_HARMONY_EXPORTS,
  996. 1
  997. )
  998. );
  999. break;
  1000. case "reexport-dynamic-default":
  1001. initFragments.push(
  1002. this.getReexportFragment(
  1003. module,
  1004. "reexport default from dynamic",
  1005. moduleGraph
  1006. .getExportsInfo(module)
  1007. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1008. importVar,
  1009. null,
  1010. runtimeRequirements
  1011. )
  1012. );
  1013. break;
  1014. case "reexport-fake-namespace-object":
  1015. initFragments.push(
  1016. ...this.getReexportFakeNamespaceObjectFragments(
  1017. module,
  1018. moduleGraph
  1019. .getExportsInfo(module)
  1020. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1021. importVar,
  1022. mode.fakeType,
  1023. runtimeRequirements
  1024. )
  1025. );
  1026. break;
  1027. case "reexport-undefined":
  1028. initFragments.push(
  1029. this.getReexportFragment(
  1030. module,
  1031. "reexport non-default export from non-harmony",
  1032. moduleGraph
  1033. .getExportsInfo(module)
  1034. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1035. "undefined",
  1036. "",
  1037. runtimeRequirements
  1038. )
  1039. );
  1040. break;
  1041. case "reexport-named-default":
  1042. initFragments.push(
  1043. this.getReexportFragment(
  1044. module,
  1045. "reexport default export from named module",
  1046. moduleGraph
  1047. .getExportsInfo(module)
  1048. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1049. importVar,
  1050. "",
  1051. runtimeRequirements
  1052. )
  1053. );
  1054. break;
  1055. case "reexport-namespace-object":
  1056. initFragments.push(
  1057. this.getReexportFragment(
  1058. module,
  1059. "reexport module object",
  1060. moduleGraph
  1061. .getExportsInfo(module)
  1062. .getUsedName(/** @type {string} */ (mode.name), runtime),
  1063. importVar,
  1064. "",
  1065. runtimeRequirements
  1066. )
  1067. );
  1068. break;
  1069. case "normal-reexport":
  1070. for (const {
  1071. name,
  1072. ids,
  1073. checked,
  1074. hidden
  1075. } of /** @type {NormalReexportItem[]} */ (mode.items)) {
  1076. if (hidden) continue;
  1077. if (checked) {
  1078. const connection = moduleGraph.getConnection(dep);
  1079. const key = `harmony reexport (checked) ${importVar} ${name}`;
  1080. const runtimeCondition = dep.weak
  1081. ? false
  1082. : connection
  1083. ? filterRuntime(runtime, r => connection.isTargetActive(r))
  1084. : true;
  1085. initFragments.push(
  1086. new ConditionalInitFragment(
  1087. `/* harmony reexport (checked) */ ${this.getConditionalReexportStatement(
  1088. module,
  1089. name,
  1090. importVar,
  1091. ids,
  1092. runtimeRequirements
  1093. )}`,
  1094. moduleGraph.isAsync(importedModule)
  1095. ? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
  1096. : InitFragment.STAGE_HARMONY_IMPORTS,
  1097. dep.sourceOrder,
  1098. key,
  1099. runtimeCondition
  1100. )
  1101. );
  1102. } else {
  1103. initFragments.push(
  1104. this.getReexportFragment(
  1105. module,
  1106. "reexport safe",
  1107. moduleGraph.getExportsInfo(module).getUsedName(name, runtime),
  1108. importVar,
  1109. moduleGraph
  1110. .getExportsInfo(importedModule)
  1111. .getUsedName(ids, runtime),
  1112. runtimeRequirements
  1113. )
  1114. );
  1115. }
  1116. }
  1117. break;
  1118. case "dynamic-reexport": {
  1119. const ignored = mode.hidden
  1120. ? combine(
  1121. /** @type {ExportModeIgnored} */
  1122. (mode.ignored),
  1123. mode.hidden
  1124. )
  1125. : /** @type {ExportModeIgnored} */ (mode.ignored);
  1126. const modern =
  1127. runtimeTemplate.supportsConst() &&
  1128. runtimeTemplate.supportsArrowFunction();
  1129. let content =
  1130. "/* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {};\n" +
  1131. `/* harmony reexport (unknown) */ for(${
  1132. modern ? "const" : "var"
  1133. } __WEBPACK_IMPORT_KEY__ in ${importVar}) `;
  1134. // Filter out exports which are defined by other exports
  1135. // and filter out default export because it cannot be reexported with *
  1136. if (ignored.size > 1) {
  1137. content += `if(${JSON.stringify([
  1138. ...ignored
  1139. ])}.indexOf(__WEBPACK_IMPORT_KEY__) < 0) `;
  1140. } else if (ignored.size === 1) {
  1141. content += `if(__WEBPACK_IMPORT_KEY__ !== ${JSON.stringify(
  1142. first(ignored)
  1143. )}) `;
  1144. }
  1145. content += "__WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = ";
  1146. content += modern
  1147. ? `() => ${importVar}[__WEBPACK_IMPORT_KEY__]`
  1148. : `function(key) { return ${importVar}[key]; }.bind(0, __WEBPACK_IMPORT_KEY__)`;
  1149. runtimeRequirements.add(RuntimeGlobals.exports);
  1150. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1151. const exportsName = module.exportsArgument;
  1152. initFragments.push(
  1153. new InitFragment(
  1154. `${content}\n/* harmony reexport (unknown) */ ${RuntimeGlobals.definePropertyGetters}(${exportsName}, __WEBPACK_REEXPORT_OBJECT__);\n`,
  1155. moduleGraph.isAsync(importedModule)
  1156. ? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
  1157. : InitFragment.STAGE_HARMONY_IMPORTS,
  1158. dep.sourceOrder
  1159. )
  1160. );
  1161. break;
  1162. }
  1163. default:
  1164. throw new Error(`Unknown mode ${mode.type}`);
  1165. }
  1166. }
  1167. /**
  1168. * @param {Module} module the current module
  1169. * @param {string} comment comment
  1170. * @param {UsedName} key key
  1171. * @param {string} name name
  1172. * @param {string | string[] | null | false} valueKey value key
  1173. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1174. * @returns {HarmonyExportInitFragment} harmony export init fragment
  1175. */
  1176. getReexportFragment(
  1177. module,
  1178. comment,
  1179. key,
  1180. name,
  1181. valueKey,
  1182. runtimeRequirements
  1183. ) {
  1184. const returnValue = this.getReturnValue(name, valueKey);
  1185. runtimeRequirements.add(RuntimeGlobals.exports);
  1186. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1187. const map = new Map();
  1188. map.set(key, `/* ${comment} */ ${returnValue}`);
  1189. return new HarmonyExportInitFragment(module.exportsArgument, map);
  1190. }
  1191. /**
  1192. * @param {Module} module module
  1193. * @param {string | string[] | false} key key
  1194. * @param {string} name name
  1195. * @param {number} fakeType fake type
  1196. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1197. * @returns {[InitFragment<GenerateContext>, HarmonyExportInitFragment]} init fragments
  1198. */
  1199. getReexportFakeNamespaceObjectFragments(
  1200. module,
  1201. key,
  1202. name,
  1203. fakeType,
  1204. runtimeRequirements
  1205. ) {
  1206. runtimeRequirements.add(RuntimeGlobals.exports);
  1207. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1208. runtimeRequirements.add(RuntimeGlobals.createFakeNamespaceObject);
  1209. const map = new Map();
  1210. map.set(
  1211. key,
  1212. `/* reexport fake namespace object from non-harmony */ ${name}_namespace_cache || (${name}_namespace_cache = ${
  1213. RuntimeGlobals.createFakeNamespaceObject
  1214. }(${name}${fakeType ? `, ${fakeType}` : ""}))`
  1215. );
  1216. return [
  1217. new InitFragment(
  1218. `var ${name}_namespace_cache;\n`,
  1219. InitFragment.STAGE_CONSTANTS,
  1220. -1,
  1221. `${name}_namespace_cache`
  1222. ),
  1223. new HarmonyExportInitFragment(module.exportsArgument, map)
  1224. ];
  1225. }
  1226. /**
  1227. * @param {Module} module module
  1228. * @param {ChunkGraph} chunkGraph chunkGraph
  1229. * @param {string | false | string[]} key key
  1230. * @param {string} name name
  1231. * @param {import("../Module").ExportsType} exportsType exportsType
  1232. * @param {Set<string>} runtimeRequirements runtimeRequirements
  1233. * @returns {InitFragment<GenerateContext>[]} fragments
  1234. */
  1235. getReexportDeferredNamespaceObjectFragments(
  1236. module,
  1237. chunkGraph,
  1238. key,
  1239. name,
  1240. exportsType,
  1241. runtimeRequirements
  1242. ) {
  1243. runtimeRequirements.add(RuntimeGlobals.exports);
  1244. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1245. runtimeRequirements.add(RuntimeGlobals.makeDeferredNamespaceObject);
  1246. const map = new Map();
  1247. const moduleId = JSON.stringify(chunkGraph.getModuleId(module));
  1248. const mode = getMakeDeferredNamespaceModeFromExportsType(exportsType);
  1249. map.set(
  1250. key,
  1251. `/* reexport deferred namespace object */ ${name}_deferred_namespace_cache || (${name}_deferred_namespace_cache = ${RuntimeGlobals.makeDeferredNamespaceObject}(${moduleId}, ${mode}))`
  1252. );
  1253. return [
  1254. new InitFragment(
  1255. `var ${name}_deferred_namespace_cache;\n`,
  1256. InitFragment.STAGE_CONSTANTS,
  1257. -1,
  1258. `${name}_deferred_namespace_cache`
  1259. ),
  1260. new HarmonyExportInitFragment(module.exportsArgument, map)
  1261. ];
  1262. }
  1263. /**
  1264. * @param {Module} module module
  1265. * @param {string} key key
  1266. * @param {string} name name
  1267. * @param {string | string[] | false} valueKey value key
  1268. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  1269. * @returns {string} result
  1270. */
  1271. getConditionalReexportStatement(
  1272. module,
  1273. key,
  1274. name,
  1275. valueKey,
  1276. runtimeRequirements
  1277. ) {
  1278. if (valueKey === false) {
  1279. return "/* unused export */\n";
  1280. }
  1281. const exportsName = module.exportsArgument;
  1282. const returnValue = this.getReturnValue(name, valueKey);
  1283. runtimeRequirements.add(RuntimeGlobals.exports);
  1284. runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
  1285. runtimeRequirements.add(RuntimeGlobals.hasOwnProperty);
  1286. return `if(${RuntimeGlobals.hasOwnProperty}(${name}, ${JSON.stringify(
  1287. valueKey[0]
  1288. )})) ${
  1289. RuntimeGlobals.definePropertyGetters
  1290. }(${exportsName}, { ${propertyName(
  1291. key
  1292. )}: function() { return ${returnValue}; } });\n`;
  1293. }
  1294. /**
  1295. * @param {string} name name
  1296. * @param {null | false | string | string[]} valueKey value key
  1297. * @returns {string | undefined} value
  1298. */
  1299. getReturnValue(name, valueKey) {
  1300. if (valueKey === null) {
  1301. return `${name}_default.a`;
  1302. }
  1303. if (valueKey === "") {
  1304. return name;
  1305. }
  1306. if (valueKey === false) {
  1307. return "/* unused export */ undefined";
  1308. }
  1309. return `${name}${propertyAccess(valueKey)}`;
  1310. }
  1311. };
  1312. class HarmonyStarExportsList {
  1313. constructor() {
  1314. /** @type {HarmonyExportImportedSpecifierDependency[]} */
  1315. this.dependencies = [];
  1316. }
  1317. /**
  1318. * @param {HarmonyExportImportedSpecifierDependency} dep dependency
  1319. * @returns {void}
  1320. */
  1321. push(dep) {
  1322. this.dependencies.push(dep);
  1323. }
  1324. slice() {
  1325. return [...this.dependencies];
  1326. }
  1327. /**
  1328. * @param {ObjectSerializerContext} context context
  1329. */
  1330. serialize({ write, setCircularReference }) {
  1331. setCircularReference(this);
  1332. write(this.dependencies);
  1333. }
  1334. /**
  1335. * @param {ObjectDeserializerContext} context context
  1336. */
  1337. deserialize({ read, setCircularReference }) {
  1338. setCircularReference(this);
  1339. this.dependencies = read();
  1340. }
  1341. }
  1342. makeSerializable(
  1343. HarmonyStarExportsList,
  1344. "webpack/lib/dependencies/HarmonyExportImportedSpecifierDependency",
  1345. "HarmonyStarExportsList"
  1346. );
  1347. module.exports.HarmonyStarExportsList = HarmonyStarExportsList;