Scripts

Tola has no built-in JavaScript bundler. Pick the lightest mechanism that fits: native ES modules for small enhancements, inline scripts for pre-paint setup, and before-build + generate-outputs hooks for TypeScript bundling.

Static ES modules

For small enhancements (copy buttons, scroll-spy, popovers), browsers load ES modules natively without Node.js. Place .js files under [assets] and load with type: "module":

toml
[assets]
cache-busting = true
trees = [{ source = "static/scripts", url-prefix = "/scripts" }]
typst
#import "@tola/address:0.0.0": asset-url

#html.script("", type: "module", src: asset-url("/scripts/copy-code.js"))

type="module" is deferred automatically and scopes top-level bindings per file.

Inline scripts

Tiny scripts that must run before first paint (applying a saved dark theme from localStorage, restoring sidebar scroll or disclosure state) avoid a flash of unstyled state when read directly into <head>:

typst
#html.head[
  #html.script(read("/static/scripts/theme-init.js"))
  #html.link(rel: "stylesheet", href: asset-url("/styles/site.css"))
]

Keep inline scripts minimal (well under 1 KB) and leave the rest in external modules.

Bundle TypeScript

When your client code uses TypeScript or npm dependencies (such as esbuild), split compilation and publishing across two dev = "reuse" hooks: before-build writes into generated/ (making minified init snippets readable via read(...)) and generate-outputs copies browser bundles into the publish tree.

toml
[[hooks.before-build]]
name = "scripts-build"
nu = "tools/scripts.nu"
args = ["bundle"]
dev = "reuse"
rerun-on = { include = ["scripts-src", "package.json", "package-lock.json"] }
generates = ["generated/scripts"]

[[hooks.generate-outputs]]
name = "scripts-publish"
nu = "tools/scripts.nu"
args = ["publish"]
dev = "reuse"
rerun-on = { include = ["generated/scripts"] }
outputs = [{ tree = "scripts" }]
nu
use tola get-hook

def "main bundle" [] {
  let stage = ((get-hook).temp-dir | path join "scripts")
  let entries = (glob "scripts-src/*.ts" | sort)
  ^npx --no-install esbuild ...$entries --bundle --minify --format=iife --outdir=$stage
  mkdir generated/scripts
  for file in (glob ($stage | path join "*.js")) {
    let dest = ("generated/scripts" | path join ($file | path basename))
    if not (($dest | path exists) and (open --raw $file) == (open --raw $dest)) {
      mv --force $file $dest
    }
  }
}

def "main publish" [] {
  let dest = ((get-hook).output-dir | path join "scripts")
  mkdir $dest
  cp ...(glob "generated/scripts/*.js") $dest
}

Reference published bundles via output-url("scripts/app.js"). Staging into temp-dir and skipping unchanged files preserves timestamps during tola dev.

Search this site

Use the arrow keys to move through results.

Type to search.