Skip to content

Build your personal knowledge base with Trilium Notes

build(spreadsheet): strip the data of Univer's emoji picker

Elian Doran39f541c510eb
diff --git a/apps/client/vite-plugins.mts b/apps/client/vite-plugins.mts
index 21564f975d..48077a0c18 100644
--- a/apps/client/vite-plugins.mts
+++ b/apps/client/vite-plugins.mts
@@ -51,6 +51,54 @@ function stubPath(name: string): string {
return fileURLToPath(new URL(`./src/stubs/${name}.ts`, import.meta.url));
}
+/**
+ * Empties the data `@univerjs/ui` generates for its emoji picker: the emoji table in the
+ * entry (about 200 kB) and the search index and titles in every locale (about 520 kB each
+ * in English). Only the Univer Docs ribbon opens the picker, so the spreadsheet note type
+ * never shows it. The table keeps its category keys, which `EMOJI_CATEGORIES` is built
+ * from, and `getEmojiLocaleData()` reads the search index and titles as optional.
+ * `vite-plugins.spec.ts` checks the rules still match the installed dependency.
+ */
+export function stripUniverEmojiData(): Plugin {
+ return {
+ name: "strip-univer-emoji-data",
+ enforce: "pre",
+ transform: (code, id) => stripUniverEmojiSource(code, id)
+ };
+}
+
+/**
+ * Returns `code` with the generated emoji data emptied when `id` is the `@univerjs/ui`
+ * entry or one of its locales, and `null` for every other module.
+ */
+export function stripUniverEmojiSource(code: string, id: string): string | null {
+ const path = id.replace(/\\/g, "/");
+ if (path.endsWith(UI_ENTRY)) {
+ return replaceOnce(code, EMOJI_TABLE, (_, region: string, table: string) => {
+ const categories = Object.keys(JSON.parse(table) as Record<string, unknown>);
+ return `${region}const emojis = ${JSON.stringify(Object.fromEntries(categories.map((key) => [ key, [] ])))};\n`;
+ });
+ }
+
+ if (UI_LOCALE.test(path)) {
+ return replaceOnce(code, EMOJI_LOCALE, (_, region: string) => `${region}const emojiLocale = {};\n`);
+ }
+
+ return null;
+}
+
+export const UI_ENTRY = "@univerjs/ui/lib/es/index.js";
+export const UI_LOCALE = /@univerjs\/ui\/lib\/es\/locale\/[^/]+\.js$/;
+export const EMOJI_TABLE = /(\/\/#region src\/views\/emoji-picker\/emojis\.generated\.ts\n)const emojis = (\{[\s\S]*?\n\});\n/;
+export const EMOJI_LOCALE = /(\/\/#region src\/locale\/emoji-locale\/[^\n]+\.generated\.ts\n)const emojiLocale = \{[\s\S]*?\n\};\n/;
+
+function replaceOnce(code: string, pattern: RegExp, replacer: (match: string, ...groups: string[]) => string): string {
+ if (!pattern.test(code)) {
+ throw new Error(`strip-univer-emoji-data: ${pattern} no longer matches; update vite-plugins.mts.`);
+ }
+ return code.replace(pattern, replacer);
+}
+
/** The name of the entry the share theme loads mermaid through, `src/share_mermaid.ts`. */
export const SHARE_MERMAID_ENTRY = "share_mermaid";
diff --git a/apps/client/vite-plugins.spec.ts b/apps/client/vite-plugins.spec.ts
index 6006e1ab16..7871c8ac5c 100644
--- a/apps/client/vite-plugins.spec.ts
+++ b/apps/client/vite-plugins.spec.ts
@@ -1,4 +1,4 @@
-import { readFileSync } from "fs";
+import { readdirSync, readFileSync } from "fs";
import { createRequire } from "module";
import { dirname, join } from "path";
import { describe, expect, it } from "vitest";
@@ -7,7 +7,9 @@ import {
buildShareMermaidManifest,
ENGINE_RENDER_ENTRY,
LANGUAGE_DETECTOR_PACKAGE,
- resolveUniverHyphenationStub
+ resolveUniverHyphenationStub,
+ stripUniverEmojiSource,
+ UI_ENTRY
} from "./vite-plugins.mjs";
const entryPath = createRequire(import.meta.url).resolve("@univerjs/engine-render/lib/es/index.js");
@@ -75,6 +77,50 @@ describe("stripUniverHyphenation", () => {
});
});
+/**
+ * Canary for `stripUniverEmojiData`. An upgrade that renames the generated regions makes
+ * the plugin throw during the build; one that moves the data elsewhere, or starts reading
+ * it outside the picker, makes the plugin a no-op or breaks live code.
+ */
+describe("stripUniverEmojiData", () => {
+ const uiEntryPath = createRequire(import.meta.url).resolve("@univerjs/ui/lib/es/index.js");
+ const uiEntry = readFileSync(uiEntryPath, "utf8");
+ const localeDir = join(dirname(uiEntryPath), "locale");
+
+ it("empties the emoji table in the entry, keeping its categories", () => {
+ expect(uiEntryPath.replace(/\\/g, "/")).toContain(UI_ENTRY);
+ const stripped = stripUniverEmojiSource(uiEntry, uiEntryPath);
+
+ expect(stripped).toContain(`const emojis = {"frequent":[],"people":[]`);
+ expect(stripped?.length).toBeLessThan(uiEntry.length - 200_000);
+ // The picker reads the locale data through this guard, so an absent index is not an error.
+ expect(uiEntry).toContain("if (!emojiPicker || typeof emojiPicker !== \"object\" || Array.isArray(emojiPicker)) return {};");
+ expect(uiEntry.match(/\bemojis\b/g)?.length).toBe(stripped?.match(/\bemojis\b/g)?.length);
+ });
+
+ it("empties the search index and titles of every locale, leaving its other strings", () => {
+ const locales = readdirSync(localeDir).filter((file) => file.endsWith(".js"));
+ expect(locales.length).toBeGreaterThan(10);
+
+ for (const file of locales) {
+ const path = join(localeDir, file).replace(/\\/g, "/");
+ const source = readFileSync(path, "utf8");
+ const stripped = stripUniverEmojiSource(source, path);
+
+ expect(stripped).toContain("const emojiLocale = {};");
+ expect(stripped).not.toContain("emojiSearchIndex");
+ expect(stripped).not.toContain("emojiTitles");
+ expect(stripped).toContain("...emojiLocale");
+ expect(stripped).toContain("clearFormatting:");
+ }
+ });
+
+ it("leaves every other module alone and fails loudly when the shape changes", () => {
+ expect(stripUniverEmojiSource(uiEntry, "/app/src/services/froca.ts")).toBeNull();
+ expect(() => stripUniverEmojiSource("export {};", uiEntryPath)).toThrow("no longer matches");
+ });
+});
+
describe("buildShareMermaidManifest", () => {
const chunk = (fileName: string, extra: { name?: string; isEntry?: boolean; imports?: string[];
dynamicImports?: string[]; css?: string[] } = {}) => ({
diff --git a/apps/client/vite.config.mts b/apps/client/vite.config.mts
index 10ebd41cfb..fbe0b0c216 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 { shareMermaidManifest, stripUniverHyphenation } from './vite-plugins.mjs';
+import { shareMermaidManifest, stripUniverEmojiData, stripUniverHyphenation } from './vite-plugins.mjs';
const assets = [ "assets", "stylesheets", "fonts", "translations" ];
@@ -18,11 +18,13 @@ if (isDev) {
// a view with thousands of instances of one component slows to a stop. Set TRILIUM_NO_HMR to
// work on such a view; components then reload with the page instead of in place.
...(process.env.TRILIUM_NO_HMR ? [] : [ prefresh() ]),
- stripUniverHyphenation()
+ stripUniverHyphenation(),
+ stripUniverEmojiData()
];
} else {
plugins = [
stripUniverHyphenation(),
+ stripUniverEmojiData(),
shareMermaidManifest("src/share_mermaid.json"),
viteStaticCopy({
targets: assets.map((asset) => ({
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index 2384697258..25ccaffa2a 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 { shareMermaidManifest, stripUniverHyphenation } from "../client/vite-plugins.mjs";
+import { shareMermaidManifest, stripUniverEmojiData, stripUniverHyphenation } from "../client/vite-plugins.mjs";
const clientAssets = ["assets", "stylesheets", "fonts", "translations"];
@@ -158,6 +158,7 @@ const sqliteWasmPlugin = viteStaticCopy({
let plugins: any = [
stripUniverHyphenation(),
+ stripUniverEmojiData(),
sqliteWasmDedupePlugin(),
sqliteWasmPlugin,
shareThemeAssetListPlugin(),