Skip to content

bundlers/obsidian-plugin-mobile-load-check

Fails a production build whose main.js cannot even be LOADED on Obsidian mobile.

The failure this guards against is invisible on desktop and reads as a mystery on a phone: a module reached while the bundle initializes evaluates a platform-only API, so the plugin dies before any of its code runs. The integration-test harness plugin died exactly that way on Android: the generated barrels imported desktop-demo-vault-opener.ts, whose adm-zip dependency opens with a top-level const { randomFillSync } = require('crypto'), and mobile’s require hands back undefined.

The check evaluates the emitted bundle in a node:vm context shaped like Obsidian mobile: require returns undefined for every Node builtin and a permissive stub for the externals the app itself provides (obsidian, electron, the CodeMirror packages), process is present and renderer-shaped, which is what keeps a runtime-branching dependency such as debug on its browser branch there, and the globals a webview has (the DOM, URL, TextEncoder, Obsidian’s own createDiv…) are present. Node’s own globals, Buffer first of all, are absent, as they are on a phone. Loading the bundle that shipped the Android failure reproduces that exact error in milliseconds, so this is a faithful model rather than a proxy for one.

It asserts only that module INITIALIZATION survives: nothing is called, so a helper that reaches a Node builtin from inside a function (a call-time dynamic import()) passes, which is the whole point: platform-only work is allowed, evaluating it at load time is not. Executing the bundle, rather than scanning its text for process.env or require("node:..."), reports only what really throws on a phone, whatever its spelling.

Interface Description
AssertMainJsLoadsOnMobileParams Parameters for assertMainJsLoadsOnMobile: the part of the build context it reads.
AssertMobileLoadableBundleParams Parameters for assertMobileLoadableBundle.
Function Description
assertMainJsLoadsOnMobile Asserts that a production main.js loads on Obsidian mobile, unless the plugin’s built manifest.json says it is desktop-only, the check is turned off, or the build is a development one. Every bundler adapter calls it once the output folder is complete.
assertMobileLoadableBundle Asserts that the bundle’s top level survives on Obsidian mobile.