vue.esm-bundler.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 } 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__ */ Object.create(null);
  15. function compileToFunction(template, options) {
  16. if (!isString(template)) {
  17. if (template.nodeType) {
  18. template = template.innerHTML;
  19. } else {
  20. !!(process.env.NODE_ENV !== "production") && warn(`invalid template option: `, template);
  21. return NOOP;
  22. }
  23. }
  24. const key = template;
  25. const cached = compileCache[key];
  26. if (cached) {
  27. return cached;
  28. }
  29. if (template[0] === "#") {
  30. const el = document.querySelector(template);
  31. if (!!(process.env.NODE_ENV !== "production") && !el) {
  32. warn(`Template element not found or is empty: ${template}`);
  33. }
  34. template = el ? el.innerHTML : ``;
  35. }
  36. const opts = extend(
  37. {
  38. hoistStatic: true,
  39. onError: !!(process.env.NODE_ENV !== "production") ? onError : void 0,
  40. onWarn: !!(process.env.NODE_ENV !== "production") ? (e) => onError(e, true) : NOOP
  41. },
  42. options
  43. );
  44. if (!opts.isCustomElement && typeof customElements !== "undefined") {
  45. opts.isCustomElement = (tag) => !!customElements.get(tag);
  46. }
  47. const { code } = compile(template, opts);
  48. function onError(err, asWarning = false) {
  49. const message = asWarning ? err.message : `Template compilation error: ${err.message}`;
  50. const codeFrame = err.loc && generateCodeFrame(
  51. template,
  52. err.loc.start.offset,
  53. err.loc.end.offset
  54. );
  55. warn(codeFrame ? `${message}
  56. ${codeFrame}` : message);
  57. }
  58. const render = new Function("Vue", code)(runtimeDom);
  59. render._rc = true;
  60. return compileCache[key] = render;
  61. }
  62. registerRuntimeCompiler(compileToFunction);
  63. export { compileToFunction as compile };