Public
Edited
Jan 15, 2023
Insert cell
Insert cell
//require("just-once")
Insert cell
sauce = fetch("https://unpkg.com/@babel/traverse").then(async (_) => [
getUrlDirectory(_.url),
await _.text()
])
Insert cell
unpkg = "https://unpkg.com/"
Insert cell
function getUrlDirectory(url) {
const urlObject = new URL(url);
const pathname = urlObject.pathname;
const lastSlashIndex = pathname.lastIndexOf("/");
return pathname.substring(1, lastSlashIndex);
}
Insert cell
Object.fromEntries(
[...sauce[1].matchAll(/require\(["'](.+?)["']\)/g)]
.map((a) => a[1])
.map((a) => [
a,
{
url:
a[0] == "." ? unpkg + simplifyUrl(unpkg + sauce[0] + a) : unpkg + a,
tree: {}
}
])
)
Insert cell
function simplifyUrl(url) {
const urlObject = new URL(url);
let pathname = urlObject.pathname;

// Remove any leading '/'
if (pathname[0] === "/") {
pathname = pathname.substring(1);
}

// Split the pathname into an array of parts
const parts = pathname.split("/");

// Create a new array to store the simplified parts
const simplified = [];

// Iterate over the parts
for (let i = 0; i < parts.length; i++) {
const part = parts[i];

// If the part is '.' or '', skip it
if (part === "." || part === "") {
continue;
}

// If the part is '..', remove the last element from the simplified array
if (part === "..") {
simplified.pop();
} else {
// Otherwise, add the part to the simplified array
simplified.push(part);
}
}

// Join the simplified parts and return the result
return "/" + simplified.join("/");
}
Insert cell
{
var TopTree = {};
var urls = [];

async function getChildren(url, tree, path) {
if (path.includes(url)) {
tree.circular = true;
console.log(path, url);
console.log("circular dependency detected, sad");
return tree;
} else if (urls.includes(url)) {
tree.requested = true;
return tree;
} else {
urls.push(url);
}
path.push(url);
let sauce = await fetch(url).then(async (_) => [
getUrlDirectory(_.url),
await _.text()
]);
Object.assign(
tree,
Object.fromEntries(
[...sauce[1].matchAll(/require\(["'](.+?)["']\)/g)]
.map((a) => a[1])
.map((a) => [
a,
{
url:
a[0] == "."
? unpkg + simplifyUrl(unpkg + sauce[0] + "/" + a).substr(1)
: unpkg + a,
tree: {}
}
])
)
);
for (const [key, value] of Object.entries(tree)) {
await getChildren(value.url, value.tree, path.slice());
console.log("fetching " + value.url);
}
}
getChildren("https://unpkg.com/@babel/traverse", TopTree, []);
return TopTree;
}
Insert cell
//FS = { require("https://unpkg.com/@isomorphic-git/lightning-fs@4.6.0/src/index.js")}
Insert cell
//fs = new FS("testfs")
Insert cell
//Babel = require("https://unpkg.com/@babel/standalone@7.0.0-beta.40/babel.js")
Insert cell
{
const code = `function square(n) {
return n * n;
}`;
return traverse.traverse(parser.parse(code), {
Program(path) {
console.log("hi");
}
});
}
Insert cell
parser = {
/*const requires = require.alias({
react95: `https://unpkg.com/@babel/parser`
});*/
const modules = {};
const _exports = (window.exports = {});
const _require = (window.require = (name) => modules[name]);
return require("@babel/parser").catch(() => {
delete window.exports;
delete window.require;
return _exports;
});
}
Insert cell
traverse = {
parser;//avoids race condition
/*const requires = require.alias({
react95: `https://unpkg.com/@babel/parser`
});*/
const modules = {};
const _exports = (window.exports = {});
const _require = (window.require = (name) => modules[name]);
return require("@babel/traverse").catch(() => {
delete window.exports;
delete window.require;
return _exports;
});
}
Insert cell
parser
Insert cell
traverse
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more