You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HarmonyImportDependency.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ModuleDependency = require("./ModuleDependency");
  8. const Template = require("../Template");
  9. class HarmonyImportDependency extends ModuleDependency {
  10. constructor(request, originModule, sourceOrder, parserScope) {
  11. super(request);
  12. this.redirectedModule = undefined;
  13. this.originModule = originModule;
  14. this.sourceOrder = sourceOrder;
  15. this.parserScope = parserScope;
  16. }
  17. get _module() {
  18. return this.redirectedModule || this.module;
  19. }
  20. getReference() {
  21. if (!this._module) return null;
  22. return new DependencyReference(
  23. this._module,
  24. false,
  25. this.weak,
  26. this.sourceOrder
  27. );
  28. }
  29. getImportVar() {
  30. let importVarMap = this.parserScope.importVarMap;
  31. if (!importVarMap) this.parserScope.importVarMap = importVarMap = new Map();
  32. let importVar = importVarMap.get(this._module);
  33. if (importVar) return importVar;
  34. importVar = `${Template.toIdentifier(
  35. `${this.userRequest}`
  36. )}__WEBPACK_IMPORTED_MODULE_${importVarMap.size}__`;
  37. importVarMap.set(this._module, importVar);
  38. return importVar;
  39. }
  40. getImportStatement(update, runtime) {
  41. return runtime.importStatement({
  42. update,
  43. module: this._module,
  44. importVar: this.getImportVar(),
  45. request: this.request,
  46. originModule: this.originModule
  47. });
  48. }
  49. updateHash(hash) {
  50. super.updateHash(hash);
  51. const importedModule = this._module;
  52. hash.update(
  53. (importedModule &&
  54. (!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
  55. ""
  56. );
  57. hash.update((importedModule && importedModule.id) + "");
  58. }
  59. disconnect() {
  60. super.disconnect();
  61. this.redirectedModule = undefined;
  62. }
  63. }
  64. module.exports = HarmonyImportDependency;
  65. const importEmittedMap = new WeakMap();
  66. HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate {
  67. apply(dep, source, runtime) {
  68. // no-op
  69. }
  70. getHarmonyInitOrder(dep) {
  71. return dep.sourceOrder;
  72. }
  73. static isImportEmitted(dep, source) {
  74. let sourceInfo = importEmittedMap.get(source);
  75. if (!sourceInfo) return false;
  76. const key = dep._module || dep.request;
  77. return key && sourceInfo.emittedImports.get(key);
  78. }
  79. harmonyInit(dep, source, runtime, dependencyTemplates) {
  80. let sourceInfo = importEmittedMap.get(source);
  81. if (!sourceInfo) {
  82. importEmittedMap.set(
  83. source,
  84. (sourceInfo = {
  85. emittedImports: new Map()
  86. })
  87. );
  88. }
  89. const key = dep._module || dep.request;
  90. if (key && sourceInfo.emittedImports.get(key)) return;
  91. sourceInfo.emittedImports.set(key, true);
  92. const content = dep.getImportStatement(false, runtime);
  93. source.insert(-1, content);
  94. }
  95. };