Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

157 рядки
4.6 KiB

  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Module")} Module */
  7. const MODULE_REFERENCE_REGEXP =
  8. /^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(?:_asiSafe(\d))?__$/;
  9. const DEFAULT_EXPORT = "__WEBPACK_DEFAULT_EXPORT__";
  10. const NAMESPACE_OBJECT_EXPORT = "__WEBPACK_NAMESPACE_OBJECT__";
  11. /**
  12. * @typedef {object} ExternalModuleInfo
  13. * @property {number} index
  14. * @property {Module} module
  15. */
  16. /**
  17. * @typedef {object} ConcatenatedModuleInfo
  18. * @property {number} index
  19. * @property {Module} module
  20. * @property {Map<string, string>} exportMap mapping from export name to symbol
  21. * @property {Map<string, string>} rawExportMap mapping from export name to symbol
  22. * @property {string=} namespaceExportSymbol
  23. */
  24. /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo} ModuleInfo */
  25. /**
  26. * @typedef {object} ModuleReferenceOptions
  27. * @property {string[]} ids the properties/exports of the module
  28. * @property {boolean} call true, when this referenced export is called
  29. * @property {boolean} directImport true, when this referenced export is directly imported (not via property access)
  30. * @property {boolean | undefined} asiSafe if the position is ASI safe or unknown
  31. */
  32. class ConcatenationScope {
  33. /**
  34. * @param {ModuleInfo[] | Map<Module, ModuleInfo>} modulesMap all module info by module
  35. * @param {ConcatenatedModuleInfo} currentModule the current module info
  36. */
  37. constructor(modulesMap, currentModule) {
  38. this._currentModule = currentModule;
  39. if (Array.isArray(modulesMap)) {
  40. const map = new Map();
  41. for (const info of modulesMap) {
  42. map.set(info.module, info);
  43. }
  44. modulesMap = map;
  45. }
  46. this._modulesMap = modulesMap;
  47. }
  48. /**
  49. * @param {Module} module the referenced module
  50. * @returns {boolean} true, when it's in the scope
  51. */
  52. isModuleInScope(module) {
  53. return this._modulesMap.has(module);
  54. }
  55. /**
  56. * @param {string} exportName name of the export
  57. * @param {string} symbol identifier of the export in source code
  58. */
  59. registerExport(exportName, symbol) {
  60. if (!this._currentModule.exportMap) {
  61. this._currentModule.exportMap = new Map();
  62. }
  63. if (!this._currentModule.exportMap.has(exportName)) {
  64. this._currentModule.exportMap.set(exportName, symbol);
  65. }
  66. }
  67. /**
  68. * @param {string} exportName name of the export
  69. * @param {string} expression expression to be used
  70. */
  71. registerRawExport(exportName, expression) {
  72. if (!this._currentModule.rawExportMap) {
  73. this._currentModule.rawExportMap = new Map();
  74. }
  75. if (!this._currentModule.rawExportMap.has(exportName)) {
  76. this._currentModule.rawExportMap.set(exportName, expression);
  77. }
  78. }
  79. /**
  80. * @param {string} symbol identifier of the export in source code
  81. */
  82. registerNamespaceExport(symbol) {
  83. this._currentModule.namespaceExportSymbol = symbol;
  84. }
  85. /**
  86. * @param {Module} module the referenced module
  87. * @param {Partial<ModuleReferenceOptions>} options options
  88. * @returns {string} the reference as identifier
  89. */
  90. createModuleReference(
  91. module,
  92. { ids = undefined, call = false, directImport = false, asiSafe = false }
  93. ) {
  94. const info = /** @type {ModuleInfo} */ (this._modulesMap.get(module));
  95. const callFlag = call ? "_call" : "";
  96. const directImportFlag = directImport ? "_directImport" : "";
  97. const asiSafeFlag = asiSafe
  98. ? "_asiSafe1"
  99. : asiSafe === false
  100. ? "_asiSafe0"
  101. : "";
  102. const exportData = ids
  103. ? Buffer.from(JSON.stringify(ids), "utf-8").toString("hex")
  104. : "ns";
  105. // a "._" is appended to allow "delete ...", which would cause a SyntaxError in strict mode
  106. return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${asiSafeFlag}__._`;
  107. }
  108. /**
  109. * @param {string} name the identifier
  110. * @returns {boolean} true, when it's an module reference
  111. */
  112. static isModuleReference(name) {
  113. return MODULE_REFERENCE_REGEXP.test(name);
  114. }
  115. /**
  116. * @param {string} name the identifier
  117. * @returns {ModuleReferenceOptions & { index: number } | null} parsed options and index
  118. */
  119. static matchModuleReference(name) {
  120. const match = MODULE_REFERENCE_REGEXP.exec(name);
  121. if (!match) return null;
  122. const index = Number(match[1]);
  123. const asiSafe = match[5];
  124. return {
  125. index,
  126. ids:
  127. match[2] === "ns"
  128. ? []
  129. : JSON.parse(Buffer.from(match[2], "hex").toString("utf-8")),
  130. call: Boolean(match[3]),
  131. directImport: Boolean(match[4]),
  132. asiSafe: asiSafe ? asiSafe === "1" : undefined
  133. };
  134. }
  135. }
  136. ConcatenationScope.DEFAULT_EXPORT = DEFAULT_EXPORT;
  137. ConcatenationScope.NAMESPACE_OBJECT_EXPORT = NAMESPACE_OBJECT_EXPORT;
  138. module.exports = ConcatenationScope;