lemmy-ui/src/shared/dynamic-imports.ts
matc-pub 201e5fcd53
Lazy loading less common languages for syntax highlighting (#2388)
* Use fewer syntax highlighter languages.

Reduces client.js size by about 250kB (800kB uncompressed)

Common languages:
bash, c, cpp, csharp, css, diff, go, graphql, ini, java, javascript,
json, kotlin, less, lua, makefile, markdown, objectivec, perl,
php-template, php, plaintext, python-repl, python, r, ruby, rust, scss,
shell, sql, swift, typescript, vbnet, wasm, xml, yaml

Additionally enabled languages:
dockerfile, pgsql

* Configurable syntax highlighter languages

Allows to individually enable languages.

* Lazy load syntax highlighter languages

Allows to enable additional languages that will not be autodetected.

* Include highlight.js in dynamic import check
2024-03-14 08:31:07 -04:00

58 lines
1.8 KiB
TypeScript

import { verifyTranslationImports } from "./services/I18NextService";
import { verifyDateFnsImports } from "@utils/app/setup-date-fns";
import { verifyHighlighjsImports } from "./lazy-highlightjs";
export class ImportReport {
error: Array<{ id: string; error: Error | string | undefined }> = [];
success: string[] = [];
message?: string;
}
export type ImportReportCollection = {
translation?: ImportReport;
"date-fns"?: ImportReport;
"highlight.js"?: ImportReport;
};
function collect(
verbose: boolean,
kind: keyof ImportReportCollection,
collection: ImportReportCollection,
report: ImportReport,
) {
collection[kind] = report;
if (verbose) {
for (const { id, error } of report.error) {
console.warn(`${kind} "${id}" failed: ${error}`);
}
const message = report.message ? ` (${report.message})` : "";
const good = report.success.length;
const bad = report.error.length;
if (bad) {
console.error(
`${bad} out of ${bad + good} ${kind} imports failed.` + message,
);
} else {
console.log(`${good} ${kind} imports verified.` + message);
}
}
}
// This verifies that the parameters used for parameterized imports are
// correct, that the respective chunks are reachable or bundled, and that the
// returned objects match expectations.
export async function verifyDynamicImports(
verbose: boolean,
): Promise<ImportReportCollection> {
const collection: ImportReportCollection = {};
await verifyTranslationImports().then(report =>
collect(verbose, "translation", collection, report),
);
await verifyDateFnsImports().then(report =>
collect(verbose, "date-fns", collection, report),
);
await verifyHighlighjsImports().then(report =>
collect(verbose, "highlight.js", collection, report),
);
return collection;
}