-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
329 lines (278 loc) · 9.08 KB
/
build.js
File metadata and controls
329 lines (278 loc) · 9.08 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const fs = require("fs");
const esbuild = require("esbuild");
const inlineImage = require("esbuild-plugin-inline-image");
const path = require("path");
const minJS = "Main.min.js";
const minCSS = "Main.min.css";
const minHTML = "Main.min.html";
process.chdir(process.cwd() + "/WebKit/Source/WebInspectorUI/UserInterface");
async function generateEntries() {
const html = fs.readFileSync("Main.html", "utf-8");
const scriptRegex = /<script\s+src="([^"]+)"\s*>/g;
const linkRegex = /<link\s+rel="stylesheet"\s+href="([^"]+)"\s*>/g;
const jsPaths = [];
const cssPaths = [];
let match;
while ((match = scriptRegex.exec(html)) !== null) {
let file = match[1];
const basename = path.basename(file);
if (["WebInspectorUIAdditions.js"].includes(basename)) {
file = `../../../../HazyCora/injectedCode/${basename}`;
}
jsPaths.push(file);
}
jsPaths.push("External/Esprima/esprima.js");
while ((match = linkRegex.exec(html)) !== null) {
let file = match[1];
const basename = path.basename(file);
if (["WebInspectorUIAdditions.css"].includes(basename)) {
file = `../../../../HazyCora/injectedCode/${basename}`;
}
cssPaths.push(file);
}
const jsEntry = jsPaths.map((p) => `import "./${p}";`).join("\n");
const cssEntry = cssPaths.map((p) => `@import "./${p}";`).join("\n");
console.log(`✓ In-memory JS entry with ${jsPaths.length} imports`);
console.log(`✓ In-memory CSS entry with ${cssPaths.length} imports`);
return { jsEntry, cssEntry };
}
function getGlobalsPatch(source) {
var globalMembers = eslintGlobals.filter((glob) =>
source.match(new RegExp(`(?:function|class|var) ${glob}[\s ]*(?:(?:[\n\r])|(?:[=\{\(]))`, "g"))
);
return globalMembers.map((glob) => `eval('globalThis.${glob} = ${glob}');`).join("\n");
}
const fixModules = {
name: "fix-modules",
setup(build) {
build.onLoad({ filter: /.+\.js$/ }, async (args) => {
let source = await fs.promises.readFile(args.path, "utf8");
let patched = source;
basename = path.basename(args.path);
barename = path.basename(basename, path.extname(basename));
dirname = path.basename(path.dirname(args.path));
switch (basename) {
case "OrbitControls.js":
patched = `
eval('globalThis.THREE = import_three');
${source}`;
break;
case "codemirror.js":
patched = `
${source};
eval('globalThis.CodeMirror = require_codemirror()');
`;
break;
case "esprima.js":
patched = `
// ${source};
// eval('globalThis.esprima = require_esprima()');
const esprima = require('esprima-next');
eval('globalThis.esprima = esprima');
`;
break;
}
switch (dirname) {
case "Base":
case "Views":
var globals = getGlobalsPatch(source);
patched = `
${source};
${globals};
`;
break;
case "CodeMirror":
var globals = getGlobalsPatch(source);
if (globals == "") {
patched = patched.replace(
/\(function\(mod\)\s*\{[\S\s]+?\}\)\(function\(CodeMirror\)([\s\S.]+)}\);$/gm,
`(function(CodeMirror)
$1
})(globalThis.CodeMirror);`
);
}
patched = `
${patched};
${globals};`;
}
return {
contents: patched,
loader: "js",
};
});
},
};
const absolutePathFix = {
name: "resolve-abs-paths",
setup(build) {
build.onResolve({ filter: /^\/.+/ }, (args) => {
const resolvedPath = path.join(process.cwd(), args.path);
return { path: resolvedPath };
});
},
};
async function rewriteHtml(inputFile, outputFile, jsPath, cssPath) {
let html = await fs.promises.readFile(inputFile, "utf8");
const scriptBlockRegex = /^[\s\t]*<script>([\s\S]*?)<\/script>(?=[ \t]*\r?\n?)/gm;
html = html.replace(/^[\s\t]*<script\s+src="[^"]*"\s*><\/script>[\t\r\n]*/gm, "");
html = html.replace(/^[\s\t]*<link\s+rel="stylesheet"\s+href="[^"]*"\s*\/?>[\t\r\n]*/gm, "");
const injectBlock = `
<link rel="stylesheet" href="${cssPath}">
<script src="${jsPath}"></script>
`;
const injectCSS = `
<style>
.tab-bar > .tabs > .item { border-top: var(--tab-item-medium-border-style) !important }
.tab-bar > .border.top { display: block !important }
</style>
`;
[injectBlock, injectCSS].forEach((block) => {
html = html.replace(/<\/head>/, `${block}</head>`);
});
html = html.replace("; script-src ", "; script-src 'unsafe-eval' ");
while ((match = scriptBlockRegex.exec(html))) {
html = html.replace(`${match[0]}`, "");
html = html.replace(/<\/head>/, `${match[0]}\n</head>`);
}
await fs.promises.writeFile(outputFile, html);
console.log(`✓ Rewrote HTML: ${outputFile}`);
}
function getEslintGlobals(eslintPath = "../.eslintrc") {
if (!fs.existsSync(eslintPath)) return [];
const config = JSON.parse(
fs
.readFileSync(eslintPath, "utf8")
.replaceAll(/[\s\t]*(\/*[^]*\*\/[\r\n]+)|(\/\/[^\r\n]+)/gm, "")
.replaceAll(/,([\r\n\t ]+})/gm, "$1")
);
const globals = config.globals || {};
return Object.keys(globals).concat(["WebKitAdditions", "IterableWeakSet", "isWebKitInjectedScript"]);
}
function allowEslintGlobalsPlugin(globals = []) {
const globalSet = new Set(globals);
return {
name: "allow-eslint-globals",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (globalSet.has(args.path)) {
return { external: true };
}
return null;
});
},
};
}
function cssImageVarsPlugin() {
const usedVars = new Map();
const fragmentRefs = new Map();
return {
name: "css-image-vars",
setup(build) {
build.onLoad({ filter: /\.css$/ }, async (args) => {
const contents = await fs.promises.readFile(args.path, "utf8");
const urlRegex = /url\(([^)]+?\/)?([^/]+?\.(svg|png))(#[^)]+)?\)/gi;
const rewritten = contents.replace(urlRegex, (match, pathPrefix = "", filename, ext, fragment = "") => {
const extType = ext.toLowerCase();
const baseName = path.basename(filename, `.${extType}`);
const varName = `--${baseName.toLowerCase().replace(/[^a-z0-9]/gi, "-")}-${extType}`;
const fullPath = path.resolve(path.dirname(args.path), pathPrefix || "", filename);
if (fragment) {
fragmentRefs.set(varName, { path: fullPath, fragment });
} else {
usedVars.set(varName, fullPath);
}
return `var(${varName})`;
});
return { contents: rewritten, loader: "css" };
});
build.onEnd(async (result) => {
const cssOutput = result.outputFiles?.find((f) => f.path.endsWith(".css"));
if (!cssOutput) {
console.warn("⚠️ No CSS output found");
return;
}
let cssText = cssOutput.text;
const rootVars = [];
const allVars = new Map([...usedVars, ...fragmentRefs]);
for (const [varName, info] of allVars.entries()) {
const filePath = typeof info === "string" ? info : info.path;
const fragment = typeof info === "object" ? info.fragment || "" : "";
if (!fs.existsSync(filePath)) {
console.warn(`◻ Missing file: ${filePath}`);
continue;
}
const ext = path.extname(filePath).slice(1).toLowerCase();
const mimeType = ext === "svg" ? "image/svg+xml" : `image/${ext}`;
const base64 = (await fs.promises.readFile(filePath)).toString("base64");
const dataUri = `url("data:${mimeType};base64,${base64}${fragment}")`;
rootVars.push(` ${varName}: ${dataUri};`);
}
if (rootVars.length === 0) return;
const rootBlock = `:root {\n${rootVars.join("\n")}\n}\n\n`;
cssOutput.text = rootBlock + cssText;
cssOutput.contents = Buffer.from(rootBlock + cssText);
console.log(`✓ Injected ${rootVars.length} image variable(s) into in-memory CSS`);
});
},
};
}
const eslintGlobals = getEslintGlobals();
// 🚀 Main build flow
(async () => {
const { jsEntry, cssEntry } = await generateEntries();
const allowGlobalsPlugin = allowEslintGlobalsPlugin(eslintGlobals);
// JS build
await esbuild
.build({
stdin: {
contents: jsEntry,
resolveDir: process.cwd(),
loader: "js",
sourcefile: "main-entry.js",
},
bundle: true,
minify: false,
write: false,
platform: "browser",
format: "iife",
outfile: minJS,
logLevel: "error",
plugins: [inlineImage(), fixModules, allowGlobalsPlugin, absolutePathFix],
})
.then((result) => {
const outJS = result.outputFiles.find((f) => f.path.endsWith(".js"));
fs.writeFileSync(minJS, outJS.contents);
console.log(`✓ Final JS written to ${minJS}`);
});
// CSS build
await esbuild
.build({
stdin: {
contents: cssEntry,
resolveDir: process.cwd(),
loader: "css",
sourcefile: "main-entry.css",
},
bundle: true,
minify: false,
write: false,
loader: {
".css": "css",
".svg": "file",
".png": "file",
},
assetNames: "assets/[name]-[hash]",
outfile: minCSS,
logLevel: "error",
plugins: [cssImageVarsPlugin(), absolutePathFix],
})
.then((result) => {
const outCSS = result.outputFiles.find((f) => f.path.endsWith(".css"));
fs.writeFileSync(minCSS, outCSS.contents);
console.log(`✓ Final CSS written to ${minCSS}`);
});
await rewriteHtml("Main.html", minHTML, minJS, minCSS);
console.log("✓ All builds complete");
})().catch((err) => {
console.error("✕ Build failed:", err.message);
});