/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js

https://bitbucket.org/jorgeh1218/empresas-web · JavaScript · 156 lines · 137 code · 11 blank · 8 comment · 10 complexity · 94a4c837eaa968cda601075c15feb451 MD5 · raw file

  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 HarmonyLinkingError = require("../HarmonyLinkingError");
  9. class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
  10. constructor(
  11. request,
  12. originModule,
  13. sourceOrder,
  14. parserScope,
  15. id,
  16. name,
  17. range,
  18. strictExportPresence
  19. ) {
  20. super(request, originModule, sourceOrder, parserScope);
  21. this.id = id === null ? null : `${id}`;
  22. this.name = name;
  23. this.range = range;
  24. this.strictExportPresence = strictExportPresence;
  25. this.namespaceObjectAsContext = false;
  26. this.callArgs = undefined;
  27. this.call = undefined;
  28. this.directImport = undefined;
  29. this.shorthand = undefined;
  30. }
  31. get type() {
  32. return "harmony import specifier";
  33. }
  34. getReference() {
  35. if (!this.module) return null;
  36. return new DependencyReference(
  37. this.module,
  38. this.id && !this.namespaceObjectAsContext ? [this.id] : true,
  39. false
  40. );
  41. }
  42. getWarnings() {
  43. if (
  44. this.strictExportPresence ||
  45. this.originModule.buildMeta.strictHarmonyModule
  46. ) {
  47. return [];
  48. }
  49. return this._getErrors();
  50. }
  51. getErrors() {
  52. if (
  53. this.strictExportPresence ||
  54. this.originModule.buildMeta.strictHarmonyModule
  55. ) {
  56. return this._getErrors();
  57. }
  58. return [];
  59. }
  60. _getErrors() {
  61. const importedModule = this.module;
  62. if (!importedModule) {
  63. return;
  64. }
  65. if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
  66. // It's not an harmony module
  67. if (
  68. this.originModule.buildMeta.strictHarmonyModule &&
  69. this.id !== "default"
  70. ) {
  71. // In strict harmony modules we only support the default export
  72. const exportName = this.id
  73. ? `the named export '${this.id}'`
  74. : "the namespace object";
  75. return [
  76. new HarmonyLinkingError(
  77. `Can't import ${exportName} from non EcmaScript module (only default export is available)`
  78. )
  79. ];
  80. }
  81. return;
  82. }
  83. if (!this.id) {
  84. return;
  85. }
  86. if (importedModule.isProvided(this.id) !== false) {
  87. // It's provided or we are not sure
  88. return;
  89. }
  90. // We are sure that it's not provided
  91. const idIsNotNameMessage =
  92. this.id !== this.name ? ` (imported as '${this.name}')` : "";
  93. const errorMessage = `"export '${
  94. this.id
  95. }'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
  96. return [new HarmonyLinkingError(errorMessage)];
  97. }
  98. // implement this method to allow the occurrence order plugin to count correctly
  99. getNumberOfIdOccurrences() {
  100. return 0;
  101. }
  102. updateHash(hash) {
  103. super.updateHash(hash);
  104. const importedModule = this.module;
  105. hash.update((importedModule && this.id) + "");
  106. hash.update(
  107. (importedModule && this.id && importedModule.isUsed(this.id)) + ""
  108. );
  109. hash.update(
  110. (importedModule &&
  111. (!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
  112. ""
  113. );
  114. hash.update(
  115. (importedModule &&
  116. importedModule.used + JSON.stringify(importedModule.usedExports)) + ""
  117. );
  118. }
  119. }
  120. HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
  121. apply(dep, source, runtime) {
  122. super.apply(dep, source, runtime);
  123. const content = this.getContent(dep, runtime);
  124. source.replace(dep.range[0], dep.range[1] - 1, content);
  125. }
  126. getContent(dep, runtime) {
  127. const exportExpr = runtime.exportFromImport({
  128. module: dep.module,
  129. request: dep.request,
  130. exportName: dep.id,
  131. originModule: dep.originModule,
  132. asiSafe: dep.shorthand,
  133. isCall: dep.call,
  134. callContext: !dep.directImport,
  135. importVar: dep.getImportVar()
  136. });
  137. return dep.shorthand ? `${dep.name}: ${exportExpr}` : exportExpr;
  138. }
  139. };
  140. module.exports = HarmonyImportSpecifierDependency;