add license and client apis

This commit is contained in:
Avad3 2024-06-17 06:20:32 -04:00
parent ecff2aca52
commit aac959936c
19 changed files with 1004 additions and 89 deletions

View file

@ -1,34 +1,62 @@
import { parse } from "meriyah";
import { parseModule } from "meriyah";
import { generate } from "astring";
import { makeTraveler } from "astravel";
import { encodeUrl } from "./url";
// i am a cat. i like to be petted. i like to be fed. i like to be
// js rewiter is NOT finished
// location
// window
// self
// globalThis
// this
// top
// parent
export function rewriteJs(js: string, origin?: URL) {
const ast = parse(js, {
module: true
});
const customTraveler = makeTraveler({
ImportDeclaration: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ImportExpression: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ExportAllDeclaration: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ExportNamedDeclaration: (node) => {
if (node.source) node.source.value = encodeUrl(node.source.value, origin);
}
});
customTraveler.go(ast);
try {
const ast = parseModule(js, {
module: true,
webcompat: true
});
return generate(ast);
// const identifierList = [
// "window",
// "self",
// "globalThis",
// "parent",
// "top",
// "location",
// ""
// ]
const customTraveler = makeTraveler({
ImportDeclaration: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ImportExpression: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ExportAllDeclaration: (node) => {
node.source.value = encodeUrl(node.source.value, origin);
},
ExportNamedDeclaration: (node) => {
if (node.source) node.source.value = encodeUrl(node.source.value, origin);
},
});
customTraveler.go(ast);
return generate(ast);
} catch {
console.log(js);
return js;
}
}