Import and export
The environment that third-party applications run in is far from ordinary, mostly because we're dealing with the ArcOS filesystem instead of regular old web assets. Unfortunately this means that importing and exporting between JS files is also a little bit... unorthodox. You can't use a regular require
call or import
keyword.
What's different from normal JS?
You have access to top-level await and return statements
There is no
import
,export
orrequire
TPA javascript has a lot more global variables, classes and properties, which you've undoubtedly encountered already.
Importing and exporting
Say you have a class in tray.js
that you want to make use of in main.js
. This is quite simple! First, at the bottom of tray.js
, export the class by "returning" it:
Then, inside main.js
, destructure the result of an awaited call to the load
function to get the class:
Assuming none of the files in the "import chain" had syntactic errors, your class is now available in main.js
.
The Working Directory
In regular JS, if you want to import a file from another directory on the same level, you'll use ..
to get to the parent directory, like so:
Paths in ArcOS third-party apps are a little different. No matter in what child directory your file is located, your working directory (or __dirname
if you're a NodeJS person) will always be equal to the root of your project. So, if js/main.js
wants to import that MyClass, it'll have to import js/tray.js
instead of tray.js
. This example might seem simple, but it gets a lot more complicated the more files and folders you intertwine.
Behind the scenes
Internally, when the text of your script file is loaded, it is wrapped in a export default async function
before being loaded using a dynamic import. This function takes an object that contains all variables, functions, classes and other globals made accessible to third-party apps. Observe this function stolen from the User Daemon:
This wrapper takes your code and wraps it into a function. This is exactly the reason why top-level awaits work and why you have to return stuff instead of exporting them. The props
variable here is the Third-party app props object assembled before this function is first called. I'm automatically destructuring the props to make them easily accessible to you. You're welcome.
Last updated