Skip to content

Build your personal knowledge base with Trilium Notes

build(standalone): bundle officeparser without its 2.7 MB browser monolith

Elian Doran5f85ddd77aa3
diff --git a/apps/standalone/package.json b/apps/standalone/package.json
index 37d66a64ea..d9c32d265b 100644
--- a/apps/standalone/package.json
+++ b/apps/standalone/package.json
@@ -36,6 +36,7 @@
"@triliumnext/split.js": "workspace:*",
"autocomplete.js": "0.38.1",
"bootstrap": "5.3.8",
+ "buffer": "6.0.3",
"boxicons": "2.1.4",
"clsx": "2.1.1",
"color": "5.0.3",
diff --git a/apps/standalone/src/office_preview.spec.ts b/apps/standalone/src/office_preview.spec.ts
new file mode 100644
index 0000000000..f0156ee580
--- /dev/null
+++ b/apps/standalone/src/office_preview.spec.ts
@@ -0,0 +1,39 @@
+import { convertOfficeToHtml } from "@triliumnext/core/src/services/office_preview.js";
+import { readFileSync } from "fs";
+import { fileURLToPath } from "url";
+import { describe, expect, it } from "vitest";
+
+/**
+ * Canary for the officeparser aliases in vite.config.mts: the standalone build bypasses
+ * the package's prebuilt `browser` entry and bundles `officeparser/dist/index.mjs`
+ * directly (with pdfjs-dist/tesseract.js stubbed out). That reaches into officeparser's
+ * dist/ internals, so an upstream upgrade could silently break the alias target or the
+ * stubbed-out code paths. This spec resolves officeparser through the same aliases, so
+ * a breaking upgrade fails here instead of at runtime in the browser.
+ */
+describe("office preview with aliased officeparser", () => {
+ it("converts a DOCX fixture to HTML through the slim entry", async () => {
+ const docx = readFixture("demo.docx");
+ const html = await convertOfficeToHtml(docx, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
+
+ expect(html).toContain("Welcome to Trilium Notes!");
+ // An embeddable fragment, not a standalone document.
+ expect(html).not.toContain("<html");
+ });
+
+ it("converts an XLSX spreadsheet fixture to HTML", async () => {
+ const xlsx = readFixture("demo.xlsx");
+ const html = await convertOfficeToHtml(xlsx, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+
+ expect(html).toContain("<table");
+ });
+});
+
+function readFixture(name: string): Uint8Array {
+ // Vitest serves transformed modules from Vite's virtual /@fs/ root — strip it
+ // to get a real filesystem path (no-op when the prefix is absent).
+ const path = fileURLToPath(new URL(`../../server/src/services/ocr/processors/samples/${name}`, import.meta.url))
+ .replace(/^[/\\]@fs/, "");
+
+ return new Uint8Array(readFileSync(path));
+}
diff --git a/apps/standalone/src/stubs/buffer_polyfill.ts b/apps/standalone/src/stubs/buffer_polyfill.ts
new file mode 100644
index 0000000000..9bb0024063
--- /dev/null
+++ b/apps/standalone/src/stubs/buffer_polyfill.ts
@@ -0,0 +1,13 @@
+/**
+ * Installs the npm `buffer` polyfill as the global Buffer for officeparser, which
+ * uses Buffer throughout its conversion path. Its own prebuilt browser bundle ships
+ * this same polyfill; since we bundle from the package's Node entry instead (see the
+ * officeparser alias in vite.config.mts), we have to provide it ourselves. The
+ * trailing slash on "buffer/" forces resolution to the npm package rather than the
+ * Node built-in.
+ */
+import { Buffer } from "buffer/";
+
+if (!("Buffer" in globalThis)) {
+ Object.assign(globalThis, { Buffer });
+}
diff --git a/apps/standalone/src/stubs/empty.ts b/apps/standalone/src/stubs/empty.ts
new file mode 100644
index 0000000000..54a364fa9d
--- /dev/null
+++ b/apps/standalone/src/stubs/empty.ts
@@ -0,0 +1,10 @@
+/**
+ * Empty stand-in for heavy optional dependencies of officeparser (pdfjs-dist,
+ * tesseract.js, puppeteer). officeparser only reaches them through dynamic imports
+ * on code paths that office-format conversion never hits (PDF parsing, OCR, PDF
+ * generation — PDFs have a dedicated viewer and never pass the isOfficeMimeType
+ * gate), but without an alias Vite would either emit their multi-megabyte chunks
+ * into the build output or fail dev import-analysis on unresolvable specifiers.
+ * See the officeparser aliases in vite.config.mts.
+ */
+export default {};
diff --git a/apps/standalone/src/stubs/officeparser_dist.d.ts b/apps/standalone/src/stubs/officeparser_dist.d.ts
new file mode 100644
index 0000000000..2ec37094d2
--- /dev/null
+++ b/apps/standalone/src/stubs/officeparser_dist.d.ts
@@ -0,0 +1,9 @@
+/**
+ * officeparser's dist/index.mjs is reached directly by path (bypassing the package's
+ * `exports` map, which only exposes the prebuilt browser monolith to bundlers), so it
+ * carries no type information of its own. It re-exports exactly the package's public
+ * API, so borrow the types from the package entry.
+ */
+declare module "*/officeparser/dist/index.mjs" {
+ export * from "officeparser";
+}
diff --git a/apps/standalone/src/stubs/officeparser_entry.ts b/apps/standalone/src/stubs/officeparser_entry.ts
new file mode 100644
index 0000000000..f86f5e502d
--- /dev/null
+++ b/apps/standalone/src/stubs/officeparser_entry.ts
@@ -0,0 +1,10 @@
+/**
+ * Entry module behind the `officeparser` alias in vite.config.mts. It loads the
+ * package's Node ESM entry instead of the prebuilt `browser` monolith — the monolith
+ * inlines pdfjs-dist for PDF parsing (unused here), which alone is ~2.2 MB of the
+ * ~2.7 MB bundle. The Buffer polyfill import must stay first: ES module evaluation
+ * order guarantees it runs before any officeparser code.
+ */
+import "./buffer_polyfill.js";
+
+export * from "../../../../node_modules/officeparser/dist/index.mjs";
diff --git a/apps/standalone/vite.config.mts b/apps/standalone/vite.config.mts
index 65dceb7526..999ce5ab0a 100644
--- a/apps/standalone/vite.config.mts
+++ b/apps/standalone/vite.config.mts
@@ -244,6 +244,32 @@ export default defineConfig(() => ({
{
find: "@client",
replacement: join(__dirname, "../client/src")
+ },
+ // Bypass officeparser's `browser` entry — a prebuilt ~2.7 MB monolith that
+ // inlines pdfjs-dist for its PDF-parsing feature. The wrapper bundles the
+ // package's Node ESM entry instead (plus the Buffer polyfill it needs),
+ // keeping only the office-format parsers and the HTML generator (~0.4 MB).
+ // Guarded against upstream layout changes by office_preview.spec.ts.
+ {
+ find: /^officeparser$/,
+ replacement: join(__dirname, "src/stubs/officeparser_entry.ts")
+ },
+ // Heavy optional officeparser dependencies, only reachable via dynamic import
+ // on code paths office-format conversion never executes (PDF parsing, OCR,
+ // PDF generation via puppeteer). Without the stubs, Vite would either emit
+ // their multi-megabyte chunks or fail dev import-analysis on unresolvable
+ // specifiers.
+ {
+ find: /^pdfjs-dist(\/.*)?$/,
+ replacement: join(__dirname, "src/stubs/empty.ts")
+ },
+ {
+ find: /^tesseract\.js$/,
+ replacement: join(__dirname, "src/stubs/empty.ts")
+ },
+ {
+ find: /^puppeteer$/,
+ replacement: join(__dirname, "src/stubs/empty.ts")
}
],
dedupe: [
@@ -286,7 +312,13 @@ export default defineConfig(() => ({
}
},
optimizeDeps: {
- exclude: ['@sqlite.org/sqlite-wasm', '@triliumnext/core']
+ exclude: ['@sqlite.org/sqlite-wasm', '@triliumnext/core'],
+ // Dynamically imported from the excluded @triliumnext/core, so the dep scanner
+ // never discovers it on its own. Pre-bundling matters beyond warm-up here: the
+ // officeparser alias resolves to CJS internals that only the dep optimizer
+ // converts to ESM in dev — without it, the dev server serves raw CJS and the
+ // import fails in the browser.
+ include: ['officeparser']
},
worker: {
format: "es" as const
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3dd8de4f86..e0de7383a0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -899,6 +899,9 @@ importers:
boxicons:
specifier: 2.1.4
version: 2.1.4
+ buffer:
+ specifier: 6.0.3
+ version: 6.0.3
clsx:
specifier: 2.1.1
version: 2.1.1