Added support for ES Module-style import and export statements.
ES Modules are a way to organize code into separate listeners.
With this update, CasualOS now supports using import and export statements in scripts.
By default, modules can be imported from 3 different places:
Scripts based on system tag. You can import exported functions and variables from any script based on the system tag of its bot. For example, import {abc} from "example.system.tag" can be used to import the abc variable or function from the bot with the example.system system and the tag tag.
The casualos module. This module exports all of CasualOS's built-in functions. It is the only module that cannot be overriden with @onResolveModule. For example:
import { os } from 'casualos';
os.toast('Hello from my module!');
In addition to the default ways, you can provide your own custom module resolution logic by using @onResolveModule.
See the documentation for @onResolveModule for more information.
Added the š prefix for "library" scripts.
Before, the only way to make a script was to use the @ prefix. Using the @ prefix makes what we call a "listener script" or "listener tag".
Now, you can use the š prefix instead of @. Using š makes what we call a "library script" or "library tag".
Library scripts work similarly to listener scripts, except with a couple key changes:
Library scripts cannot be shouted to. Instead, they have to be imported. This means you cannot use shout() or whisper() to communicate with a library script. Additionally, thisBot.script() does not work either.
Library scripts have to import all CasualOS functions. This means you cannot simply use os.toast(), you have to first import the casualos module. For example:
import { os } from 'casualos';
os.toast('Hello from my module!');
:rocket: Features
import
andexport
statements.import
andexport
statements in scripts.import {abc} from "example.system.tag"
can be used to import theabc
variable or function from the bot with theexample.system
system and thetag
tag.import { sortBy } from 'https://esm.run/lodash-es';"
imports thesortBy
function from thelodash-es
module provided by https://esm.run.casualos
module. This module exports all of CasualOS's built-in functions. It is the only module that cannot be overriden with@onResolveModule
. For example:@onResolveModule
.@onResolveModule
for more information.š
prefix for "library" scripts.@
prefix. Using the@
prefix makes what we call a "listener script" or "listener tag".š
prefix instead of@
. Usingš
makes what we call a "library script" or "library tag".shout()
orwhisper()
to communicate with a library script. Additionally,thisBot.script()
does not work either.os.toast()
, you have to first import thecasualos
module. For example:Closes #335