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..
Asked
Active
Viewed 467 times
1
-
2Where exactly does the Error get thrown? Could you add some Logs. Also, when you add the library where exactly? Some Snippets or Steps to reproduce would be nice. – novarx May 08 '22 at 17:38
-
1I am not familiar with webpack, but maybe you could get some ideas by looking at other plugins that use webpack? See for example https://github.com/hadynz/obsidian-kindle-plugin – Håkon Hægland May 08 '22 at 17:57
1 Answers
2
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.

leonhma
- 110
- 9