DynamicExports.js 2.0 KB

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