DynamicExports.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../Parser").ParserState} ParserState */
  7. /** @type {WeakMap<ParserState, boolean>} */
  8. const parserStateExportsState = new WeakMap();
  9. /**
  10. * @param {ParserState} parserState parser state
  11. * @returns {void}
  12. */
  13. exports.bailout = parserState => {
  14. const value = parserStateExportsState.get(parserState);
  15. parserStateExportsState.set(parserState, false);
  16. if (value === true) {
  17. parserState.module.buildMeta.exportsType = undefined;
  18. parserState.module.buildMeta.defaultObject = false;
  19. }
  20. };
  21. /**
  22. * @param {ParserState} parserState parser state
  23. * @returns {void}
  24. */
  25. exports.enable = parserState => {
  26. const value = parserStateExportsState.get(parserState);
  27. if (value === false) return;
  28. parserStateExportsState.set(parserState, true);
  29. if (value !== true) {
  30. parserState.module.buildMeta.exportsType = "default";
  31. parserState.module.buildMeta.defaultObject = "redirect";
  32. }
  33. };
  34. /**
  35. * @param {ParserState} parserState parser state
  36. * @returns {void}
  37. */
  38. exports.setFlagged = parserState => {
  39. const value = parserStateExportsState.get(parserState);
  40. if (value !== true) return;
  41. const buildMeta = parserState.module.buildMeta;
  42. if (buildMeta.exportsType === "dynamic") return;
  43. buildMeta.exportsType = "flagged";
  44. };
  45. /**
  46. * @param {ParserState} parserState parser state
  47. * @returns {void}
  48. */
  49. exports.setDynamic = parserState => {
  50. const value = parserStateExportsState.get(parserState);
  51. if (value !== true) return;
  52. parserState.module.buildMeta.exportsType = "dynamic";
  53. };
  54. /**
  55. * @param {ParserState} parserState parser state
  56. * @returns {boolean} true, when enabled
  57. */
  58. exports.isEnabled = parserState => {
  59. const value = parserStateExportsState.get(parserState);
  60. return value === true;
  61. };