utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Manually “tree shaken” from:
  2. // <https://github.com/nodejs/node/blob/81a9a97/lib/internal/modules/esm/utils.js>
  3. // Last checked on: Apr 29, 2024.
  4. import {codes} from './errors.js'
  5. const {ERR_INVALID_ARG_VALUE} = codes
  6. // In Node itself these values are populated from CLI arguments, before any
  7. // user code runs.
  8. // Here we just define the defaults.
  9. const DEFAULT_CONDITIONS = Object.freeze(['node', 'import'])
  10. const DEFAULT_CONDITIONS_SET = new Set(DEFAULT_CONDITIONS)
  11. /**
  12. * Returns the default conditions for ES module loading.
  13. */
  14. function getDefaultConditions() {
  15. return DEFAULT_CONDITIONS
  16. }
  17. /**
  18. * Returns the default conditions for ES module loading, as a Set.
  19. */
  20. function getDefaultConditionsSet() {
  21. return DEFAULT_CONDITIONS_SET
  22. }
  23. /**
  24. * @param {Array<string>} [conditions]
  25. * @returns {Set<string>}
  26. */
  27. export function getConditionsSet(conditions) {
  28. if (conditions !== undefined && conditions !== getDefaultConditions()) {
  29. if (!Array.isArray(conditions)) {
  30. throw new ERR_INVALID_ARG_VALUE(
  31. 'conditions',
  32. conditions,
  33. 'expected an array'
  34. )
  35. }
  36. return new Set(conditions)
  37. }
  38. return getDefaultConditionsSet()
  39. }