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=} 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;