Skip to content

Build your personal knowledge base with Trilium Notes

perf(spreadsheet): drop Univer's hyphenation pattern tables from the bundle

Elian Doran84e1d5fc30c9
diff --git a/apps/client/src/stubs/univer_hyphenation_pattern.ts b/apps/client/src/stubs/univer_hyphenation_pattern.ts
new file mode 100644
index 0000000000..c1d52ebd16
--- /dev/null
+++ b/apps/client/src/stubs/univer_hyphenation_pattern.ts
@@ -0,0 +1,12 @@
+/**
+ * Stand-in for the 77 TeX hyphenation tables `@univerjs/engine-render` ships under
+ * `components/docs/layout/hyphenation/patterns/`. Only `Hyphen.loadPattern()` imports
+ * them, and `shaping()` calls it just for a paragraph whose section sets
+ * `autoHyphenation` — a Univer Docs setting the spreadsheet note type never turns on.
+ * `loadPattern()` reads the table off the namespace under its Pascal-cased locale name
+ * and returns early when it is missing, so an empty module disables hyphenation instead
+ * of breaking layout. See `stripUniverHyphenationPatterns` in vite-plugins.mts.
+ */
+const noPatterns: Record<string, unknown> = {};
+
+export default noPatterns;
diff --git a/apps/client/tsconfig.spec.json b/apps/client/tsconfig.spec.json
index ba1f22c11e..c3228df4ec 100644
--- a/apps/client/tsconfig.spec.json
+++ b/apps/client/tsconfig.spec.json
@@ -14,6 +14,8 @@
"include": [
"vite.config.ts",
"vite.config.mts",
+ "vite-plugins.mts",
+ "vite-plugins.spec.ts",
"vitest.config.ts",
"vitest.config.mts",
"src/**/*.test.ts",
diff --git a/apps/client/vite-plugins.mts b/apps/client/vite-plugins.mts
new file mode 100644
index 0000000000..d24637ab57
--- /dev/null
+++ b/apps/client/vite-plugins.mts
@@ -0,0 +1,36 @@
+import type { Plugin } from "vite";
+
+/**
+ * Drops the TeX hyphenation tables `@univerjs/engine-render` lazy-loads. Its bundled
+ * `index.js` has exactly 77 relative imports and every one of them is a pattern table
+ * (4.4 MB of chunks, Hungarian alone 747 kB); resolving them all to one empty module
+ * collapses them into a single stub chunk. `Hyphen` reaches a table only for a
+ * paragraph whose section sets `autoHyphenation`, a Univer Docs setting the
+ * spreadsheet note type never turns on.
+ *
+ * Both the client and the standalone build bundle Univer, so both apply this.
+ * `vite-plugins.spec.ts` checks the rule still matches the installed dependency.
+ */
+export function stripUniverHyphenationPatterns(): Plugin {
+ return {
+ name: "strip-univer-hyphenation-patterns",
+ enforce: "pre",
+ resolveId: resolveUniverHyphenationPattern
+ };
+}
+
+/**
+ * Returns the stub for a relative import made by `@univerjs/engine-render`'s entry,
+ * and `null` for everything else so other resolvers keep their turn.
+ */
+export function resolveUniverHyphenationPattern(source: string, importer: string | undefined): string | null {
+ if (!importer?.replace(/\\/g, "/").endsWith(ENGINE_RENDER_ENTRY)) {
+ return null;
+ }
+
+ return source.startsWith("./") ? HYPHENATION_STUB : null;
+}
+
+export const ENGINE_RENDER_ENTRY = "@univerjs/engine-render/lib/es/index.js";
+
+const HYPHENATION_STUB = new URL("./src/stubs/univer_hyphenation_pattern.ts", import.meta.url).pathname;
diff --git a/apps/client/vite-plugins.spec.ts b/apps/client/vite-plugins.spec.ts
new file mode 100644
index 0000000000..ea0e9515c5
--- /dev/null
+++ b/apps/client/vite-plugins.spec.ts
@@ -0,0 +1,70 @@
+import { readFileSync } from "fs";
+import { createRequire } from "module";
+import { dirname, join } from "path";
+import { describe, expect, it } from "vitest";
+
+import { ENGINE_RENDER_ENTRY, resolveUniverHyphenationPattern } from "./vite-plugins.mjs";
+
+const entryPath = createRequire(import.meta.url).resolve("@univerjs/engine-render/lib/es/index.js");
+const entrySource = readFileSync(entryPath, "utf8");
+
+/**
+ * Canary for `stripUniverHyphenationPatterns`. The rule it applies — every relative
+ * import made by `@univerjs/engine-render`'s entry is a hyphenation table — holds for
+ * the installed version, but an upgrade could rename the entry, inline the tables, or
+ * start importing a real sibling module. The first two turn the plugin into a no-op
+ * that silently puts 4.4 MB back into the build; the third makes it stub out live code.
+ */
+describe("stripUniverHyphenationPatterns", () => {
+ it("matches an entry that still lazy-loads a pattern table per locale", () => {
+ expect(entryPath.replace(/\\/g, "/")).toContain(ENGINE_RENDER_ENTRY);
+ expect(patternLoaders().length).toBeGreaterThan(50);
+ });
+
+ it("finds nothing relative in the entry beyond the tables, so the blanket rule stays safe", () => {
+ const relativeImports = [...entrySource.matchAll(/\bimport\("(\.[^"]*)"\)/g)].map(([, source]) => source);
+
+ expect(new Set(relativeImports)).toEqual(new Set(patternLoaders().map(({ source }) => source)));
+ expect([...entrySource.matchAll(/\bfrom\s*"(\.[^"]*)"/g)]).toHaveLength(0);
+ });
+
+ it("intercepts every table the loader map points at", () => {
+ for (const { locale, source } of patternLoaders()) {
+ expect(resolveUniverHyphenationPattern(source, entryPath)).toContain("univer_hyphenation_pattern");
+
+ // Each table exports its locale Pascal-cased (`de-ch-1901` -> `DeCh1901`),
+ // which is also how `Hyphen.loadPattern()` reads it back off the namespace.
+ const table = readFileSync(join(dirname(entryPath), source), "utf8");
+ expect(table).toContain(`export { ${pascalCaseLocale(locale)} }`);
+ }
+ });
+
+ it("declines a bare specifier and any importer outside the entry", () => {
+ expect(resolveUniverHyphenationPattern("rxjs", entryPath)).toBeNull();
+ expect(resolveUniverHyphenationPattern("./hu-DVk7Y_ka.js", "/app/src/services/froca.ts")).toBeNull();
+ expect(resolveUniverHyphenationPattern("./hu-DVk7Y_ka.js", undefined)).toBeNull();
+ });
+
+ it("stubs a module that the pattern loader reads as an absent table", async () => {
+ // `loadPattern()` takes the table off the namespace under its Pascal-cased locale
+ // and returns early when it is missing, so nothing throws and `hasPattern()` stays
+ // false — the hyphenating line breaker is never built.
+ const stub: Record<string, unknown> = await import("./src/stubs/univer_hyphenation_pattern.js");
+
+ expect(Array.isArray(stub)).toBe(false);
+ for (const { locale } of patternLoaders()) {
+ expect(stub[pascalCaseLocale(locale)]).toBeUndefined();
+ }
+ });
+});
+
+/** Reads the generated `PATTERN_LOADERS` map, whose keys are the locales Univer hyphenates. */
+function patternLoaders(): { locale: string; source: string }[] {
+ const entries = entrySource.matchAll(/\["([a-z0-9-]+)"\]\s*:\s*\(\)\s*=>\s*import\("(\.[^"]*)"\)/g);
+
+ return [...entries].map(([, locale, source]) => ({ locale, source }));
+}
+
+function pascalCaseLocale(locale: string): string {
+ return locale.split("-").filter(Boolean).map((part) => part[0].toUpperCase() + part.slice(1)).join("");
+}
diff --git a/apps/client/vite.config.mts b/apps/client/vite.config.mts
index 5175a11777..b087bc7a90 100644
--- a/apps/client/vite.config.mts
+++ b/apps/client/vite.config.mts
@@ -5,6 +5,8 @@ import { join } from 'path';
import { defineConfig } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy'
+import { stripUniverHyphenationPatterns } from './vite-plugins.mjs';
+
const assets = [ "assets", "stylesheets", "fonts", "translations" ];
const isDev = process.env.NODE_ENV === "development";
@@ -13,10 +15,12 @@ let plugins: any = [];
if (isDev) {
// Add Prefresh for Preact HMR in development
plugins = [
- prefresh()
+ prefresh(),
+ stripUniverHyphenationPatterns()
];
} else {
plugins = [
+ stripUniverHyphenationPatterns(),
viteStaticCopy({
targets: assets.map((asset) => ({
src: `src/${asset}/**/*`,
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index bab636e230..1d7ededc6e 100644
--- a/apps/standalone/vite.config.mts
+++ b/apps/standalone/vite.config.mts
@@ -6,6 +6,8 @@ import prefresh from "@prefresh/vite";
import { defineConfig, type Plugin } from "vite";
import { viteStaticCopy } from "vite-plugin-static-copy";
+import { stripUniverHyphenationPatterns } from "../client/vite-plugins.mjs";
+
const clientAssets = ["assets", "stylesheets", "fonts", "translations"];
const isDev = process.env.NODE_ENV === "development";
@@ -111,6 +113,7 @@ const sqliteWasmPlugin = viteStaticCopy({
});
let plugins: any = [
+ stripUniverHyphenationPatterns(),
sqliteWasmDedupePlugin(),
sqliteWasmPlugin,
viteStaticCopy({