HarmonyExportImportedSpecifierDependency.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependencyReference = require("./DependencyReference");
  7. const HarmonyImportDependency = require("./HarmonyImportDependency");
  8. const Template = require("../Template");
  9. const HarmonyLinkingError = require("../HarmonyLinkingError");
  10. /** @typedef {import("../Module")} Module */
  11. /** @typedef {"missing"|"unused"|"empty-star"|"reexport-non-harmony-default"|"reexport-named-default"|"reexport-namespace-object"|"reexport-non-harmony-default-strict"|"reexport-fake-namespace-object"|"rexport-non-harmony-undefined"|"safe-reexport"|"checked-reexport"|"dynamic-reexport"} ExportModeType */
  12. /** @type {Map<string, string>} */
  13. const EMPTY_MAP = new Map();
  14. class ExportMode {
  15. /**
  16. * @param {ExportModeType} type type of the mode
  17. */
  18. constructor(type) {
  19. /** @type {ExportModeType} */
  20. this.type = type;
  21. /** @type {string|null} */
  22. this.name = null;
  23. /** @type {Map<string, string>} */
  24. this.map = EMPTY_MAP;
  25. /** @type {Module|null} */
  26. this.module = null;
  27. /** @type {string|null} */
  28. this.userRequest = null;
  29. }
  30. }
  31. const EMPTY_STAR_MODE = new ExportMode("empty-star");
  32. class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
  33. constructor(
  34. request,
  35. originModule,
  36. sourceOrder,
  37. parserScope,
  38. id,
  39. name,
  40. activeExports,
  41. otherStarExports,
  42. strictExportPresence
  43. ) {
  44. super(request, originModule, sourceOrder, parserScope);
  45. this.id = id;
  46. this.redirectedId = undefined;
  47. this.name = name;
  48. this.activeExports = activeExports;
  49. this.otherStarExports = otherStarExports;
  50. this.strictExportPresence = strictExportPresence;
  51. }
  52. get type() {
  53. return "harmony export imported specifier";
  54. }
  55. get _id() {
  56. return this.redirectedId || this.id;
  57. }
  58. getMode(ignoreUnused) {
  59. const name = this.name;
  60. const id = this._id;
  61. const used = this.originModule.isUsed(name);
  62. const importedModule = this._module;
  63. if (!importedModule) {
  64. const mode = new ExportMode("missing");
  65. mode.userRequest = this.userRequest;
  66. return mode;
  67. }
  68. if (
  69. !ignoreUnused &&
  70. (name ? !used : this.originModule.usedExports === false)
  71. ) {
  72. const mode = new ExportMode("unused");
  73. mode.name = name || "*";
  74. return mode;
  75. }
  76. const strictHarmonyModule = this.originModule.buildMeta.strictHarmonyModule;
  77. if (name && id === "default" && importedModule.buildMeta) {
  78. if (!importedModule.buildMeta.exportsType) {
  79. const mode = new ExportMode(
  80. strictHarmonyModule
  81. ? "reexport-non-harmony-default-strict"
  82. : "reexport-non-harmony-default"
  83. );
  84. mode.name = name;
  85. mode.module = importedModule;
  86. return mode;
  87. } else if (importedModule.buildMeta.exportsType === "named") {
  88. const mode = new ExportMode("reexport-named-default");
  89. mode.name = name;
  90. mode.module = importedModule;
  91. return mode;
  92. }
  93. }
  94. const isNotAHarmonyModule =
  95. importedModule.buildMeta && !importedModule.buildMeta.exportsType;
  96. if (name) {
  97. let mode;
  98. if (id) {
  99. // export { name as name }
  100. if (isNotAHarmonyModule && strictHarmonyModule) {
  101. mode = new ExportMode("rexport-non-harmony-undefined");
  102. mode.name = name;
  103. } else {
  104. mode = new ExportMode("safe-reexport");
  105. mode.map = new Map([[name, id]]);
  106. }
  107. } else {
  108. // export { * as name }
  109. if (isNotAHarmonyModule && strictHarmonyModule) {
  110. mode = new ExportMode("reexport-fake-namespace-object");
  111. mode.name = name;
  112. } else {
  113. mode = new ExportMode("reexport-namespace-object");
  114. mode.name = name;
  115. }
  116. }
  117. mode.module = importedModule;
  118. return mode;
  119. }
  120. const hasUsedExports = Array.isArray(this.originModule.usedExports);
  121. const hasProvidedExports = Array.isArray(
  122. importedModule.buildMeta.providedExports
  123. );
  124. const activeFromOtherStarExports = this._discoverActiveExportsFromOtherStartExports();
  125. // export *
  126. if (hasUsedExports) {
  127. // reexport * with known used exports
  128. if (hasProvidedExports) {
  129. const map = new Map(
  130. this.originModule.usedExports
  131. .filter(id => {
  132. if (id === "default") return false;
  133. if (this.activeExports.has(id)) return false;
  134. if (activeFromOtherStarExports.has(id)) return false;
  135. if (!importedModule.buildMeta.providedExports.includes(id))
  136. return false;
  137. return true;
  138. })
  139. .map(item => [item, item])
  140. );
  141. if (map.size === 0) {
  142. return EMPTY_STAR_MODE;
  143. }
  144. const mode = new ExportMode("safe-reexport");
  145. mode.module = importedModule;
  146. mode.map = map;
  147. return mode;
  148. }
  149. const map = new Map(
  150. this.originModule.usedExports
  151. .filter(id => {
  152. if (id === "default") return false;
  153. if (this.activeExports.has(id)) return false;
  154. if (activeFromOtherStarExports.has(id)) return false;
  155. return true;
  156. })
  157. .map(item => [item, item])
  158. );
  159. if (map.size === 0) {
  160. return EMPTY_STAR_MODE;
  161. }
  162. const mode = new ExportMode("checked-reexport");
  163. mode.module = importedModule;
  164. mode.map = map;
  165. return mode;
  166. }
  167. if (hasProvidedExports) {
  168. const map = new Map(
  169. importedModule.buildMeta.providedExports
  170. .filter(id => {
  171. if (id === "default") return false;
  172. if (this.activeExports.has(id)) return false;
  173. if (activeFromOtherStarExports.has(id)) return false;
  174. return true;
  175. })
  176. .map(item => [item, item])
  177. );
  178. if (map.size === 0) {
  179. return EMPTY_STAR_MODE;
  180. }
  181. const mode = new ExportMode("safe-reexport");
  182. mode.module = importedModule;
  183. mode.map = map;
  184. return mode;
  185. }
  186. const mode = new ExportMode("dynamic-reexport");
  187. mode.module = importedModule;
  188. return mode;
  189. }
  190. getReference() {
  191. const mode = this.getMode(false);
  192. switch (mode.type) {
  193. case "missing":
  194. case "unused":
  195. case "empty-star":
  196. return null;
  197. case "reexport-non-harmony-default":
  198. case "reexport-named-default":
  199. return new DependencyReference(
  200. mode.module,
  201. ["default"],
  202. false,
  203. this.sourceOrder
  204. );
  205. case "reexport-namespace-object":
  206. case "reexport-non-harmony-default-strict":
  207. case "reexport-fake-namespace-object":
  208. case "rexport-non-harmony-undefined":
  209. return new DependencyReference(
  210. mode.module,
  211. true,
  212. false,
  213. this.sourceOrder
  214. );
  215. case "safe-reexport":
  216. case "checked-reexport":
  217. return new DependencyReference(
  218. mode.module,
  219. Array.from(mode.map.values()),
  220. false,
  221. this.sourceOrder
  222. );
  223. case "dynamic-reexport":
  224. return new DependencyReference(
  225. mode.module,
  226. true,
  227. false,
  228. this.sourceOrder
  229. );
  230. default:
  231. throw new Error(`Unknown mode ${mode.type}`);
  232. }
  233. }
  234. _discoverActiveExportsFromOtherStartExports() {
  235. if (!this.otherStarExports) return new Set();
  236. const result = new Set();
  237. // try to learn impossible exports from other star exports with provided exports
  238. for (const otherStarExport of this.otherStarExports) {
  239. const otherImportedModule = otherStarExport._module;
  240. if (
  241. otherImportedModule &&
  242. Array.isArray(otherImportedModule.buildMeta.providedExports)
  243. ) {
  244. for (const exportName of otherImportedModule.buildMeta
  245. .providedExports) {
  246. result.add(exportName);
  247. }
  248. }
  249. }
  250. return result;
  251. }
  252. getExports() {
  253. if (this.name) {
  254. return {
  255. exports: [this.name],
  256. dependencies: undefined
  257. };
  258. }
  259. const importedModule = this._module;
  260. if (!importedModule) {
  261. // no imported module available
  262. return {
  263. exports: null,
  264. dependencies: undefined
  265. };
  266. }
  267. if (Array.isArray(importedModule.buildMeta.providedExports)) {
  268. const activeFromOtherStarExports = this._discoverActiveExportsFromOtherStartExports();
  269. return {
  270. exports: importedModule.buildMeta.providedExports.filter(
  271. id =>
  272. id !== "default" &&
  273. !activeFromOtherStarExports.has(id) &&
  274. !this.activeExports.has(id)
  275. ),
  276. dependencies: [importedModule]
  277. };
  278. }
  279. if (importedModule.buildMeta.providedExports) {
  280. return {
  281. exports: true,
  282. dependencies: undefined
  283. };
  284. }
  285. return {
  286. exports: null,
  287. dependencies: [importedModule]
  288. };
  289. }
  290. getWarnings() {
  291. if (
  292. this.strictExportPresence ||
  293. this.originModule.buildMeta.strictHarmonyModule
  294. ) {
  295. return [];
  296. }
  297. return this._getErrors();
  298. }
  299. getErrors() {
  300. if (
  301. this.strictExportPresence ||
  302. this.originModule.buildMeta.strictHarmonyModule
  303. ) {
  304. return this._getErrors();
  305. }
  306. return [];
  307. }
  308. _getErrors() {
  309. const importedModule = this._module;
  310. if (!importedModule) {
  311. return;
  312. }
  313. if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
  314. // It's not an harmony module
  315. if (
  316. this.originModule.buildMeta.strictHarmonyModule &&
  317. this._id &&
  318. this._id !== "default"
  319. ) {
  320. // In strict harmony modules we only support the default export
  321. return [
  322. new HarmonyLinkingError(
  323. `Can't reexport the named export '${this._id}' from non EcmaScript module (only default export is available)`
  324. )
  325. ];
  326. }
  327. return;
  328. }
  329. if (!this._id) {
  330. return;
  331. }
  332. if (importedModule.isProvided(this._id) !== false) {
  333. // It's provided or we are not sure
  334. return;
  335. }
  336. // We are sure that it's not provided
  337. const idIsNotNameMessage =
  338. this._id !== this.name ? ` (reexported as '${this.name}')` : "";
  339. const errorMessage = `"export '${this._id}'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
  340. return [new HarmonyLinkingError(errorMessage)];
  341. }
  342. updateHash(hash) {
  343. super.updateHash(hash);
  344. const hashValue = this.getHashValue(this._module);
  345. hash.update(hashValue);
  346. }
  347. getHashValue(importedModule) {
  348. if (!importedModule) {
  349. return "";
  350. }
  351. const stringifiedUsedExport = JSON.stringify(importedModule.usedExports);
  352. const stringifiedProvidedExport = JSON.stringify(
  353. importedModule.buildMeta.providedExports
  354. );
  355. return (
  356. importedModule.used + stringifiedUsedExport + stringifiedProvidedExport
  357. );
  358. }
  359. disconnect() {
  360. super.disconnect();
  361. this.redirectedId = undefined;
  362. }
  363. }
  364. module.exports = HarmonyExportImportedSpecifierDependency;
  365. HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
  366. harmonyInit(dep, source, runtime, dependencyTemplates) {
  367. super.harmonyInit(dep, source, runtime, dependencyTemplates);
  368. const content = this.getContent(dep);
  369. source.insert(-1, content);
  370. }
  371. getHarmonyInitOrder(dep) {
  372. if (dep.name) {
  373. const used = dep.originModule.isUsed(dep.name);
  374. if (!used) return NaN;
  375. } else {
  376. const importedModule = dep._module;
  377. const activeFromOtherStarExports = dep._discoverActiveExportsFromOtherStartExports();
  378. if (Array.isArray(dep.originModule.usedExports)) {
  379. // we know which exports are used
  380. const unused = dep.originModule.usedExports.every(id => {
  381. if (id === "default") return true;
  382. if (dep.activeExports.has(id)) return true;
  383. if (importedModule.isProvided(id) === false) return true;
  384. if (activeFromOtherStarExports.has(id)) return true;
  385. return false;
  386. });
  387. if (unused) return NaN;
  388. } else if (
  389. dep.originModule.usedExports &&
  390. importedModule &&
  391. Array.isArray(importedModule.buildMeta.providedExports)
  392. ) {
  393. // not sure which exports are used, but we know which are provided
  394. const unused = importedModule.buildMeta.providedExports.every(id => {
  395. if (id === "default") return true;
  396. if (dep.activeExports.has(id)) return true;
  397. if (activeFromOtherStarExports.has(id)) return true;
  398. return false;
  399. });
  400. if (unused) return NaN;
  401. }
  402. }
  403. return super.getHarmonyInitOrder(dep);
  404. }
  405. getContent(dep) {
  406. const mode = dep.getMode(false);
  407. const module = dep.originModule;
  408. const importedModule = dep._module;
  409. const importVar = dep.getImportVar();
  410. switch (mode.type) {
  411. case "missing":
  412. return `throw new Error(${JSON.stringify(
  413. `Cannot find module '${mode.userRequest}'`
  414. )});\n`;
  415. case "unused":
  416. return `${Template.toNormalComment(
  417. `unused harmony reexport ${mode.name}`
  418. )}\n`;
  419. case "reexport-non-harmony-default":
  420. return (
  421. "/* harmony reexport (default from non-harmony) */ " +
  422. this.getReexportStatement(
  423. module,
  424. module.isUsed(mode.name),
  425. importVar,
  426. null
  427. )
  428. );
  429. case "reexport-named-default":
  430. return (
  431. "/* harmony reexport (default from named exports) */ " +
  432. this.getReexportStatement(
  433. module,
  434. module.isUsed(mode.name),
  435. importVar,
  436. ""
  437. )
  438. );
  439. case "reexport-fake-namespace-object":
  440. return (
  441. "/* harmony reexport (fake namespace object from non-harmony) */ " +
  442. this.getReexportFakeNamespaceObjectStatement(
  443. module,
  444. module.isUsed(mode.name),
  445. importVar
  446. )
  447. );
  448. case "rexport-non-harmony-undefined":
  449. return (
  450. "/* harmony reexport (non default export from non-harmony) */ " +
  451. this.getReexportStatement(
  452. module,
  453. module.isUsed(mode.name),
  454. "undefined",
  455. ""
  456. )
  457. );
  458. case "reexport-non-harmony-default-strict":
  459. return (
  460. "/* harmony reexport (default from non-harmony) */ " +
  461. this.getReexportStatement(
  462. module,
  463. module.isUsed(mode.name),
  464. importVar,
  465. ""
  466. )
  467. );
  468. case "reexport-namespace-object":
  469. return (
  470. "/* harmony reexport (module object) */ " +
  471. this.getReexportStatement(
  472. module,
  473. module.isUsed(mode.name),
  474. importVar,
  475. ""
  476. )
  477. );
  478. case "empty-star":
  479. return "/* empty/unused harmony star reexport */";
  480. case "safe-reexport":
  481. return Array.from(mode.map.entries())
  482. .map(item => {
  483. return (
  484. "/* harmony reexport (safe) */ " +
  485. this.getReexportStatement(
  486. module,
  487. module.isUsed(item[0]),
  488. importVar,
  489. importedModule.isUsed(item[1])
  490. ) +
  491. "\n"
  492. );
  493. })
  494. .join("");
  495. case "checked-reexport":
  496. return Array.from(mode.map.entries())
  497. .map(item => {
  498. return (
  499. "/* harmony reexport (checked) */ " +
  500. this.getConditionalReexportStatement(
  501. module,
  502. item[0],
  503. importVar,
  504. item[1]
  505. ) +
  506. "\n"
  507. );
  508. })
  509. .join("");
  510. case "dynamic-reexport": {
  511. const activeExports = new Set([
  512. ...dep.activeExports,
  513. ...dep._discoverActiveExportsFromOtherStartExports()
  514. ]);
  515. let content =
  516. "/* harmony reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in " +
  517. importVar +
  518. ") ";
  519. // Filter out exports which are defined by other exports
  520. // and filter out default export because it cannot be reexported with *
  521. if (activeExports.size > 0) {
  522. content +=
  523. "if(" +
  524. JSON.stringify(Array.from(activeExports).concat("default")) +
  525. ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
  526. } else {
  527. content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
  528. }
  529. const exportsName = dep.originModule.exportsArgument;
  530. return (
  531. content +
  532. `(function(key) { __webpack_require__.d(${exportsName}, key, function() { return ${importVar}[key]; }) }(__WEBPACK_IMPORT_KEY__));\n`
  533. );
  534. }
  535. default:
  536. throw new Error(`Unknown mode ${mode.type}`);
  537. }
  538. }
  539. getReexportStatement(module, key, name, valueKey) {
  540. const exportsName = module.exportsArgument;
  541. const returnValue = this.getReturnValue(name, valueKey);
  542. return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
  543. key
  544. )}, function() { return ${returnValue}; });\n`;
  545. }
  546. getReexportFakeNamespaceObjectStatement(module, key, name) {
  547. const exportsName = module.exportsArgument;
  548. return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
  549. key
  550. )}, function() { return __webpack_require__.t(${name}); });\n`;
  551. }
  552. getConditionalReexportStatement(module, key, name, valueKey) {
  553. if (valueKey === false) {
  554. return "/* unused export */\n";
  555. }
  556. const exportsName = module.exportsArgument;
  557. const returnValue = this.getReturnValue(name, valueKey);
  558. return `if(__webpack_require__.o(${name}, ${JSON.stringify(
  559. valueKey
  560. )})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
  561. key
  562. )}, function() { return ${returnValue}; });\n`;
  563. }
  564. getReturnValue(name, valueKey) {
  565. if (valueKey === null) {
  566. return `${name}_default.a`;
  567. }
  568. if (valueKey === "") {
  569. return name;
  570. }
  571. if (valueKey === false) {
  572. return "/* unused export */ undefined";
  573. }
  574. return `${name}[${JSON.stringify(valueKey)}]`;
  575. }
  576. };