[TW5] AWS Plugin and external modules

203 views
Skip to first unread message

Philip Wonski

unread,
Oct 31, 2019, 1:27:06 PM10/31/19
to TiddlyWiki
Hi Group-

1) I have a TW that does some frontend AJAX using simple XMLHttpRequest in the browser. 

2) If I try to render the AJAX tiddler in Lambda, it throws the common node error since node requires a separate package xmlhttprequest.

3) My question is, how best to include this dependency in the Lambda. I tried first with npm locally, then zipping the updated package.json plus the node_modules folder with xmlhttprequest module included. 

3b- this still throws an error as the lambda is not finding the module. Presumably I am violating "everything is a tiddler" and need my js inside the index.js. 

3c- so it seems better approach is to require the module as a tiddler like var XMLHttpRequest = require("$:/mymodules/xmlhttprequest").XMLHttpRequest;


But I am new to creating modules in TW, so before I waste hours with trial and error I figured I'd make sure I'm on the right track. How should my original xmlhttprequest.js be modified so that it works as a tiddler like $:/mymodules/xmlhttprequest


Sincere thanks,

Phil 

TonyM

unread,
Oct 31, 2019, 4:22:46 PM10/31/19
to TiddlyWiki
Phil,

I expect you will get help here but this ind of issue would possibly be best posted here https://groups.google.com/forum/?hl=en#!forum/tiddlywikidev

Jeremy has implemented tiddlywiki on AWS

Regards
Tony

Jeremy Ruston

unread,
Nov 2, 2019, 12:37:42 PM11/2/19
to tiddl...@googlegroups.com
Hi Phil

As you say, the best solution is to wrap the xmlhttprequest JS file into a tiddler, and then require that tiddler.

The basic approach is to use a  “tiddlywiki.files” file to turn the raw JS file into a tiddler. This mechanism includes the possibility of adding a prefix/suffix to the text of the file:


There are a number of examples of third party modules wrapped for TiddlyWiki in the core repo:


In particular, the jszip plugin contains an example of publishing a module as a tiddler, and the AWS plugin contains an example of using it.

Best wishes

Jeremy


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/47575207-c2b9-4386-b8d5-aafe9ec48faa%40googlegroups.com.

Philip Wonski

unread,
Nov 4, 2019, 9:48:30 PM11/4/19
to TiddlyWiki
Thank you Jeremy and Tony for the replies (and noted on the separate Dev group). 

Appreciate the tips, I will work off of the examples. I've been itching to do this sort of thing to extend the use of 'TW on AWS' as a static site generator. 

Will report back with any worthwhile documentation for other learners. 

Phil 
To unsubscribe from this group and stop receiving emails from it, send an email to tiddl...@googlegroups.com.

Philip Wonski

unread,
Nov 13, 2019, 9:56:25 PM11/13/19
to tiddl...@googlegroups.com
Jeremy,

Following up on this, I believe I have imported the module without issue (verified by checking the index.js output of  ``--build lambda`` ). 

However, testing the Lambda in AWS still throws an error.

It seems the issue is that my javascript macro which makes the AJAX call is actually just using the built-in httprequest capability within TW5. As such, the AWS console is telling me the variable is undefined in a bunch of tiddlers (tiddlers listed at the bottom of this message). 

So, forgive me if this is a bit of a newbie question, but how would I go about making the variable definition global so that TW itself has a definition for this type of request? 

I was thinking it might make sense to just define it in each of the tiddlers individually... but my macro has this definition and somehow still ends up listed here... (it's possible I have not defined it correctly). 

--

Here's the AWS error with tiddlers using httprequest (note $:/sandbox/myjax_json.js is my javascript macro... it works well in the browser): 

{
  "errorType": "ReferenceError",
  "errorMessage": "XMLHttpRequest is not defined",
  "trace": [
    "ReferenceError: XMLHttpRequest is not defined",
    "    at Object.exports.httpRequest ($:/core/modules/utils/dom/http.js:27:13)",
    "    at MacroCallWidget.exports.run ($:/sandbox/myjax_json.js:42:13)",
    "    at MacroCallWidget.Widget.evaluateMacroModule ($:/core/modules/widgets/widget.js:215:21)",
    "    at MacroCallWidget.Widget.getVariableInfo ($:/core/modules/widgets/widget.js:133:14)",
    "    at MacroCallWidget.execute ($:/core/modules/widgets/macrocall.js:52:23)",
    "    at MacroCallWidget.render ($:/core/modules/widgets/macrocall.js:32:7)",
    "    at Widget.renderChildren ($:/core/modules/widgets/widget.js:390:15)",
    "    at Widget.render ($:/core/modules/widgets/widget.js:75:7)",
    "    at $tw.Wiki.exports.renderTiddler ($:/core/modules/wiki.js:1054:13)",
    "    at Command.subCommands.s3-rendertiddler ($:/plugins/tiddlywiki/aws/command.js:110:33)"
  ]
}


Phil 

Jeremy Ruston

unread,
Nov 20, 2019, 5:35:51 AM11/20/19
to tiddl...@googlegroups.com
Hi Phil

You can use a construction like this to choose the right implementation:

var xhr = $tw.browser ? XMLHttpRequest : require("$:/path/to/my/polyfill").XMLHttpRequest;

And then you'd use xhr as the XMLHttpRequest object

Best wishes

Jeremy


On 14 Nov 2019, at 02:56, Philip Wonski <phil...@gmail.com> wrote:


Jeremy,

Following up on this, I believe I have imported the module without issue (verified by checking the index.js output of  ``--build lambda`` ). 

However, testing the Lambda in AWS still throws an error.

It seems the issue is that my javascript macro that makes the AJAX call is actually just using the built-in httprequest capability within TW5. As such, the AWS console is telling me the variable is undefined in a bunch of tiddlers (tiddlers listed at the bottom of this message). 

So, forgive me if this is a bit of a newbie question, but how would I go about making the variable definition global so that TW itself has a definition for this type of request? 

To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/548de3f4-4afe-4a87-90a8-959c8c92cf80%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages