vue.esm-bundler.js 2.7 KB

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