nostalgie
Advanced tools
Comparing version 0.39.0 to 0.39.1
@@ -26,2 +26,15 @@ 'use strict'; | ||
let timeout = null; | ||
const findParentScriptTag = (node) => { | ||
let nextNode = node; | ||
while (nextNode) { | ||
if (nextNode instanceof HTMLScriptElement) { | ||
break; | ||
} | ||
nextNode = nextNode.parentNode; | ||
if (!nextNode) { | ||
throw new Error(`Unable to find the triggering script element`); | ||
} | ||
} | ||
return nextNode; | ||
}; | ||
const onChange = (records) => { | ||
@@ -32,3 +45,3 @@ if (timeout) { | ||
timeout = setTimeout(() => { | ||
refresh(records.map((record) => record.target)); | ||
refresh(records.map((record) => findParentScriptTag(record.target))); | ||
timeout = null; | ||
@@ -38,2 +51,3 @@ }, 1000); | ||
const observer = new MutationObserver(onChange); | ||
const scriptTagsToBasePaths = new WeakMap(); | ||
function refresh(scripts) { | ||
@@ -44,4 +58,8 @@ const entrypointPaths = []; | ||
for (const script of scripts) { | ||
const scriptId = idx++; | ||
const basePath = `/script/${scriptId}`; | ||
let basePath = scriptTagsToBasePaths.get(script); | ||
if (!basePath) { | ||
const scriptId = idx++; | ||
basePath = `/script/${scriptId}`; | ||
scriptTagsToBasePaths.set(script, basePath); | ||
} | ||
const rawDependencies = script.dataset.dependencies; | ||
@@ -59,3 +77,3 @@ const dependencies = {}; | ||
files[`${basePath}/package.json`] = JSON.stringify({ | ||
name: `script${scriptId}`, | ||
name: 'script', | ||
version: '0.0.0', | ||
@@ -82,3 +100,8 @@ main: 'index.js', | ||
entrypointPaths.push(`${basePath}/index.js`); | ||
observer.observe(script, { childList: true }); | ||
observer.observe(script, { | ||
attributes: true, | ||
attributeFilter: ['data-dependencies', 'src'], | ||
characterData: true, | ||
subtree: true, | ||
}); | ||
} | ||
@@ -85,0 +108,0 @@ const cdnStrategy = strategyCdn.CdnStrategy.forJsDelivr(readUrl); |
@@ -26,2 +26,15 @@ 'use strict'; | ||
let timeout = null; | ||
const findParentScriptTag = (node) => { | ||
let nextNode = node; | ||
while (nextNode) { | ||
if (nextNode instanceof HTMLScriptElement) { | ||
break; | ||
} | ||
nextNode = nextNode.parentNode; | ||
if (!nextNode) { | ||
throw new Error(`Unable to find the triggering script element`); | ||
} | ||
} | ||
return nextNode; | ||
}; | ||
const onChange = (records) => { | ||
@@ -32,3 +45,3 @@ if (timeout) { | ||
timeout = setTimeout(() => { | ||
refresh(records.map((record) => record.target)); | ||
refresh(records.map((record) => findParentScriptTag(record.target))); | ||
timeout = null; | ||
@@ -38,2 +51,3 @@ }, 1000); | ||
const observer = new MutationObserver(onChange); | ||
const scriptTagsToBasePaths = new WeakMap(); | ||
function refresh(scripts) { | ||
@@ -44,4 +58,8 @@ const entrypointPaths = []; | ||
for (const script of scripts) { | ||
const scriptId = idx++; | ||
const basePath = `/script/${scriptId}`; | ||
let basePath = scriptTagsToBasePaths.get(script); | ||
if (!basePath) { | ||
const scriptId = idx++; | ||
basePath = `/script/${scriptId}`; | ||
scriptTagsToBasePaths.set(script, basePath); | ||
} | ||
const rawDependencies = script.dataset.dependencies; | ||
@@ -59,3 +77,3 @@ const dependencies = {}; | ||
files[`${basePath}/package.json`] = JSON.stringify({ | ||
name: `script${scriptId}`, | ||
name: 'script', | ||
version: '0.0.0', | ||
@@ -82,3 +100,8 @@ main: 'index.js', | ||
entrypointPaths.push(`${basePath}/index.js`); | ||
observer.observe(script, { childList: true }); | ||
observer.observe(script, { | ||
attributes: true, | ||
attributeFilter: ['data-dependencies', 'src'], | ||
characterData: true, | ||
subtree: true, | ||
}); | ||
} | ||
@@ -85,0 +108,0 @@ const cdnStrategy = strategyCdn.CdnStrategy.forJsDelivr(readUrl); |
{ | ||
"name": "nostalgie", | ||
"version": "0.39.0", | ||
"version": "0.39.1", | ||
"description": "A reminder of what the web once was - your hopes and dreams and a single html file", | ||
@@ -71,3 +71,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "2af5691d917fb97c783dbc736c422f07941f141c" | ||
"gitHead": "d7a0ba61dc4dfac9acd58f9cd7c38be0f247b2c6" | ||
} |
@@ -27,3 +27,3 @@ # nostalgie | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/nostalgie@0.39.0/dist/index.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/nostalgie@0.39.1/dist/index.umd.js"></script> | ||
``` | ||
@@ -30,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
2149968
2468