Skip to content

Build your personal knowledge base with Trilium Notes

feat(desktop): switch the Electron main bundle to ESM with code splitting

Elian Doran5d1c5ff97342
diff --git a/apps/desktop/e2e/example.spec.ts b/apps/desktop/e2e/example.spec.ts
index d07085ef91..41ca6f0db9 100644
--- a/apps/desktop/e2e/example.spec.ts
+++ b/apps/desktop/e2e/example.spec.ts
@@ -5,7 +5,7 @@ import App from './support';
let app: ElectronApplication;
test.beforeAll(async () => {
- const distPath = join(__dirname, '../../desktop/dist/main.cjs');
+ const distPath = join(__dirname, '../../desktop/dist/main.mjs');
console.log("Dir", join(__dirname, 'traces'));
app = await electron.launch({
args: [ distPath ]
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.ts
index 1dd3161b15..960832c373 100644
--- a/apps/desktop/scripts/build.ts
+++ b/apps/desktop/scripts/build.ts
@@ -13,7 +13,10 @@ async function main() {
// (it never references import.meta.url). Build it first so the main bundle,
// built after, leaves the final meta.json.
await build.buildBackend([ "src/preload.ts" ], { importMetaUrlShim: false });
- await build.buildBackend([ "src/main.ts" ]);
+ // ESM so dynamic-import boundaries split into chunks/ that only load on
+ // first use; the preload above must stay CJS (sandboxed renderer) and the
+ // image worker below is spawned by its .cjs path.
+ await build.buildBackend([ "src/main.ts" ], { format: "esm" });
// The image compression worker, which lives in the server it embeds. Built here too so the
// desktop app can compress off-thread rather than falling back to doing it in the main process.
await build.buildBackend([ "../server/src/services/image_worker.ts" ]);
@@ -44,7 +47,7 @@ function generatePackageJson() {
const { version, author, license, description, dependencies, devDependencies } = originalPackageJson;
const packageJson = {
name: "trilium",
- main: "main.cjs",
+ main: "main.mjs",
version, author, license, description,
dependencies: {
"better-sqlite3": dependencies["better-sqlite3"],
diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts
index 5a93f04d91..daec32033c 100644
--- a/scripts/build-utils.ts
+++ b/scripts/build-utils.ts
@@ -111,6 +111,11 @@ export default class BuildHelper {
// ESM, so each output file defines them from `import.meta.url`.
// esbuild's `__require` interop helper picks up the banner's
// `require` binding for external packages (better-sqlite3 etc.).
+ // `__dirname` in a chunk resolves to the bundle root, not
+ // `chunks/`: bundled code uses it to find siblings of the entry
+ // (preload.cjs, image_worker.cjs, assets/), and which chunk a
+ // module lands in must not change what the path means. The
+ // `chunks` suffix check matches `chunkNames` above.
...(esm ? {
banner: {
js: [
@@ -119,7 +124,8 @@ export default class BuildHelper {
`import { dirname as __bundleDirname } from "node:path";`,
`const require = __bundleCreateRequire(import.meta.url);`,
`const __filename = __bundleFileURLToPath(import.meta.url);`,
- `const __dirname = __bundleDirname(__filename);`
+ `const __bundleFileDir = __bundleDirname(__filename);`,
+ `const __dirname = /[\\\\/]chunks$/.test(__bundleFileDir) ? __bundleDirname(__bundleFileDir) : __bundleFileDir;`
].join("\n")
}
} : importMetaUrlShim && {