vue.esm-bundler.js 2.3 KB

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