How to access other plugins as dependencies?

I am going to develop a plugin and I find some other plugins have done great work. Can I add these plugins as my dependencies?

Take a look at workbench, it taps into natural language dates and doesn’t let you activate it unless natural languages dates is installed.

Note that when checking for dependencies, you should ensure you’re doing so after the workspace’s layout-ready event, or else you may falsely conclude that the plugin you want isn’t activated.

The reason for this is that when Obsidian initially loads a vault, it activates the plugins in what’s essentially random order before setting up the layout of the workspace.

So, when your plugin’s onload() runs, you might not see the other plugin as enabled, even if it is enabled, because it just hasn’t loaded yet. That’s why you need to perform your check (and setup) after the workspace layout event. Otherwise, you may end up thinking another plugin isn’t there.

So your onload should look something like:

onload() { () => {
        // check for any plugins you need, then
        //      display a notice and exit if not found

        // do all your actual setup work, and use
        //      this.register(()=>stuff); to do stuff on unload

You should not have an onunload() method, but instead register callbacks to run at unload time, or if you do have an onunload(), it should only undo things that you know were actually done. (Since the plugin might never have started, due to missing dependencies.)

1 Like