PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ahmad.jamal/sally
JavaScript | 321 lines | 224 code | 29 blank | 68 comment | 21 complexity | 8b1f4da9b9e7c7f2ea134c88f530e5f7 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 Dependency = require("../Dependency");
  7. const {
  8. getDependencyUsedByExportsCondition
  9. } = require("../optimize/InnerGraph");
  10. const makeSerializable = require("../util/makeSerializable");
  11. const propertyAccess = require("../util/propertyAccess");
  12. const HarmonyImportDependency = require("./HarmonyImportDependency");
  13. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  14. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  15. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  16. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  17. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  18. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  19. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  20. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  21. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  22. /** @typedef {import("../WebpackError")} WebpackError */
  23. /** @typedef {import("../util/Hash")} Hash */
  24. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  25. const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");
  26. class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
  27. constructor(
  28. request,
  29. sourceOrder,
  30. ids,
  31. name,
  32. range,
  33. strictExportPresence,
  34. assertions
  35. ) {
  36. super(request, sourceOrder, assertions);
  37. this.ids = ids;
  38. this.name = name;
  39. this.range = range;
  40. this.strictExportPresence = strictExportPresence;
  41. this.namespaceObjectAsContext = false;
  42. this.call = undefined;
  43. this.directImport = undefined;
  44. this.shorthand = undefined;
  45. this.asiSafe = undefined;
  46. /** @type {Set<string> | boolean} */
  47. this.usedByExports = undefined;
  48. }
  49. // TODO webpack 6 remove
  50. get id() {
  51. throw new Error("id was renamed to ids and type changed to string[]");
  52. }
  53. // TODO webpack 6 remove
  54. getId() {
  55. throw new Error("id was renamed to ids and type changed to string[]");
  56. }
  57. // TODO webpack 6 remove
  58. setId() {
  59. throw new Error("id was renamed to ids and type changed to string[]");
  60. }
  61. get type() {
  62. return "harmony import specifier";
  63. }
  64. /**
  65. * @param {ModuleGraph} moduleGraph the module graph
  66. * @returns {string[]} the imported ids
  67. */
  68. getIds(moduleGraph) {
  69. const meta = moduleGraph.getMetaIfExisting(this);
  70. if (meta === undefined) return this.ids;
  71. const ids = meta[idsSymbol];
  72. return ids !== undefined ? ids : this.ids;
  73. }
  74. /**
  75. * @param {ModuleGraph} moduleGraph the module graph
  76. * @param {string[]} ids the imported ids
  77. * @returns {void}
  78. */
  79. setIds(moduleGraph, ids) {
  80. moduleGraph.getMeta(this)[idsSymbol] = ids;
  81. }
  82. /**
  83. * @param {ModuleGraph} moduleGraph module graph
  84. * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
  85. */
  86. getCondition(moduleGraph) {
  87. return getDependencyUsedByExportsCondition(
  88. this,
  89. this.usedByExports,
  90. moduleGraph
  91. );
  92. }
  93. /**
  94. * @param {ModuleGraph} moduleGraph the module graph
  95. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  96. */
  97. getModuleEvaluationSideEffectsState(moduleGraph) {
  98. return false;
  99. }
  100. /**
  101. * Returns list of exports referenced by this dependency
  102. * @param {ModuleGraph} moduleGraph module graph
  103. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  104. * @returns {(string[] | ReferencedExport)[]} referenced exports
  105. */
  106. getReferencedExports(moduleGraph, runtime) {
  107. let ids = this.getIds(moduleGraph);
  108. if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
  109. let namespaceObjectAsContext = this.namespaceObjectAsContext;
  110. if (ids[0] === "default") {
  111. const selfModule = moduleGraph.getParentModule(this);
  112. const importedModule = moduleGraph.getModule(this);
  113. switch (
  114. importedModule.getExportsType(
  115. moduleGraph,
  116. selfModule.buildMeta.strictHarmonyModule
  117. )
  118. ) {
  119. case "default-only":
  120. case "default-with-named":
  121. if (ids.length === 1) return Dependency.EXPORTS_OBJECT_REFERENCED;
  122. ids = ids.slice(1);
  123. namespaceObjectAsContext = true;
  124. break;
  125. case "dynamic":
  126. return Dependency.EXPORTS_OBJECT_REFERENCED;
  127. }
  128. }
  129. if (
  130. this.call &&
  131. !this.directImport &&
  132. (namespaceObjectAsContext || ids.length > 1)
  133. ) {
  134. if (ids.length === 1) return Dependency.EXPORTS_OBJECT_REFERENCED;
  135. ids = ids.slice(0, -1);
  136. }
  137. return [ids];
  138. }
  139. /**
  140. * Returns warnings
  141. * @param {ModuleGraph} moduleGraph module graph
  142. * @returns {WebpackError[]} warnings
  143. */
  144. getWarnings(moduleGraph) {
  145. if (
  146. this.strictExportPresence ||
  147. moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
  148. ) {
  149. return null;
  150. }
  151. return this._getErrors(moduleGraph);
  152. }
  153. /**
  154. * Returns errors
  155. * @param {ModuleGraph} moduleGraph module graph
  156. * @returns {WebpackError[]} errors
  157. */
  158. getErrors(moduleGraph) {
  159. if (
  160. this.strictExportPresence ||
  161. moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
  162. ) {
  163. return this._getErrors(moduleGraph);
  164. }
  165. return null;
  166. }
  167. /**
  168. * @param {ModuleGraph} moduleGraph module graph
  169. * @returns {WebpackError[] | undefined} errors
  170. */
  171. _getErrors(moduleGraph) {
  172. const ids = this.getIds(moduleGraph);
  173. return this.getLinkingErrors(
  174. moduleGraph,
  175. ids,
  176. `(imported as '${this.name}')`
  177. );
  178. }
  179. /**
  180. * implement this method to allow the occurrence order plugin to count correctly
  181. * @returns {number} count how often the id is used in this dependency
  182. */
  183. getNumberOfIdOccurrences() {
  184. return 0;
  185. }
  186. serialize(context) {
  187. const { write } = context;
  188. write(this.ids);
  189. write(this.name);
  190. write(this.range);
  191. write(this.strictExportPresence);
  192. write(this.namespaceObjectAsContext);
  193. write(this.call);
  194. write(this.directImport);
  195. write(this.shorthand);
  196. write(this.asiSafe);
  197. write(this.usedByExports);
  198. super.serialize(context);
  199. }
  200. deserialize(context) {
  201. const { read } = context;
  202. this.ids = read();
  203. this.name = read();
  204. this.range = read();
  205. this.strictExportPresence = read();
  206. this.namespaceObjectAsContext = read();
  207. this.call = read();
  208. this.directImport = read();
  209. this.shorthand = read();
  210. this.asiSafe = read();
  211. this.usedByExports = read();
  212. super.deserialize(context);
  213. }
  214. }
  215. makeSerializable(
  216. HarmonyImportSpecifierDependency,
  217. "webpack/lib/dependencies/HarmonyImportSpecifierDependency"
  218. );
  219. HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends (
  220. HarmonyImportDependency.Template
  221. ) {
  222. /**
  223. * @param {Dependency} dependency the dependency for which the template should be applied
  224. * @param {ReplaceSource} source the current replace source which can be modified
  225. * @param {DependencyTemplateContext} templateContext the context object
  226. * @returns {void}
  227. */
  228. apply(dependency, source, templateContext) {
  229. const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
  230. const { moduleGraph, module, runtime, concatenationScope } =
  231. templateContext;
  232. const connection = moduleGraph.getConnection(dep);
  233. // Skip rendering depending when dependency is conditional
  234. if (connection && !connection.isTargetActive(runtime)) return;
  235. const ids = dep.getIds(moduleGraph);
  236. let exportExpr;
  237. if (
  238. connection &&
  239. concatenationScope &&
  240. concatenationScope.isModuleInScope(connection.module)
  241. ) {
  242. if (ids.length === 0) {
  243. exportExpr = concatenationScope.createModuleReference(
  244. connection.module,
  245. {
  246. asiSafe: dep.asiSafe
  247. }
  248. );
  249. } else if (dep.namespaceObjectAsContext && ids.length === 1) {
  250. exportExpr =
  251. concatenationScope.createModuleReference(connection.module, {
  252. asiSafe: dep.asiSafe
  253. }) + propertyAccess(ids);
  254. } else {
  255. exportExpr = concatenationScope.createModuleReference(
  256. connection.module,
  257. {
  258. ids,
  259. call: dep.call,
  260. directImport: dep.directImport,
  261. asiSafe: dep.asiSafe
  262. }
  263. );
  264. }
  265. } else {
  266. super.apply(dependency, source, templateContext);
  267. const { runtimeTemplate, initFragments, runtimeRequirements } =
  268. templateContext;
  269. exportExpr = runtimeTemplate.exportFromImport({
  270. moduleGraph,
  271. module: moduleGraph.getModule(dep),
  272. request: dep.request,
  273. exportName: ids,
  274. originModule: module,
  275. asiSafe: dep.shorthand ? true : dep.asiSafe,
  276. isCall: dep.call,
  277. callContext: !dep.directImport,
  278. defaultInterop: true,
  279. importVar: dep.getImportVar(moduleGraph),
  280. initFragments,
  281. runtime,
  282. runtimeRequirements
  283. });
  284. }
  285. if (dep.shorthand) {
  286. source.insert(dep.range[1], `: ${exportExpr}`);
  287. } else {
  288. source.replace(dep.range[0], dep.range[1] - 1, exportExpr);
  289. }
  290. }
  291. };
  292. module.exports = HarmonyImportSpecifierDependency;