HarmonyImportSpecifierDependency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NullDependency = require("./NullDependency");
  7. class HarmonyImportSpecifierDependency extends NullDependency {
  8. constructor(importDependency, importedVar, id, name, range, strictExportPresence) {
  9. super();
  10. this.importDependency = importDependency;
  11. this.importedVar = importedVar;
  12. this.id = id;
  13. this.name = name;
  14. this.range = range;
  15. this.strictExportPresence = strictExportPresence;
  16. }
  17. get type() {
  18. return "harmony import specifier";
  19. }
  20. getReference() {
  21. if(!this.importDependency.module) return null;
  22. return {
  23. module: this.importDependency.module,
  24. importedNames: this.id ? [this.id] : true
  25. };
  26. }
  27. getWarnings() {
  28. if(this.strictExportPresence) {
  29. return [];
  30. }
  31. return this._getErrors();
  32. }
  33. getErrors() {
  34. if(this.strictExportPresence) {
  35. return this._getErrors();
  36. }
  37. return [];
  38. }
  39. _getErrors() {
  40. const importedModule = this.importDependency.module;
  41. if(!importedModule || !importedModule.meta || !importedModule.meta.harmonyModule) {
  42. return;
  43. }
  44. if(!this.id) {
  45. return;
  46. }
  47. if(importedModule.isProvided(this.id) !== false) {
  48. return;
  49. }
  50. const idIsNotNameMessage = this.id !== this.name ? ` (imported as '${this.name}')` : "";
  51. const errorMessage = `"export '${this.id}'${idIsNotNameMessage} was not found in '${this.importDependency.userRequest}'`;
  52. const err = new Error(errorMessage);
  53. err.hideStack = true;
  54. return [err];
  55. }
  56. updateHash(hash) {
  57. super.updateHash(hash);
  58. const importedModule = this.importDependency.module;
  59. hash.update((importedModule && importedModule.id) + "");
  60. hash.update((importedModule && this.id) + "");
  61. hash.update((importedModule && this.importedVar) + "");
  62. hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
  63. hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
  64. hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
  65. }
  66. }
  67. HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate {
  68. apply(dep, source) {
  69. const content = this.getContent(dep);
  70. source.replace(dep.range[0], dep.range[1] - 1, content);
  71. }
  72. getContent(dep) {
  73. const importedModule = dep.importDependency.module;
  74. const defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
  75. const shortHandPrefix = this.getShortHandPrefix(dep);
  76. const importedVar = dep.importedVar;
  77. const importedVarSuffix = this.getImportVarSuffix(dep, defaultImport, importedModule);
  78. if(dep.call && defaultImport) {
  79. return `${shortHandPrefix}${importedVar}_default()`;
  80. }
  81. if(dep.call && dep.id) {
  82. return `${shortHandPrefix}__webpack_require__.i(${importedVar}${importedVarSuffix})`;
  83. }
  84. return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
  85. }
  86. getImportVarSuffix(dep, defaultImport, importedModule) {
  87. if(defaultImport) {
  88. return "_default.a";
  89. }
  90. if(dep.id) {
  91. const used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
  92. const optionalComment = dep.id !== used ? " /* " + dep.id + " */" : "";
  93. return `[${JSON.stringify(used)}${optionalComment}]`;
  94. }
  95. return "";
  96. }
  97. getShortHandPrefix(dep) {
  98. if(!dep.shorthand) {
  99. return "";
  100. }
  101. return dep.name + ": ";
  102. }
  103. };
  104. module.exports = HarmonyImportSpecifierDependency;