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.

109 lines
2.7 KiB

4 years ago
  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. };