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