---
title: "Advanced Packaging"
description: "Run the production bundle on Bun or Deno, ship native addons, add launcher actions, and package helper commands."
---

# Advanced Packaging

These recipes extend [Deploying](/guide/deploying) for apps that run their bundle on another runtime, load their own native addons, add launcher actions or ship a second command.

## Run with Bun or Deno

The GTKX CLI, development server, and tests run on Node.js, and `gtkx deploy` packages Node.js with the application.
The production build in `dist/` also runs on [Bun](https://bun.sh) and [Deno](https://deno.com):

```bash
bun dist/bundle.mjs
deno run --allow-all dist/bundle.mjs
```

### Standalone executables with Bun

`bun compile`, which Bun's canary builds provide from 1.4.6, compiles the bundle and GTKX's native addon into one executable:

```bash
cd dist
bun compile ./bundle.mjs --outfile my-app
```

Stable Bun up to 1.4.5 has no `bun compile`, and its `bun build --compile` leaves the native addon out, so that executable fails to start.

The executable cannot read the other files `gtkx build` writes beside the bundle: `gtkx.gresource` for imported
assets, the compiled [settings schemas](/guide/deploying#settings-schemas), and the application icon. An application that imports
assets or uses settings fails to start as an executable; run its bundle instead.

## Additional native addons

Import a prebuilt `.node` file with `?url` so GTKX includes it in the package:

```ts
import { createRequire } from "node:module";
import addonPath from "../vendor/addon.node?url";

const require = createRequire(import.meta.url);
const addon = require(addonPath) as { open(path: string): unknown };
```

Choose a prebuild matching the deployment architecture and Node.js ABI. Import the `.node` file itself, not
the package that loads it: `gtkx build` cannot bundle a package whose entry point requires its `.node` file
and stops with an error naming that package, and a loader that searches its original `node_modules` directory will not find that directory after bundling.
For prebuilds published as one package per
platform, such as `@node-rs/xxhash-linux-x64-gnu`, add that package as a dependency and import its `.node` file.

Every package of one deployment contains the same build. When `deploy.architectures` or `--arch` selects more
than one architecture, import a prebuild for each and load the one matching `process.arch`; GTKX does not check
the architecture of imported addons.

```ts
import { createRequire } from "node:module";
import arm64Addon from
    "@node-rs/xxhash-linux-arm64-gnu/xxhash.linux-arm64-gnu.node?url";
import x64Addon from
    "@node-rs/xxhash-linux-x64-gnu/xxhash.linux-x64-gnu.node?url";

const require = createRequire(import.meta.url);
const addonPath = process.arch === "arm64" ? arm64Addon : x64Addon;
const addon = require(addonPath) as { xxh64(input: string): bigint };
```

Package managers treat a prebuild package for another CPU differently. pnpm installs it. Yarn skips it unless
`supportedArchitectures` in `.yarnrc.yml` lists that CPU. npm rejects it with `EBADPLATFORM`, also when a
[source Flatpak](/guide/deploying#publishing-on-flathub) runs `npm ci` on another architecture, so npm projects copy each
prebuild into the project and import it by path, as in the first example.

## Desktop actions

`deploy.desktopActions` adds entries to the application's launcher menu. Choosing one runs the application with
the entry's `args`:

```ts
deploy: {
    isDbusActivatable: true,
    desktopActions: { today: { name: "Today", args: ["tasks://today"] } },
},
```

With `isDbusActivatable: true`, launchers ignore `args`. They start the application if needed and activate its
`app.<id>` action, so register an application action under each ID. An action launch does not emit `activate`,
so the windows render only after the handler calls `activate()`:

```tsx
import type * as Adw from "@gtkx/gi/adw";
import { AdwApplication } from "@gtkx/jsx/adw";
import { GSimpleAction } from "@gtkx/jsx/gio";
import { useRef, useState } from "react";
import { MainWindow } from "./main-window.js";

export const App = () => {
    const application = useRef<Adw.Application | null>(null);
    const [view, setView] = useState("inbox");

    const showToday = () => {
        setView("today");
        application.current?.activate();
    };

    return (
        <AdwApplication
            ref={application}
            actions={<GSimpleAction name="today" onActivate={showToday} />}
        >
            <MainWindow view={view} />
        </AdwApplication>
    );
};
```

## Helper commands

An application can ship a command-line helper beside it, such as a `tally-report` command that prints the
application's data without opening a window. Build the helper into its own directory with
[`gtkx build --out`](/guide/cli#gtkx-build) before every deployment, since
`gtkx deploy` rebuilds only `dist/`:

```bash
gtkx build src/report.ts --out dist-report
gtkx deploy
```

List the whole directory in `extraFiles`, so chunk names that change between helper builds need no
configuration change, along with a launcher script and the helper's dependency notices:

```ts
deploy: {
    extraFiles: {
        "lib/tally-report": "dist-report",
        "bin/tally-report": { source: "packaging/tally-report", mode: "0755" },
        "share/licenses/tally/tally-report-NOTICES":
            "dist-report/BUNDLED-NOTICES",
    },
},
```

The launcher runs the helper with the bundled Node.js runtime. Every target installs it at
`lib/<binaryName>/node` below the installation prefix, `lib/tally/node` for an application whose binary name is
`tally`, so launcher scripts can rely on that path:

```sh
#!/bin/sh
set -e
self=$(readlink -f "$0")
prefix=$(dirname "$(dirname "$self")")
exec "$prefix/lib/tally/node" "$prefix/lib/tally-report/bundle.mjs" "$@"
```

Debian, RPM, AppImage, and prebuilt Flatpak packages then contain the helper. Inside the Flatpak sandbox, run
it with `flatpak run --command=tally-report <application ID>`. GTKX does not merge the helper's notices into the
application's, and a [source Flatpak](/guide/deploying#publishing-on-flathub) builds only the application from your repository,
so building and installing the helper there is up to you.
