vue.cjs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // This entry is the "full-build" that includes both the runtime
  18. const compileCache = Object.create(null);
  19. function compileToFunction(template, options) {
  20. if (!shared.isString(template)) {
  21. if (template.nodeType) {
  22. template = template.innerHTML;
  23. }
  24. else {
  25. runtimeDom.warn(`invalid template option: `, template);
  26. return shared.NOOP;
  27. }
  28. }
  29. const key = template;
  30. const cached = compileCache[key];
  31. if (cached) {
  32. return cached;
  33. }
  34. if (template[0] === '#') {
  35. const el = document.querySelector(template);
  36. if (!el) {
  37. runtimeDom.warn(`Template element not found or is empty: ${template}`);
  38. }
  39. // __UNSAFE__
  40. // Reason: potential execution of JS expressions in in-DOM template.
  41. // The user must make sure the in-DOM template is trusted. If it's rendered
  42. // by the server, the template should not contain any user data.
  43. template = el ? el.innerHTML : ``;
  44. }
  45. const opts = shared.extend({
  46. hoistStatic: true,
  47. onError: onError ,
  48. onWarn: e => onError(e, true)
  49. }, options);
  50. if (!opts.isCustomElement && typeof customElements !== 'undefined') {
  51. opts.isCustomElement = tag => !!customElements.get(tag);
  52. }
  53. const { code } = compilerDom.compile(template, opts);
  54. function onError(err, asWarning = false) {
  55. const message = asWarning
  56. ? err.message
  57. : `Template compilation error: ${err.message}`;
  58. const codeFrame = err.loc &&
  59. shared.generateCodeFrame(template, err.loc.start.offset, err.loc.end.offset);
  60. runtimeDom.warn(codeFrame ? `${message}\n${codeFrame}` : message);
  61. }
  62. // The wildcard import results in a huge object with every export
  63. // with keys that cannot be mangled, and can be quite heavy size-wise.
  64. // In the global build we know `Vue` is available globally so we can avoid
  65. // the wildcard object.
  66. const render = (new Function('Vue', code)(runtimeDom__namespace));
  67. render._rc = true;
  68. return (compileCache[key] = render);
  69. }
  70. runtimeDom.registerRuntimeCompiler(compileToFunction);
  71. exports.compile = compileToFunction;
  72. Object.keys(runtimeDom).forEach(function(k) {
  73. if (k !== 'default') exports[k] = runtimeDom[k];
  74. });