Optimize bundle size + chunked builds + JIT loading

* Replaced AJV with other JSON schema library
* Turndown and Vim mode (very large) now only loaded when used
* Chunked builds with ESBuild
This commit is contained in:
Zef Hemel
2026-03-11 10:04:13 +01:00
parent 81408ed5cf
commit a080ee3b15
13 changed files with 352 additions and 400 deletions
+12 -3
View File
@@ -18,7 +18,7 @@ import {
} from "@codemirror/commands";
import type { Transaction } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim";
import { getVimModule } from "../../vim_loader.ts";
import type { SysCallMapping } from "../system.ts";
import type {
FilterOption,
@@ -497,14 +497,23 @@ export function editorSyscalls(client: Client): SysCallMapping {
void client.reloadEditor();
},
"editor.vimEx": (_ctx, exCommand: string) => {
const cm = vimGetCm(client.editorView);
const vimMod = getVimModule();
if (!vimMod) {
throw new Error("Vim module not loaded.");
}
const cm = vimMod.getCM(client.editorView);
if (cm?.state.vim) {
return Vim.handleEx(cm as any, exCommand);
return vimMod.Vim.handleEx(cm as any, exCommand);
} else {
throw new Error("Vim mode not active or not initialized.");
}
},
"editor.configureVimMode": () => {
const vimMod = getVimModule();
if (!vimMod) {
throw new Error("Vim module not loaded.");
}
const { Vim } = vimMod;
// Override the default "o" binding to be more intelligent and follow the markdown editor's behavior
Vim.mapCommand("o", "action", "newline-continue-markup", {}, {});
Vim.mapCommand("O", "action", "back-newline-continue-markup", {}, {});
+69 -29
View File
@@ -1,40 +1,59 @@
import type { SysCallMapping } from "../system.ts";
import { Ajv, type ValidateFunction } from "ajv";
import { type OutputUnit, Validator, format } from "@cfworker/json-schema";
const ajv = new Ajv();
// Register custom formats
format["email"] = (data: string) => data.includes("@");
format["page-ref"] = (data: string) =>
data.startsWith("[[") && data.endsWith("]]");
ajv.addFormat("email", {
validate: (data: string) => {
// TODO: Implement email validation
return data.includes("@");
},
async: false,
});
const schemaCache = new Map<string, Validator>();
ajv.addFormat("page-ref", {
validate: (data: string) => {
return data.startsWith("[[") && data.endsWith("]]");
},
async: false,
});
/**
* Deep-clone a value, replacing any functions with null.
* JSON schema can't validate functions, so we strip them before validation.
*/
function stripFunctions(value: any): any {
if (typeof value === "function") return null;
if (value === null || value === undefined || typeof value !== "object") {
return value;
}
if (Array.isArray(value)) {
return value.map(stripFunctions);
}
const result: Record<string, any> = {};
for (const key of Object.keys(value)) {
result[key] = stripFunctions(value[key]);
}
return result;
}
const schemaCache = new Map<string, ValidateFunction>();
function formatErrors(errors: OutputUnit[]): string {
// Filter out "properties" wrapper errors, keep only the specific leaf errors
const leafErrors = errors.filter((e) => e.keyword !== "properties");
const errorsToUse = leafErrors.length > 0 ? leafErrors : errors;
return errorsToUse.map((e) => {
// Convert instanceLocation from "#/foo/bar" to "foo.bar"
const path = e.instanceLocation === "#"
? ""
: e.instanceLocation.slice(2).replaceAll("/", ".");
return path ? `${path}: ${e.error}` : e.error;
}).join(", ");
}
export function validateObject(schema: any, object: any): undefined | string {
try {
const schemaKey = JSON.stringify(schema);
if (!schemaCache.has(schemaKey)) {
const validate = ajv.compile(schema);
schemaCache.set(schemaKey, validate);
const validator = new Validator(schema, "7");
schemaCache.set(schemaKey, validator);
}
const validate = schemaCache.get(schemaKey)!;
if (validate(object)) {
const validator = schemaCache.get(schemaKey)!;
const result = validator.validate(stripFunctions(object));
if (result.valid) {
return;
} else {
let text = ajv.errorsText(validate.errors);
text = text.replaceAll("/", ".");
text = text.replace(/^data[.\s]/, "");
return text;
return formatErrors(result.errors);
}
} catch (e: any) {
return e.message;
@@ -42,12 +61,33 @@ export function validateObject(schema: any, object: any): undefined | string {
}
export function validateSchema(schema: any): undefined | string {
const valid = ajv.validateSchema(schema);
if (valid) {
return;
} else {
return ajv.errorsText(ajv.errors);
if (schema === null || schema === undefined) {
return "schema must not be null or undefined";
}
if (typeof schema === "boolean") {
return;
}
if (typeof schema !== "object" || Array.isArray(schema)) {
return "schema must be an object or boolean";
}
if (schema.type !== undefined) {
const validTypes = [
"string",
"number",
"integer",
"boolean",
"object",
"array",
"null",
];
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
for (const t of types) {
if (!validTypes.includes(t)) {
return `schema.type must be one of ${validTypes.join(", ")}`;
}
}
}
return;
}
export function jsonschemaSyscalls(): SysCallMapping {