Deno 1.36 brings a small, but powerful, upgrade to deno compile
.
Now, developers can compile native binaries on Windows that don’t depend on a console window! This flag changes the WIndows subsystem your application links to, giving access to libraries specifically built for desktop, an air of legitimacy, and a more-private stdout
.
What are Windows Subsystems?
According to René Nyffenegger, Windows consists of multiple subsystems that all expose subsets of a base Windows executive system service. If you’re a developer on Windows, chances are you’ve heard of a few.
Native–used for device drivers and native Windows processes
Windows (GUI)
OS2 CUI
Posix CUI
Native 9x driver
Windows CE
EFIÂ application
EFI driver with boot services
EFI driver with run-time services
EFI ROM image
XBOX
Windows boot application
Each subsystem gives your application access to specific libraries and access to system resources. Since console and GUI (Graphic User Interface) apps are inherently different, linking the appropriate subsystem is crucial when building your executable. According to Nyffenegger, linking to the Windows subsystem not only doesn’t spawn a console window, but provides your application access to libraries for detecting mouse movement, putting multiple windows side-by-side, and other desktop-specific tasks.
I want to show you how you can build a small desktop app with Deno, and the effects of this new compiler flag.
Building a Desktop App with Deno
import { Webview } from "https://deno.land/x/webview@0.7.6/mod.ts";
const html = `
<html>
<body>
<h1>Hello from deno v${Deno.version.deno}</h1>
</body>
</html>
`;
const webview = new Webview();
webview.navigate(`data:text/html,${encodeURIComponent(html)}`);
webview.run();
Above is a simple script for launching a desktop app with Deno. You can save the following deno.jsonc
to copy the various build options I’d like to demonstrate.
{
"tasks": {
"build-dev": "deno compile -Ar --unstable --allow-ffi main.ts",
"build": "deno compile -Ar --no-terminal --unstable --allow-ffi main.ts",
"dev": "deno run -Ar --unstable --allow-ffi --watch main.ts",
"start": "deno run -Ar --unstable --allow-ffi main.ts"
}
}
Running any of these commands will result in the following window.
Running the build scripts will result in a static binary. It’s not perfect, and you can’t modify the icon or other resources without third-party tools, but it’s a start of a Deno desktop ecosystem to rival Electron. With deno compile
and FFI, desktop apps with Deno are cross-compliable and as capable as any app utilizing the OS’s native web view.
"--no-terminal" causes exe to exit immediately without error on Windows 11