Obsidian.md Plugins: ''obsidian' not defined'

592 views Asked by At

I am currently writing a plugin for obsidian.md that renders math functions inside a code block. I used webpack for bundling node libraries like yaml and function-plot. In the config, I added 'obsidian' as an external. The plugin builds with a warning about the bundle size but that doesn't matter since it's local anyways. When I add the plugin, it always says ''obsidian' not defined'. I guess it's looking for obsidian in the global context and can't find it? Here's the repo: https://github.com/leonhma/obsidian-functionplot Do you know how to configure webpack properly? There's probably some really easy fix but I'm also new to typescript, webpack and developing plugins for obsidian..

1

There are 1 answers

0
leonhma On BEST ANSWER

Thank you @håkon-hægland for your suggestion (why didn't I think of that?). First of all, the file generated by webpack looked like

function(obsidian) {
... (around 300kb code)
}(obsidian);

, so webpack tried to access some global object called 'obsidian'. The important part in webpack.config.js was

...
externals: [
    obsidian: 'obsidian'
],
...

As per your suggestion, i took a look at the other repo, and they use

...
externals: [
    obsidian: 'commonjs2 obsidian'
],
...

That fixed my problem and now obsidian is properly imported at runtime. Just posting this in case someone else has this problem, as i couldn't find an existing answer myself.

PS: For those interested, since you are most certainly developing obsidian plugins: It was also really important to set output.libraryTarget to commonjs or commonjs2 inside the webpack config.