matchAlias.js 887 B

12345678910111213141516171819202122232425
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const SLASH_CHAR_CODE = 47;
  7. /**
  8. * Whether a `resolve.alias` name applies to a request. Mirrors `AliasPlugin`:
  9. * a name matches the whole request or a leading segment of it, and
  10. * `onlyModule` restricts it to the former.
  11. * @param {string} request the request as written
  12. * @param {string} name the alias name, already normalized by enhanced-resolve
  13. * @param {boolean=} onlyModule whether the alias only matches the whole request
  14. * @returns {boolean} true when the alias applies to the request
  15. */
  16. const matchAlias = (request, name, onlyModule) => {
  17. if (!request.startsWith(name)) return false;
  18. if (request.length === name.length) return true;
  19. return !onlyModule && request.charCodeAt(name.length) === SLASH_CHAR_CODE;
  20. };
  21. module.exports = matchAlias;