This commit is contained in:
velzie 2024-07-14 19:17:49 -04:00
parent 4b0337db0c
commit 55b6666229
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
43 changed files with 4693 additions and 2712 deletions

View file

@ -16,81 +16,94 @@ import * as ESTree from "estree";
// top
// parent
export function rewriteJs(js: string, origin?: URL) {
const htmlcomment = /<!--[\s\S]*?-->/g;
js = js.replace(htmlcomment, "");
try {
const ast = parseModule(js, {
module: true,
webcompat: true
});
const htmlcomment = /<!--[\s\S]*?-->/g;
js = js.replace(htmlcomment, "");
try {
const ast = parseModule(js, {
module: true,
webcompat: true,
});
const identifierList = [
"window",
"self",
"globalThis",
"this",
"parent",
"top",
"location"
]
const identifierList = [
"window",
"self",
"globalThis",
"this",
"parent",
"top",
"location",
];
const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
ImportExpression: (node: ESTree.ImportExpression) => {
if (node.source.type === "Literal") {
node.source.value = encodeUrl(node.source.value as string, origin);
} else if (node.source.type === "Identifier") {
// this is for things that import something like
// const moduleName = "name";
// await import(moduleName);
node.source.name = `__wrapImport(${node.source.name})`;
}
},
ImportExpression: (node: ESTree.ImportExpression) => {
if (node.source.type === "Literal") {
node.source.value = encodeUrl(node.source.value as string, origin);
} else if (node.source.type === "Identifier") {
// this is for things that import something like
// const moduleName = "name";
// await import(moduleName);
node.source.name = `__wrapImport(${node.source.name})`;
}
},
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings
if (node.source) node.source.value = encodeUrl(node.source.value as string, origin);
},
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings
if (node.source)
node.source.value = encodeUrl(node.source.value as string, origin);
},
MemberExpression: (node: ESTree.MemberExpression) => {
if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) {
node.object.name = `$s(${node.object.name})`;
}
},
MemberExpression: (node: ESTree.MemberExpression) => {
if (
node.object.type === "Identifier" &&
identifierList.includes(node.object.name)
) {
node.object.name = `$s(${node.object.name})`;
}
},
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
if (node.left.type === "Identifier" && identifierList.includes(node.left.name)) {
node.left.name = `$s(${node.left.name})`;
}
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
if (
node.left.type === "Identifier" &&
identifierList.includes(node.left.name)
) {
node.left.name = `$s(${node.left.name})`;
}
if (node.right.type === "Identifier" && identifierList.includes(node.right.name)) {
node.right.name = `$s(${node.right.name})`;
}
},
if (
node.right.type === "Identifier" &&
identifierList.includes(node.right.name)
) {
node.right.name = `$s(${node.right.name})`;
}
},
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (node.init && node.init.type === "Identifier" && identifierList.includes(node.init.name)) {
node.init.name = `$s(${node.init.name})`;
}
}
});
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (
node.init &&
node.init.type === "Identifier" &&
identifierList.includes(node.init.name)
) {
node.init.name = `$s(${node.init.name})`;
}
},
});
customTraveler.go(ast);
customTraveler.go(ast);
return generate(ast);
} catch (e) {
console.error(e);
console.log(js);
return generate(ast);
} catch (e) {
console.error(e);
console.log(js);
return js;
}
return js;
}
}