vue.esm-bundler.js 2.5 KB

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