vue.esm-bundler.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import * as runtimeDom from '@vue/runtime-dom';
  2. import { setDevtoolsHook, initCustomFormatter, warn, registerRuntimeCompiler } from '@vue/runtime-dom';
  3. export * from '@vue/runtime-dom';
  4. import { getGlobalThis, isString, NOOP, extend, generateCodeFrame } from '@vue/shared';
  5. import { compile } from '@vue/compiler-dom';
  6. function initDev() {
  7. const target = getGlobalThis();
  8. target.__VUE__ = true;
  9. setDevtoolsHook(target.__VUE_DEVTOOLS_GLOBAL_HOOK__);
  10. {
  11. initCustomFormatter();
  12. }
  13. }
  14. // This entry is the "full-build" that includes both the runtime
  15. if ((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) {
  16. initDev();
  17. }
  18. const compileCache = Object.create(null);
  19. function compileToFunction(template, options) {
  20. if (!isString(template)) {
  21. if (template.nodeType) {
  22. template = template.innerHTML;
  23. }
  24. else {
  25. (process.env.NODE_ENV !== 'production') && warn(`invalid template option: `, template);
  26. return 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 ((process.env.NODE_ENV !== 'production') && !el) {
  37. 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 { code } = compile(template, extend({
  46. hoistStatic: true,
  47. onError(err) {
  48. if ((process.env.NODE_ENV !== 'production')) {
  49. const message = `Template compilation error: ${err.message}`;
  50. const codeFrame = err.loc &&
  51. generateCodeFrame(template, err.loc.start.offset, err.loc.end.offset);
  52. warn(codeFrame ? `${message}\n${codeFrame}` : message);
  53. }
  54. else {
  55. /* istanbul ignore next */
  56. throw err;
  57. }
  58. }
  59. }, options));
  60. // The wildcard import results in a huge object with every export
  61. // with keys that cannot be mangled, and can be quite heavy size-wise.
  62. // In the global build we know `Vue` is available globally so we can avoid
  63. // the wildcard object.
  64. const render = (new Function('Vue', code)(runtimeDom));
  65. render._rc = true;
  66. return (compileCache[key] = render);
  67. }
  68. registerRuntimeCompiler(compileToFunction);
  69. export { compileToFunction as compile };