getParserHelpers.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { webpackVersion } = require('../globals');
  2. /**
  3. * @callback EvaluateToString
  4. * @param {string} value
  5. * @returns {function(*): *}
  6. */
  7. /**
  8. * @callback ToConstantDependency
  9. * @param {*} parser
  10. * @param {string} value
  11. * @param {string[]} [runtimeRequirements]
  12. * @returns {function(*): boolean}
  13. */
  14. /**
  15. * @typedef {Object} ParserHelpers
  16. * @property {EvaluateToString} evaluateToString
  17. * @property {ToConstantDependency} toConstantDependency
  18. */
  19. /**
  20. * Gets parser helpers for specific webpack versions.
  21. * @returns {ParserHelpers} The needed parser helpers.
  22. */
  23. function getParserHelpers() {
  24. if (webpackVersion === 4) {
  25. const {
  26. evaluateToString,
  27. toConstantDependencyWithWebpackRequire,
  28. } = require('webpack/lib/ParserHelpers');
  29. return {
  30. evaluateToString,
  31. toConstantDependency: toConstantDependencyWithWebpackRequire,
  32. };
  33. }
  34. if (webpackVersion === 5) {
  35. return require('webpack/lib/javascript/JavascriptParserHelpers');
  36. }
  37. }
  38. module.exports = getParserHelpers;