mirror of
https://github.com/yt-dlp/ejs.git
synced 2026-06-13 08:42:29 +00:00
Support output minification
This commit is contained in:
113
src/setup.ts
Normal file
113
src/setup.ts
Normal 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",
|
||||
},
|
||||
];
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user