Skip to content

Build your personal knowledge base with Trilium Notes

perf(spreadsheet): stub Univer's language detector out of the bundle

Elian Doran2819da59483c
diff --git a/apps/client/src/stubs/franc_min.ts b/apps/client/src/stubs/franc_min.ts
new file mode 100644
index 0000000000..88cacd9d75
--- /dev/null
+++ b/apps/client/src/stubs/franc_min.ts
@@ -0,0 +1,11 @@
+/**
+ * Stand-in for `franc-min`, whose 102 kB trigram model is loaded by one caller in the
+ * tree: `@univerjs/engine-render`'s `LanguageDetector`. `shaping()` runs the detector on
+ * every paragraph it lays out, ahead of the check that decides whether to hyphenate at
+ * all, so a spreadsheet pays for detection it cannot use. Returning `und` takes the
+ * `LANG_MAP_TO_HYPHEN_LANG` entry that resolves to `unknown`, the value that already
+ * makes `shaping()` skip hyphenation. See `stripUniverHyphenation` in vite-plugins.mts.
+ */
+export function franc(): string {
+ return "und";
+}
diff --git a/apps/client/vite-plugins.mts b/apps/client/vite-plugins.mts
index d24637ab57..6d47dabcb3 100644
--- a/apps/client/vite-plugins.mts
+++ b/apps/client/vite-plugins.mts
@@ -1,36 +1,48 @@
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.
+ * Drops the hyphenation machinery `@univerjs/engine-render` carries for Univer Docs.
+ * `shaping()` hyphenates only a paragraph whose section sets `autoHyphenation`, which
+ * the spreadsheet note type never turns on, so none of it can run:
*
- * 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.
+ * - The 77 TeX pattern tables, 4.4 MB of lazy chunks (Hungarian alone 747 kB). The
+ * entry makes exactly 77 relative imports and every one is a table, so resolving
+ * them all to one empty module collapses them into a single stub chunk.
+ * - `franc-min`, whose 102 kB trigram model backs the `LanguageDetector` call that
+ * `shaping()` makes ahead of the hyphenation check, on every paragraph it lays out.
+ *
+ * Both stubs are keyed on the importer, so a second consumer of `franc-min` would still
+ * get the real package. Both the client and the standalone build bundle Univer, so both
+ * apply this. `vite-plugins.spec.ts` checks the rules still match the installed
+ * dependency.
*/
-export function stripUniverHyphenationPatterns(): Plugin {
+export function stripUniverHyphenation(): Plugin {
return {
- name: "strip-univer-hyphenation-patterns",
+ name: "strip-univer-hyphenation",
enforce: "pre",
- resolveId: resolveUniverHyphenationPattern
+ resolveId: resolveUniverHyphenationStub
};
}
/**
- * Returns the stub for a relative import made by `@univerjs/engine-render`'s entry,
+ * Returns the stub for a hyphenation 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 {
+export function resolveUniverHyphenationStub(source: string, importer: string | undefined): string | null {
if (!importer?.replace(/\\/g, "/").endsWith(ENGINE_RENDER_ENTRY)) {
return null;
}
- return source.startsWith("./") ? HYPHENATION_STUB : null;
+ if (source === LANGUAGE_DETECTOR_PACKAGE) {
+ return stubPath("franc_min");
+ }
+
+ return source.startsWith("./") ? stubPath("univer_hyphenation_pattern") : null;
}
export const ENGINE_RENDER_ENTRY = "@univerjs/engine-render/lib/es/index.js";
+export const LANGUAGE_DETECTOR_PACKAGE = "franc-min";
-const HYPHENATION_STUB = new URL("./src/stubs/univer_hyphenation_pattern.ts", import.meta.url).pathname;
+function stubPath(name: string): string {
+ return new URL(`./src/stubs/${name}.ts`, import.meta.url).pathname;
+}
diff --git a/apps/client/vite-plugins.spec.ts b/apps/client/vite-plugins.spec.ts
index ea0e9515c5..8256ac2ded 100644
--- a/apps/client/vite-plugins.spec.ts
+++ b/apps/client/vite-plugins.spec.ts
@@ -3,19 +3,19 @@ import { createRequire } from "module";
import { dirname, join } from "path";
import { describe, expect, it } from "vitest";
-import { ENGINE_RENDER_ENTRY, resolveUniverHyphenationPattern } from "./vite-plugins.mjs";
+import { ENGINE_RENDER_ENTRY, LANGUAGE_DETECTOR_PACKAGE, resolveUniverHyphenationStub } 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.
+ * Canary for `stripUniverHyphenation`. The rules it applies hold for the installed
+ * version, but an upgrade could rename the entry, inline the tables, start importing a
+ * real sibling module, or reach for `franc-min` somewhere the stub does not cover. The
+ * first two turn the plugin into a no-op that silently puts 4.4 MB back into the build;
+ * the others make it stub out live code.
*/
-describe("stripUniverHyphenationPatterns", () => {
+describe("stripUniverHyphenation", () => {
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);
@@ -30,7 +30,7 @@ describe("stripUniverHyphenationPatterns", () => {
it("intercepts every table the loader map points at", () => {
for (const { locale, source } of patternLoaders()) {
- expect(resolveUniverHyphenationPattern(source, entryPath)).toContain("univer_hyphenation_pattern");
+ expect(resolveUniverHyphenationStub(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.
@@ -39,22 +39,34 @@ describe("stripUniverHyphenationPatterns", () => {
}
});
+ it("intercepts the language detector the entry imports, taking only what it uses", () => {
+ expect(entrySource).toContain(`import { franc } from "${LANGUAGE_DETECTOR_PACKAGE}"`);
+ expect(resolveUniverHyphenationStub(LANGUAGE_DETECTOR_PACKAGE, entryPath)).toContain("franc_min");
+
+ // `detect()` maps franc's code through this table, so the stub's `und` has to
+ // land on `unknown` — the value that makes `shaping()` skip hyphenation.
+ expect(entrySource).toMatch(/\bund:\s*"unknown"/);
+ });
+
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();
+ expect(resolveUniverHyphenationStub("rxjs", entryPath)).toBeNull();
+ expect(resolveUniverHyphenationStub(LANGUAGE_DETECTOR_PACKAGE, "/app/src/services/froca.ts")).toBeNull();
+ expect(resolveUniverHyphenationStub("./hu-DVk7Y_ka.js", "/app/src/services/froca.ts")).toBeNull();
+ expect(resolveUniverHyphenationStub("./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
+ it("stubs modules that the hyphenation code reads as absent", async () => {
+ // `loadPattern()` takes a 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);
+ const patterns: Record<string, unknown> = await import("./src/stubs/univer_hyphenation_pattern.js");
+ expect(Array.isArray(patterns)).toBe(false);
for (const { locale } of patternLoaders()) {
- expect(stub[pascalCaseLocale(locale)]).toBeUndefined();
+ expect(patterns[pascalCaseLocale(locale)]).toBeUndefined();
}
+
+ const { franc } = await import("./src/stubs/franc_min.js");
+ expect(franc()).toBe("und");
});
});
diff --git a/apps/client/vite.config.mts b/apps/client/vite.config.mts
index ec9dbdb91b..52ecf45853 100644
--- a/apps/client/vite.config.mts
+++ b/apps/client/vite.config.mts
@@ -5,7 +5,7 @@ import { join } from 'path';
import { defineConfig } from 'vite';
import { viteStaticCopy } from 'vite-plugin-static-copy'
-import { stripUniverHyphenationPatterns } from './vite-plugins.mjs';
+import { stripUniverHyphenation } from './vite-plugins.mjs';
const assets = [ "assets", "stylesheets", "fonts", "translations" ];
@@ -16,11 +16,11 @@ if (isDev) {
// Add Prefresh for Preact HMR in development
plugins = [
prefresh(),
- stripUniverHyphenationPatterns()
+ stripUniverHyphenation()
];
} else {
plugins = [
- stripUniverHyphenationPatterns(),
+ stripUniverHyphenation(),
viteStaticCopy({
targets: assets.map((asset) => ({
src: `src/${asset}/**/*`,
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index 1d7ededc6e..12053e4620 100644
--- a/apps/standalone/vite.config.mts
+++ b/apps/standalone/vite.config.mts
@@ -6,7 +6,7 @@ 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";
+import { stripUniverHyphenation } from "../client/vite-plugins.mjs";
const clientAssets = ["assets", "stylesheets", "fonts", "translations"];
@@ -113,7 +113,7 @@ const sqliteWasmPlugin = viteStaticCopy({
});
let plugins: any = [
- stripUniverHyphenationPatterns(),
+ stripUniverHyphenation(),
sqliteWasmDedupePlugin(),
sqliteWasmPlugin,
viteStaticCopy({