Skip to content

Instantly share code, notes, and snippets.

@loucyx
Last active October 7, 2025 16:07
Show Gist options
  • Select an option

  • Save loucyx/884a22493dc95ef76c4adf5b2202cf50 to your computer and use it in GitHub Desktop.

Select an option

Save loucyx/884a22493dc95ef76c4adf5b2202cf50 to your computer and use it in GitHub Desktop.
Bookmarklet to have a tiny "AMD" require implementation in the global scope.
javascript:globalThis.require??=(p,c)=>Promise.all(p.map(i=>import(i.replace(/^(\w{3}):/,(_,m)=>`//esm.sh/${m==="npm"?"":`${m}/`}`)))).then(c);
require(["npm:htm/preact", "npm:preact", "npm:preact/hooks"], ([
{ html },
{ render },
{ useState },
]) => {
const Counter = () => {
const [count, setCount] = useState(0);
return html`
<button onClick=${() => setCount(count + 1)}>
Clicked ${count} times
</button>
`;
};
render(html`<${Counter} />`, document.body);
});
globalThis.require ??= (paths, callback) =>
Promise.all(
paths.map(
(path) =>
import(
path.replace(
/^(\w{3}):/,
(_, match) =>
`//esm.sh/${match === "npm" ? "" : `${match}/`}`,
)
),
),
).then(callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment