Building smart functions
Smart functions are written and run in ordinary TypeScript, but you must still build them with the Jstz dependencies before deploying them.
For examples of smart function projects, see the examples folder: https://github.com/jstz-dev/jstz/tree/main/examples.
Follow these steps to create and build a TypeScript project for your smart function:
-
Create a
package.jsonfile appropriate for a TypeScript project. You can use thenpm initcommand, theyarn initcommand, or create apackage.jsonfile manually to describe the project. -
Add the
@jstz-dev/jstzdependency andesbuildbuild dependencies. -
Add a build script that looks like this, with the name of your smart function source file in place of the variable
<SOURCE_FILE>:esbuild <SOURCE_FILE> --bundle --format=esm --target=esnext --minify --outfile=dist/index.jsHere is an example of a
package.jsonfile for a smart function project:{
"name": "my-smart-function",
"authors": "",
"version": "0.0.0",
"main": "index.ts",
"dependencies": {
"@jstz-dev/jstz": "^0.0.0"
},
"devDependencies": {
"esbuild": "^0.20.2"
},
"scripts": {
"build": "esbuild index.ts --bundle --format=esm --target=esnext --minify --outfile=dist/index.js"
}
} -
Add a
tsconfig.jsonfile to specify how TypeScript builds the file, including accessing types for Jstz code, as in this example:{
"compilerOptions": {
"lib": ["esnext"],
"module": "esnext",
"target": "esnext",
"strict": true,
"moduleResolution": "node",
"types": ["@jstz-dev/types"]
},
"exclude": ["node_modules"]
} -
Install the dependencies by running
npm installoryarn install. -
Add the code of your smart function to a TypeScript file in the project and make sure that the
buildscript builds it. For example, you can use the example smart function in Smart functions or any of the smart functions in the examples folder. -
Build the smart function with the
buildscript, either by runningnpm run buildoryarn build. Jstz builds the smart function with the necessary dependencies and writes the output todist/index.ts.
Now you can deploy the smart function to the local sandbox. See Deploying.
You can verify the size of the built smart function with the du command, as in du -bh dist/index.ts.
Smart functions must be less than 10MB to be deployed.