JsonExportsDependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  9. /** @typedef {import("../Dependency").ExportSpec} ExportSpec */
  10. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  13. /** @typedef {import("../json/JsonData")} JsonData */
  14. /** @typedef {import("../json/JsonData").JsonValue} JsonValue */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. /**
  19. * @callback GetExportsFromDataFn
  20. * @param {JsonValue} data raw json data
  21. * @param {number} [curDepth] current depth
  22. * @returns {ExportSpec[] | null} export spec or nothing
  23. */
  24. /**
  25. * @param {number} exportsDepth exportsDepth
  26. * @returns {GetExportsFromDataFn} value
  27. */
  28. const getExportsWithDepth = exportsDepth =>
  29. /** @type {GetExportsFromDataFn} */
  30. function getExportsFromData(data, curDepth = 1) {
  31. if (curDepth > exportsDepth) {
  32. return null;
  33. }
  34. if (data && typeof data === "object") {
  35. if (Array.isArray(data)) {
  36. return data.length < 100
  37. ? data.map((item, idx) => ({
  38. name: `${idx}`,
  39. canMangle: true,
  40. exports: getExportsFromData(item, curDepth + 1) || undefined
  41. }))
  42. : null;
  43. }
  44. /** @type {ExportSpec[]} */
  45. const exports = [];
  46. for (const key of Object.keys(data)) {
  47. exports.push({
  48. name: key,
  49. canMangle: true,
  50. exports: getExportsFromData(data[key], curDepth + 1) || undefined
  51. });
  52. }
  53. return exports;
  54. }
  55. return null;
  56. };
  57. class JsonExportsDependency extends NullDependency {
  58. /**
  59. * @param {JsonData} data json data
  60. * @param {number} exportsDepth the depth of json exports to analyze
  61. */
  62. constructor(data, exportsDepth) {
  63. super();
  64. this.data = data;
  65. this.exportsDepth = exportsDepth;
  66. }
  67. get type() {
  68. return "json exports";
  69. }
  70. /**
  71. * Returns the exported names
  72. * @param {ModuleGraph} moduleGraph module graph
  73. * @returns {ExportsSpec | undefined} export names
  74. */
  75. getExports(moduleGraph) {
  76. return {
  77. exports: getExportsWithDepth(this.exportsDepth)(
  78. this.data && /** @type {JsonValue} */ (this.data.get())
  79. ),
  80. dependencies: undefined
  81. };
  82. }
  83. /**
  84. * Update the hash
  85. * @param {Hash} hash hash to be updated
  86. * @param {UpdateHashContext} context context
  87. * @returns {void}
  88. */
  89. updateHash(hash, context) {
  90. this.data.updateHash(hash);
  91. }
  92. /**
  93. * @param {ObjectSerializerContext} context context
  94. */
  95. serialize(context) {
  96. const { write } = context;
  97. write(this.data);
  98. write(this.exportsDepth);
  99. super.serialize(context);
  100. }
  101. /**
  102. * @param {ObjectDeserializerContext} context context
  103. */
  104. deserialize(context) {
  105. const { read } = context;
  106. this.data = read();
  107. this.exportsDepth = read();
  108. super.deserialize(context);
  109. }
  110. }
  111. makeSerializable(
  112. JsonExportsDependency,
  113. "webpack/lib/dependencies/JsonExportsDependency"
  114. );
  115. module.exports = JsonExportsDependency;