utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.urlToRequire = void 0;
  7. const url_1 = require("url");
  8. const path_1 = __importDefault(require("path"));
  9. function urlToRequire(url, transformAssetUrlsOption = {}) {
  10. const returnValue = `"${url}"`;
  11. // same logic as in transform-require.js
  12. const firstChar = url.charAt(0);
  13. if (firstChar === '~') {
  14. const secondChar = url.charAt(1);
  15. url = url.slice(secondChar === '/' ? 2 : 1);
  16. }
  17. const uriParts = parseUriParts(url);
  18. if (transformAssetUrlsOption.base) {
  19. // explicit base - directly rewrite the url into absolute url
  20. // does not apply to absolute urls or urls that start with `@`
  21. // since they are aliases
  22. if (firstChar === '.' || firstChar === '~') {
  23. // when packaged in the browser, path will be using the posix-
  24. // only version provided by rollup-plugin-node-builtins.
  25. return `"${(path_1.default.posix || path_1.default).join(transformAssetUrlsOption.base, uriParts.path + (uriParts.hash || ''))}"`;
  26. }
  27. return returnValue;
  28. }
  29. if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
  30. if (!uriParts.hash) {
  31. return `require("${url}")`;
  32. }
  33. else {
  34. // support uri fragment case by excluding it from
  35. // the require and instead appending it as string;
  36. // assuming that the path part is sufficient according to
  37. // the above caseing(t.i. no protocol-auth-host parts expected)
  38. return `require("${uriParts.path}") + "${uriParts.hash}"`;
  39. }
  40. }
  41. return returnValue;
  42. }
  43. exports.urlToRequire = urlToRequire;
  44. /**
  45. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  46. * @param urlString an url as a string
  47. */
  48. function parseUriParts(urlString) {
  49. // initialize return value
  50. const returnValue = url_1.parse('');
  51. if (urlString) {
  52. // A TypeError is thrown if urlString is not a string
  53. // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
  54. if ('string' === typeof urlString) {
  55. // check is an uri
  56. return url_1.parse(urlString); // take apart the uri
  57. }
  58. }
  59. return returnValue;
  60. }