Hi there! o)
I was playing with the new include feature for external scripts/snippets. I think it is a nice addition to the scripting functionality (and I'm quite curious how you solved this technically, but offtopic here o)!
Some things came up my mind while looking closer, please read on.. o)
The inc_ Prefix:
I would prefer if included files could be separated into a folder, instead of mixing them into the 100+ script addins I already have in /scripts. Is the prefix really necessary? Could we add support for a path to the file to be included, supporting relative and absolute paths and aliases like /scripts as well? So we get the usual DO experience? o)
"@" vs. WSH:
The "@" character is reserved in MS JS in connection with conditional compiling. Any JS script using that "@include" statement at the top will give following error if run with the MS JS interpreter.
>cscript.exe inc_runHiddenExAIO.js
..\inc_runHiddenExAIO.js(2, 9) Microsoft JScript compilation error:
Conditional compilation is turned off
The MS JS interpreter would handle @if @end etc. if conditional compiling is enabled, you can enable cc by adding /@cc_on @/ at the top of the JS file. It still does not know about @include statement though. It will give following error message with cc enabled:
>cscript.exe inc_runHiddenExAIO.js
..\inc_runHiddenExAIO.js(3, 10) Microsoft JScript compilation error:
Expected ';'
"@" vs. nodejs:
Also nodejs will complain if @include is to be found in the script:
>node inc_runHiddenExAIO.js
..\inc\inc_runHiddenExAIO.js:3
@include inc_myinclude.js
^
SyntaxError: Invalid or unexpected token
←[90m at internalCompileFunction (node:internal/vm:73:18)←[39m
..
Node.js v18.15.0
Nodejs is using a function called require() to include external files and modules, it also handles some "return value" from inside the included script. The script would just assign a value to "module.exports" and in the parent script you can pass that value on.
var foohandler = require('/path/to/foohandler.js');
Using require() without "module.exports" will just run the external code and not ex/import any members.
And now?!:
I'm all in for an "include" of some kind, don't get me wrong, but we should make sure, it does not interfere with existing interpreters, editors and syntax highlighters and is not to be confused with @-modifiers, at first I was thinking "modifier!", but it's not to be mixed up with DO modifiers using the same syntax, right? Anyway, adding "@" to the JS files will break them, it is basically the same thing I tried to bring to your attention in 2016, where you added resources to a JS file, by injecting "== SCRIPT RESOURCES" straight into valid JS code - it was a very "quick" idea from my point of view (which still can be enhanced, no problem! o).
EASY FIX!? o)
As a first measure, until we might have a syntax friendly and nodejs compatible require() function one day, adding a simple "//" to the front of @include would make things work for now, so.. can we switch to this syntax?
//@include inc_myfile.js
Same for "== SCRIPT RESOURCES", separating the script resources from the JS body by using "//"..
//== SCRIPT RESOURCES
//..
//== SCRIPT RESOURCES - END
..would not destroy the JS syntax and the best thing:
This has no technical limitation or drawback at all (I think)! o)
The greater picture I have in mind is..
- keep/restore compatibility with jscript + nodejs syntax
- do not create a "walled garden" with new DO JS script syntax
- support for GIT repos in the /scripts folder in the future (getting more and more important)
- introduce proper require() like function (allow any rel/abs path)
- keep things future proof, without adding "hacks" which are hard to work around
Thank you! o)