You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB

4 years ago
  1. export interface Attr {
  2. name: string
  3. value: string
  4. }
  5. export interface ASTNode {
  6. tag: string
  7. attrs: Attr[]
  8. }
  9. import { UrlWithStringQuery, parse as uriParse } from 'url'
  10. export function urlToRequire(url: string): string {
  11. const returnValue = `"${url}"`
  12. // same logic as in transform-require.js
  13. const firstChar = url.charAt(0)
  14. if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
  15. if (firstChar === '~') {
  16. const secondChar = url.charAt(1)
  17. url = url.slice(secondChar === '/' ? 2 : 1)
  18. }
  19. const uriParts = parseUriParts(url)
  20. if (!uriParts.hash) {
  21. return `require("${url}")`
  22. } else {
  23. // support uri fragment case by excluding it from
  24. // the require and instead appending it as string;
  25. // assuming that the path part is sufficient according to
  26. // the above caseing(t.i. no protocol-auth-host parts expected)
  27. return `require("${uriParts.path}") + "${uriParts.hash}"`
  28. }
  29. }
  30. return returnValue
  31. }
  32. /**
  33. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  34. * @param urlString an url as a string
  35. */
  36. function parseUriParts(urlString: string): UrlWithStringQuery {
  37. // initialize return value
  38. const returnValue: UrlWithStringQuery = uriParse('')
  39. if (urlString) {
  40. // A TypeError is thrown if urlString is not a string
  41. // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
  42. if ('string' === typeof urlString) {
  43. // check is an uri
  44. return uriParse(urlString) // take apart the uri
  45. }
  46. }
  47. return returnValue
  48. }