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

--
Jeremy Ruston
[email protected]
https://jermolene.com

> On 14 Nov 2019, at 02:56, Philip Wonski <[email protected]> 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? 
> 
> 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 
> 
> 
>> On Monday, November 4, 2019 at 9:48:30 PM UTC-5, Philip Wonski wrote:
>> 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 
>> 
>>> On Saturday, November 2, 2019 at 12:37:42 PM UTC-4, Jeremy Ruston wrote:
>>> 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:
>>> 
>>> https://tiddlywiki.com/#tiddlywiki.files%20Files
>>> 
>>> There are a number of examples of third party modules wrapped for 
>>> TiddlyWiki in the core repo:
>>> 
>>> https://github.com/Jermolene/TiddlyWiki5/tree/master/plugins/tiddlywiki
>>> 
>>> 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
>>> 
>>> 
>>>> On 31 Oct 2019, at 17:27, Philip Wonski <[email protected]> wrote:
>>>> 
>>>> 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 
>>>> 
>>>> 
>>>> -- 
>>>> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/tiddlywiki/47575207-c2b9-4386-b8d5-aafe9ec48faa%40googlegroups.com.
>>> 
> 
> -- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/tiddlywiki/548de3f4-4afe-4a87-90a8-959c8c92cf80%40googlegroups.com.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/23AFF0E6-DA2F-469F-836B-5ED41D545195%40gmail.com.

Reply via email to