I took a gander at the source code here. And while I'm not amazing at reading C, I can read comments. It looks like currently there's no way to load a file that is co-located within the same folder.
I have a project that has a bunch of other stuff besides the Espruino code so I'm trying to avoid using the Espruino playground folder since that wouldn't be under the same source control. For more context, my project directory structure is:
... other non-espruino stuff ...
LEDserver.js is basically a server that understands some animation binary data from a request. warningGif.js is a file with a pre-built animation I'd like to load when things go awry, like one of the incoming frames eating up too much current. It doesn't seem like this is possible in the current setup.
I even tried hacking this a bit, allowing NPM in the Web IDE settings, and using a node_modules folder to house warningGif.js but alas, this refuses to upload to the device (an ESP32).
I'm not sure I'd be able to do it but I wouldn't mind trying. If I pulled together some C code to look up a file locally and made a PR, would that be something folks would be interested in? Is there some technical reason you can't do this?
I'm not exactly clear about what you want to achieve.
For one, the C code you looked at is to figure out when executing at run time wether the module is a built-in module, and built-in means being a part of the flashed ...bin.
Currently, no modules are pulled from Espruino at runtime. They are all expected to be built-in, cached in Modules, or on (connected) SD card. In your case you may connect an SD card and use a string variable with the module name as value in the require() rather than a literal - single or double quoted - string.
Ah that's a bummer. Been doing web dev and assumed things worked in a similar fashion. I think I was imagining something coming along, reading the requires ahead of time, and compiling everything into a single block of source code a la webpack.
Sounds like I can just keep it in source control but save it to flash separately. Thanks for the explanation!
Bummer? ... no sure about that.
There is reading of the (nested) requires ahead of time... but it is followed up differently. Compiling (or composing) to a single code entity ...nope... nor 'jit compiling' or a-like, even though there is a "compiled" option and "pre-tokenizing" available. Espruino runs on the source! ...there is just not enough memory for such things. That makes it so unique, and interestingly fast in it's own way (of course it depends on the code to execute.
There is though a way to keep these js outside of the modules folder and 'somewhat within' the project codes folder: use the repository url in the require... that will pull it from (just any) url... and if you need the require multiple time but the url is long and you want to save the space, keep the module reference in a variable placing something like this in the beginning of the code:
var aMod = require("https:aVery/Long/Repos/URL/for/Module/a"), // a=LEDServer
bMod = require("https:aVery/Long/Repos/URL/for/Module/b"); // b=warningGif
or - when a and b are actually 'classes'/Constructor functions A and B:
var A = require("https:aVery/Long/Repos/URL/for/Class/A"),
B = require("https:aVery/Long/Repos/URL/for/Class/B");
This makes sure that the module/'Class'/prototype is load into the Modules cache. Instead of Repos, you may have Web server where you put the stuff... (can even be a Dev Web Server for Code in your (W)LAN (it has to be accessible just at code upload time).
Later on, you just uses, for example, aMod.<method>(...) - or when classes - var a1 = new A(...); to get what you want.
var a1 = new A(...);
This approach though requires that you commit - push to remote - this code ahead of the other code and your repos is publicly accessible our you manage something with cert registration/exchange for authentication under the hood (you can of course always go and modify the Web IDE (and extend the upload capabilities).
Forgot to mention the multi-step-upload or composing upload: disable/un-check "Reset before upload of code" in Settings-Communications and upload multiple code entities. First you upload these project related modules with above mentioned code snippets. You have though to do a manual reset() in the terminal before the multi-step-upload and load one after the other code entities into the Web IDE editor...
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
For a full reference visit the Markdown syntax.
© Espruino, powered by microcosm.
Report a problem