HarmonyExportImportedSpecifierDependency.js 16 KB

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