vue.cjs.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var compilerDom = require('@vue/compiler-dom');
  4. var runtimeDom = require('@vue/runtime-dom');
  5. var shared = require('@vue/shared');
  6. function _interopNamespaceDefault(e) {
  7. var n = Object.create(null);
  8. if (e) {
  9. for (var k in e) {
  10. n[k] = e[k];
  11. }
  12. }
  13. n.default = e;
  14. return Object.freeze(n);
  15. }
  16. var runtimeDom__namespace = /*#__PURE__*/_interopNamespaceDefault(runtimeDom);
  17. const compileCache = /* @__PURE__ */ new WeakMap();
  18. function getCache(options) {
  19. let c = compileCache.get(options != null ? options : shared.EMPTY_OBJ);
  20. if (!c) {
  21. c = /* @__PURE__ */ Object.create(null);
  22. compileCache.set(options != null ? options : shared.EMPTY_OBJ, c);
  23. }
  24. return c;
  25. }
  26. function compileToFunction(template, options) {
  27. if (!shared.isString(template)) {
  28. if (template.nodeType) {
  29. template = template.innerHTML;
  30. } else {
  31. runtimeDom.warn(`invalid template option: `, template);
  32. return shared.NOOP;
  33. }
  34. }
  35. const key = template;
  36. const cache = getCache(options);
  37. const cached = cache[key];
  38. if (cached) {
  39. return cached;
  40. }
  41. if (template[0] === "#") {
  42. const el = document.querySelector(template);
  43. if (!el) {
  44. runtimeDom.warn(`Template element not found or is empty: ${template}`);
  45. }
  46. template = el ? el.innerHTML : ``;
  47. }
  48. const opts = shared.extend(
  49. {
  50. hoistStatic: true,
  51. onError: onError ,
  52. onWarn: (e) => onError(e, true)
  53. },
  54. options
  55. );
  56. if (!opts.isCustomElement && typeof customElements !== "undefined") {
  57. opts.isCustomElement = (tag) => !!customElements.get(tag);
  58. }
  59. const { code } = compilerDom.compile(template, opts);
  60. function onError(err, asWarning = false) {
  61. const message = asWarning ? err.message : `Template compilation error: ${err.message}`;
  62. const codeFrame = err.loc && shared.generateCodeFrame(
  63. template,
  64. err.loc.start.offset,
  65. err.loc.end.offset
  66. );
  67. runtimeDom.warn(codeFrame ? `${message}
  68. ${codeFrame}` : message);
  69. }
  70. const render = new Function("Vue", code)(runtimeDom__namespace);
  71. render._rc = true;
  72. return cache[key] = render;
  73. }
  74. runtimeDom.registerRuntimeCompiler(compileToFunction);
  75. exports.compile = compileToFunction;
  76. Object.keys(runtimeDom).forEach(function (k) {
  77. if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = runtimeDom[k];
  78. });