templateUtils.ts 996 B

12345678910111213141516171819202122232425262728
  1. import { UrlWithStringQuery, parse as uriParse } from 'url'
  2. import { isString } from '@vue/shared'
  3. export function isRelativeUrl(url: string): boolean {
  4. const firstChar = url.charAt(0)
  5. return firstChar === '.' || firstChar === '~' || firstChar === '@'
  6. }
  7. // We need an extra transform context API for injecting arbitrary import
  8. // statements.
  9. export function parseUrl(url: string): UrlWithStringQuery {
  10. const firstChar = url.charAt(0)
  11. if (firstChar === '~') {
  12. const secondChar = url.charAt(1)
  13. url = url.slice(secondChar === '/' ? 2 : 1)
  14. }
  15. return parseUriParts(url)
  16. }
  17. /**
  18. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  19. * @param urlString an url as a string
  20. */
  21. function parseUriParts(urlString: string): UrlWithStringQuery {
  22. // A TypeError is thrown if urlString is not a string
  23. // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
  24. return uriParse(isString(urlString) ? urlString : '')
  25. }