assetUrl.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. // vue compiler module for transforming `<tag>:<attribute>` to `require`
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const utils_1 = require("./utils");
  5. const defaultOptions = {
  6. audio: 'src',
  7. video: ['src', 'poster'],
  8. source: 'src',
  9. img: 'src',
  10. image: ['xlink:href', 'href'],
  11. use: ['xlink:href', 'href']
  12. };
  13. exports.default = (userOptions, transformAssetUrlsOption) => {
  14. const options = userOptions
  15. ? Object.assign({}, defaultOptions, userOptions)
  16. : defaultOptions;
  17. return {
  18. postTransformNode: (node) => {
  19. transform(node, options, transformAssetUrlsOption);
  20. }
  21. };
  22. };
  23. function transform(node, options, transformAssetUrlsOption) {
  24. for (const tag in options) {
  25. if ((tag === '*' || node.tag === tag) && node.attrs) {
  26. const attributes = options[tag];
  27. if (typeof attributes === 'string') {
  28. node.attrs.some(attr => rewrite(attr, attributes, transformAssetUrlsOption));
  29. }
  30. else if (Array.isArray(attributes)) {
  31. attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item, transformAssetUrlsOption)));
  32. }
  33. }
  34. }
  35. }
  36. function rewrite(attr, name, transformAssetUrlsOption) {
  37. if (attr.name === name) {
  38. const value = attr.value;
  39. // only transform static URLs
  40. if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
  41. attr.value = utils_1.urlToRequire(value.slice(1, -1), transformAssetUrlsOption);
  42. return true;
  43. }
  44. }
  45. return false;
  46. }