Table of Contents
References
const x = {
default: "a",
b: "42",
c: () => {},
};
const { default: y, b: z } = x;
return { y, z };
Skypack
const skypack = (pkg) => import(`https://cdn.skypack.dev/${pkg}`);
return skypack("canvas-confetti");
const skypack = (pkg) => import(`https://cdn.skypack.dev/${pkg}`);
return skypack("canvas-confetti").then((pkg) => {
const { default: fire } = pkg;
return fire();
});
const skypack = (pkg) => import(`https://cdn.skypack.dev/${pkg}`);
// I like async await
return (async (fn) => {
const { default: fire } = await skypack("canvas-confetti");
return fire();
})();
Unpkg
const unpkg = (pkg) => import(`https://unpkg.com/${pkg}?module`);
return unpkg("canvas-confetti");
const unpkg = (pkg) => import(`https://unpkg.com/${pkg}?module`);
return unpkg("canvas-confetti").then((pkg) => {
return pkg.default();
});
Native ES Module REPL
import fire from "https://cdn.skypack.dev/canvas-confetti";
export const success = "yes";
export const issue =
"module caching means it only executes once, had to add a cachebusting comment.";
export default () => import.meta
export const cons = typeof console !== "undefined" ? console : null;
fire();
import foo from '/notfound.js'
export default foo
export default async function (...args) {
return { args, console, ctx: this };
};
export default fn => console.screenshot(document.body)
const { inspect } = lit.utils;
const { wait } = lit.utils.fns;
export default async (fn) => {
// https://github.com/whatwg/console/issues/120
console.record();
await wait(1000);
console.log("tick", new Date());
await wait(1000);
console.log("tock", new Date());
return console.recordEnd.toString();
};
Local modules
This assumes you have the experimental service worker (testing/ServiceWorker) (version 0.2.10) enabled to vend files in the local filesystem via Web fetch API.
const url = new URL("./foo", location.href)
return url.toString()
import foo from "HORRIBLE_HACKcustom-module.mjs";
export const test1 = "HORRIBLE_HACKfoo";
export const test2 = new URL("HORRIBLE_HACKcustom-module.mjs").toString();
export const meta = import.meta;
export default foo();
The correct resolution to the above hacks https://www.npmjs.com/package/babel-plugin-bare-import-rewrite↗
return fetch('/testing/custom-module.mjs').then(res => res.headers.get('Content-Type'))
-
Seems likeGot0.2.2
is Still not setting headers correctly, was expectingtext/javascript
. - fetch/sw now sends correct mime type, but still get
Module name, '/testing/custom-module.mjs' does not resolve to a valid URL.
return fetch('/testing/custom-module.mjs--sw').then(res => res.text())
return fetch('/testing/custom-module.mjs').then(res => res.headers.get('server'))
-
After updating the service worker to return the correct mime type, I suspect it's failing to resolve urls relative to the base64 data:uri setup. c.f. Original repl implementation which usescreateObjectURL
const esm = ({ raw }, ...vals) =>
URL.createObjectURL(
new Blob([String.raw({ raw }, ...vals)], { type: "text/javascript" })
);