Advertisement
Guest User

Untitled

a guest
Jul 13th, 2020
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. const html = `<!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document: A-FRAME</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <script src="main.bundle.js"></script><script src="vendor.bundle.js"></script></body>
  13.  
  14. </html>`;
  15.  
  16. // Initial inject HTML into document. Only run once.
  17. document.addEventListener('DOMContentLoaded', append);
  18.  
  19. function append () {
  20. let container = document.getElementById('app');
  21. if (!container) {
  22. container = document.createElement('div');
  23. container.id = 'app';
  24. document.body.appendChild(container);
  25. }
  26. container.innerHTML = html;
  27. }
  28.  
  29. if (module.hot) {
  30. const DiffDom = require('diff-dom').DiffDOM;
  31.  
  32. module.hot.accept();
  33.  
  34. // HTML changed, diff.
  35. if (module.hot.data && module.hot.data.oldHtml) {
  36. const oldHtml = module.hot.data.oldHtml;
  37. const diffdom = new DiffDom();
  38.  
  39. const oldScene = document.createElement('div');
  40. oldScene.innerHTML = oldHtml;
  41.  
  42. const newScene = document.createElement('div');
  43. newScene.innerHTML = html;
  44.  
  45. const diff = diffdom.diff(oldScene, newScene);
  46. diffdom.apply(document.getElementById('app'), diff);
  47.  
  48. Array.from(newScene.querySelectorAll('template')).forEach(template => {
  49. const liveTemplate = document.getElementById(template.id);
  50. if (liveTemplate.innerHTML === template.innerHTML) { return; }
  51. liveTemplate.innerHTML = template.innerHTML;
  52. liveTemplate.dispatchEvent(new CustomEvent('templatemutate'));
  53. });
  54. }
  55.  
  56. // Store HTML for next module to diff.
  57. module.hot.dispose(data => {
  58. data.oldHtml = html;
  59. });
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement