JsonExportsDependency.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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").RawJsonData} RawJsonData */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. /**
  19. * @param {number} exportsDepth exportsDepth
  20. * @returns {((data: RawJsonData, curDepth?: number) => ExportSpec[] | undefined)} value
  21. */
  22. const getExportsWithDepth = exportsDepth =>
  23. function getExportsFromData(data, curDepth = 1) {
  24. if (curDepth > exportsDepth) return undefined;
  25. if (data && typeof data === "object") {
  26. if (Array.isArray(data)) {
  27. return data.length < 100
  28. ? data.map((item, idx) => ({
  29. name: `${idx}`,
  30. canMangle: true,
  31. exports: getExportsFromData(item, curDepth + 1)
  32. }))
  33. : undefined;
  34. }
  35. const exports = [];
  36. for (const key of Object.keys(data)) {
  37. exports.push({
  38. name: key,
  39. canMangle: true,
  40. exports: getExportsFromData(data[key], curDepth + 1)
  41. });
  42. }
  43. return exports;
  44. }
  45. return undefined;
  46. };
  47. class JsonExportsDependency extends NullDependency {
  48. /**
  49. * @param {JsonData} data json data
  50. * @param {number} exportsDepth the depth of json exports to analyze
  51. */
  52. constructor(data, exportsDepth) {
  53. super();
  54. this.data = data;
  55. this.exportsDepth = exportsDepth;
  56. }
  57. get type() {
  58. return "json exports";
  59. }
  60. /**
  61. * Returns the exported names
  62. * @param {ModuleGraph} moduleGraph module graph
  63. * @returns {ExportsSpec | undefined} export names
  64. */
  65. getExports(moduleGraph) {
  66. return {
  67. exports: getExportsWithDepth(this.exportsDepth)(
  68. this.data && /** @type {RawJsonData} */ (this.data.get())
  69. ),
  70. dependencies: undefined
  71. };
  72. }
  73. /**
  74. * Update the hash
  75. * @param {Hash} hash hash to be updated
  76. * @param {UpdateHashContext} context context
  77. * @returns {void}
  78. */
  79. updateHash(hash, context) {
  80. this.data.updateHash(hash);
  81. }
  82. /**
  83. * @param {ObjectSerializerContext} context context
  84. */
  85. serialize(context) {
  86. const { write } = context;
  87. write(this.data);
  88. write(this.exportsDepth);
  89. super.serialize(context);
  90. }
  91. /**
  92. * @param {ObjectDeserializerContext} context context
  93. */
  94. deserialize(context) {
  95. const { read } = context;
  96. this.data = read();
  97. this.exportsDepth = read();
  98. super.deserialize(context);
  99. }
  100. }
  101. makeSerializable(
  102. JsonExportsDependency,
  103. "webpack/lib/dependencies/JsonExportsDependency"
  104. );
  105. module.exports = JsonExportsDependency;