Support output minification

This commit is contained in:
Simon Sawicki
2025-08-26 01:20:39 +02:00
parent 9d273740d2
commit 26cb550ded
2 changed files with 115 additions and 10 deletions

113
src/setup.ts Normal file
View File

@@ -0,0 +1,113 @@
import { type Node } from "@babel/types";
export const setupNodes: Node[] = [
{
type: "ExpressionStatement",
expression: {
type: "AssignmentExpression",
operator: "=",
left: {
type: "MemberExpression",
object: {
type: "Identifier",
name: "globalThis",
},
computed: false,
property: {
type: "Identifier",
name: "XMLHttpRequest",
},
},
right: {
type: "ObjectExpression",
properties: [
{
type: "ObjectProperty",
method: false,
key: {
type: "Identifier",
name: "prototype",
},
computed: false,
shorthand: false,
value: {
type: "ObjectExpression",
properties: [],
},
},
],
},
},
},
{
type: "VariableDeclaration",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "Identifier",
name: "window",
},
init: {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: {
type: "Identifier",
name: "Object",
},
computed: false,
property: {
type: "Identifier",
name: "assign",
},
},
arguments: [
{
type: "CallExpression",
callee: {
type: "MemberExpression",
object: {
type: "Identifier",
name: "Object",
},
computed: false,
property: {
type: "Identifier",
name: "create",
},
},
arguments: [
{
type: "NullLiteral",
},
],
},
{
type: "Identifier",
name: "globalThis",
},
],
},
},
],
kind: "const",
},
{
type: "VariableDeclaration",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "Identifier",
name: "document",
},
init: {
type: "ObjectExpression",
properties: [],
},
},
],
kind: "const",
},
];

View File

@@ -4,15 +4,7 @@ import { type ArrowFunctionExpression } from "@babel/types";
import { getFunctionNodes } from "./utils.ts";
import { extract as extractSig } from "./sig.ts";
import { extract as extractNsig } from "./nsig.ts";
function setup() {
// @ts-ignore: This is used in the babel generated js
globalThis.XMLHttpRequest = { prototype: {} };
// deno-lint-ignore no-unused-vars
const window = Object.assign(Object.create(null), globalThis);
// deno-lint-ignore no-unused-vars
const document = {};
}
import { setupNodes } from "./setup.ts";
export function preprocessPlayer(data: string): string {
const ast = parse(data, {
@@ -109,7 +101,7 @@ export function preprocessPlayer(data: string): string {
});
}
ast.program.body.splice(0, 0, ...getFunctionNodes(setup));
ast.program.body.splice(0, 0, ...setupNodes);
const { code } = generate(ast, {
comments: false,