JsonExportsDependency.js 2.5 KB

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