Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(desktop): don't inject import.meta.url banner into sandboxed preload
Elian Doranc13f6e3469b4
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.tsindex d889be4195..27213f61d6 100644--- a/apps/desktop/scripts/build.ts+++ b/apps/desktop/scripts/build.ts@@ -7,7 +7,13 @@ import originalPackageJson from "../package.json" with { type: "json" };const build = new BuildHelper("apps/desktop");async function main() {- await build.buildBackend([ "src/main.ts", "src/preload.ts"]);+ // The preload runs in Electron's sandboxed renderer, where the+ // import.meta.url shim's `require("node:url")` banner throws and aborts the+ // preload before it can expose `electronApi`. Build it without the shim+ // (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" ]);// Copy assets.build.copy("src/assets", "assets/");diff --git a/scripts/build-utils.ts b/scripts/build-utils.tsindex ba539b0afb..b266f62605 100644--- a/scripts/build-utils.ts+++ b/scripts/build-utils.ts@@ -36,7 +36,15 @@ export default class BuildHelper {rmSync(join(this.outDir, path), { recursive: true });}- async buildBackend(entryPoints: string[]) {+ /**+ * @param entryPoints source entry points to bundle into CJS.+ * @param opts.importMetaUrlShim redirects `import.meta.url` to the bundle's own file so+ * bundled ESM deps calling `createRequire(import.meta.url)` work in CJS output (defaults+ * to `true`). Must be disabled for scripts that run in Electron's sandboxed renderer (e.g.+ * the desktop preload), where the injected `require("node:url")` banner throws.+ */+ async buildBackend(entryPoints: string[], opts: { importMetaUrlShim?: boolean } = {}) {+ const { importMetaUrlShim = true } = opts;const result = await esbuild({entryPoints: entryPoints.map(e => join(this.projectDir, e)),tsconfig: join(this.projectDir, "tsconfig.app.json"),@@ -86,11 +94,16 @@ export default class BuildHelper {// then throws `ERR_INVALID_ARG_VALUE`. Redirect it to the// bundle's own file so createRequire()/`.resolve()` anchor at// dist/ and can still locate sibling node_modules packages.- "import.meta.url": "__bundleImportMetaUrl",- },- banner: {- js: `const __bundleImportMetaUrl = require("node:url").pathToFileURL(__filename).href;`+ ...(importMetaUrlShim && { "import.meta.url": "__bundleImportMetaUrl" }),},+ // The banner defines the redirect target above. It uses+ // `require("node:url")`, which throws in Electron's sandboxed+ // renderer, so it must be omitted for preload-style bundles.+ ...(importMetaUrlShim && {+ banner: {+ js: `const __bundleImportMetaUrl = require("node:url").pathToFileURL(__filename).href;`+ }+ }),minify: true});writeFileSync(join(this.outDir, "meta.json"), JSON.stringify(result.metafile));