How to load a JS library module which requires another plugin library module with non-relative title?

153 views
Skip to first unread message

TheDiveO

unread,
Jan 24, 2018, 3:08:59 PM1/24/18
to TiddlyWiki
My goal is to support so-called Video.js player plugins in my TwTube plugin. Placing the video.js player itself into a TiddlyWiki library module such as $:/plugin/TheDiveO/twtube/video.js works as expected.

Now, video.js plugins, say foo.js, attach themselves to the video.js player by means of getting the referece to it via require("video.js"). And that is causing me problems: because the required module name is not relative, this would reference a (user) tiddler video.js. However, it should be the plugin tiddler instead, somewhere inside $:/plugins/...

Of course, I can edit the plugin source to fix the require()...

But: is there another way ot hack to get the require working as needed? One bad idea that occured to me is simply adding an alias to $tw.modules.titles... Any better ideas?

Best regards,
TheDiveO

Jeremy Ruston

unread,
Jan 24, 2018, 3:14:44 PM1/24/18
to tiddl...@googlegroups.com
Hi TheDiveO

But: is there another way ot hack to get the require working as needed? One bad idea that occured to me is simply adding an alias to $tw.modules.titles... Any better ideas?

The core Jasmine plugin has the same problem and solves it with a fairly hefty hack: it executes the plugin modules with a special context that includes a custom override of the require() function:


Perhaps you can do something similar?

We could also consider pulling the guts of the technique into a generic $tw.utils function.

Best wishes

Jeremy.


Best regards,
TheDiveO

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/1e9464ca-fbda-40c7-aeed-8c147ca95260%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

TheDiveO

unread,
Jan 24, 2018, 3:47:42 PM1/24/18
to TiddlyWiki
Hi Jeremy,

thank you very much for your answer. Being able to have a utility function that allows to load a JS module with a caller-supplied require() context or similar looks appealing. This would keep "aliasing" or dedicated module name resolution local to such modules that need it. And it would be cleaner than mucking around with $tw.modules.

Best regards,
TheDiveO

BJ

unread,
Jan 24, 2018, 3:59:58 PM1/24/18
to tiddl...@googlegroups.com
maybe you could use the 'prefix' field in the tiddlywiki.files and redefine 'require' there for each videojs plugin..

Jeremy Ruston

unread,
Jan 24, 2018, 4:00:05 PM1/24/18
to tiddl...@googlegroups.com
Hi TheDiveO

thank you very much for your answer. Being able to have a utility function that allows to load a JS module with a caller-supplied require() context or similar looks appealing. This would keep "aliasing" or dedicated module name resolution local to such modules that need it. And it would be cleaner than mucking around with $tw.modules.

Most of the logic is already encapsulated in $tw.utils.evalSandboxed(). Let’s see whether the technique works for you before we try to generalise it further.

Best wishes

Jeremy.



Best regards,
TheDiveO

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.

TheDiveO

unread,
Jan 24, 2018, 4:54:36 PM1/24/18
to TiddlyWiki
Ah, I see. So I probably just need to set up a context object and give it a require() that fixes the one specific absolute module name and thain chains to the $tw.modules.execute(). And then I run the video.js plugin sources through $tw.utils.evalSandboxed(). If that's more or less all then a generalization would be overkill, as there's really not much to generalize then.

Thank you very much for pointing me to the interesting places and clarifying things, so I can now try some code. Before, I wasn't sure that I could change require() functionality at all, or if so, how.

Best regards,
TheDiveO

TheDiveO

unread,
Jan 25, 2018, 1:51:45 PM1/25/18
to TiddlyWiki
I've now a first prototype up and running using the sandbox util function and can load a videojs plugin. I still need to iron out some wrinkles concerning plugin-specific contexts; but no showstopper here. These wrinkles are probably due to the way video.js plugins work.

One thing I noticed is related instead to using the getTiddlersFiltered wikimethod in a startup module. I only could get this going without throwing exceptions about missing bits and pieces after declaring my startup module to be run "after: startup". That's the kind of seemingly self-contradicting code I really like.

I have no idea why the jasemine startup module you pointed me to doesn't seem to have this issue.

Best regards,
TheDiveO

Reply all
Reply to author
Forward
0 new messages