File size: 2,917 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { config, flagEnabled } from "..";
import { URLMeta } from "./url";

import { getRewriter, JsRewriterOutput, textDecoder } from "./wasm";

Error.stackTraceLimit = 50;

function rewriteJsWasm(

	input: string | Uint8Array,

	source: string | null,

	meta: URLMeta,

	module: boolean

): { js: string | Uint8Array; map: Uint8Array | null; tag: string } {
	let [rewriter, ret] = getRewriter(meta);

	try {
		let out: JsRewriterOutput;
		const before = performance.now();
		try {
			if (typeof input === "string") {
				out = rewriter.rewrite_js(
					input,
					meta.base.href,
					source || "(unknown)",
					module
				);
			} else {
				out = rewriter.rewrite_js_bytes(
					input,
					meta.base.href,
					source || "(unknown)",
					module
				);
			}
		} catch (err) {
			const err1 = err as Error;
			console.warn(
				"failed rewriting js for",
				source,
				err1.message,
				input instanceof Uint8Array ? textDecoder.decode(input) : input
			);

			return { js: input, tag: "", map: null };
		}
		dbg.time(meta, before, `oxc rewrite for "${source || "(unknown)"}"`);

		let { js, map, scramtag, errors } = out;

		if (flagEnabled("sourcemaps", meta.base) && !globalThis.clients) {
			globalThis[config.globals.pushsourcemapfn](Array.from(map), scramtag);

			map = null;
		}

		if (flagEnabled("rewriterLogs", meta.base)) {
			for (const error of errors) {
				console.error("oxc parse error", error);
			}
		}

		return {
			js: typeof input === "string" ? textDecoder.decode(js) : js,
			tag: scramtag,
			map,
		};
	} finally {
		ret();
	}
}

// 1. does not work with modules
// 2. cannot proxy import()
// 3. disables "use strict" optimizations
// 4. i think the global state can get clobbered somehow
//
// if you can ensure all the preconditions are met this is faster than full rewrites
function rewriteJsNaiive(js: string | ArrayBuffer) {
	if (typeof js !== "string") {
		js = textDecoder.decode(js);
	}

	return `

		with (${config.globals.wrapfn}(globalThis)) {



			${js}



		}

	`;
}

function rewriteJsInner(

	js: string | Uint8Array,

	url: string | null,

	meta: URLMeta,

	module = false

) {
	if (flagEnabled("naiiveRewriter", meta.origin)) {
		const text = typeof js === "string" ? js : new TextDecoder().decode(js);
		let out: any = rewriteJsNaiive(text);
		if (typeof js === "string") out = out;
		else out = new TextEncoder().encode(out);

		return { js: out, tag: "", map: null };
	}

	return rewriteJsWasm(js, url, meta, module);
}

export function rewriteJs(

	js: string | Uint8Array,

	url: string | null,

	meta: URLMeta,

	module = false

) {
	return rewriteJsInner(js, url, meta, module).js;
}

export function rewriteJsWithMap(

	js: string | Uint8Array,

	url: string | null,

	meta: URLMeta,

	module = false

) {
	return rewriteJsInner(js, url, meta, module);
}