ContextElementDependency.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 makeSerializable = require("../util/makeSerializable");
  8. const ModuleDependency = require("./ModuleDependency");
  9. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  10. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  11. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  12. class ContextElementDependency extends ModuleDependency {
  13. constructor(request, userRequest, typePrefix, category, referencedExports) {
  14. super(request);
  15. this.referencedExports = referencedExports;
  16. this._typePrefix = typePrefix;
  17. this._category = category;
  18. if (userRequest) {
  19. this.userRequest = userRequest;
  20. }
  21. }
  22. get type() {
  23. if (this._typePrefix) {
  24. return `${this._typePrefix} context element`;
  25. }
  26. return "context element";
  27. }
  28. get category() {
  29. return this._category;
  30. }
  31. /**
  32. * Returns list of exports referenced by this dependency
  33. * @param {ModuleGraph} moduleGraph module graph
  34. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  35. * @returns {(string[] | ReferencedExport)[]} referenced exports
  36. */
  37. getReferencedExports(moduleGraph, runtime) {
  38. return this.referencedExports
  39. ? this.referencedExports.map(e => ({
  40. name: e,
  41. canMangle: false
  42. }))
  43. : Dependency.EXPORTS_OBJECT_REFERENCED;
  44. }
  45. serialize(context) {
  46. const { write } = context;
  47. write(this._typePrefix);
  48. write(this._category);
  49. write(this.referencedExports);
  50. super.serialize(context);
  51. }
  52. deserialize(context) {
  53. const { read } = context;
  54. this._typePrefix = read();
  55. this._category = read();
  56. this.referencedExports = read();
  57. super.deserialize(context);
  58. }
  59. }
  60. makeSerializable(
  61. ContextElementDependency,
  62. "webpack/lib/dependencies/ContextElementDependency"
  63. );
  64. module.exports = ContextElementDependency;