You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -15,6 +15,16 @@ This example tutorial is applicable for desktop operating systems only.
15
15
16
16
We recommend reading the general [sidecar guide] first for a deeper understanding of how Tauri sidecars work.
17
17
18
+
## Goals
19
+
20
+
- Package a Node.js application as a binary.
21
+
- Integrate this binary as a Tauri sidecar.
22
+
23
+
## Implementation Details
24
+
25
+
- For this we use the [pkg] tool, but any other tool that can compile JavaScript or Typescript into a binary application will work.
26
+
- You can also embed the Node runtime itself into your Tauri application and ship bundled JavaScript as a resource, but this will ship the JavaScript content as readable-ish files and the runtime is usually larger than a `pkg` packaged application.
27
+
18
28
In this example we will create a Node.js application that reads input from the command line [process.argv]
19
29
and writes output to stdout using [console.log]. <br/>
20
30
You can leverage alternative inter-process communication systems such as a localhost server, stdin/stdout or local sockets.
@@ -35,7 +45,6 @@ If you are not an advanced user it's **highly recommended** that you use the opt
35
45
- Choose your package manager: `pnpm`
36
46
- Choose your UI template: `Vanilla`
37
47
- Choose your UI flavor: `Typescript`
38
-
- Would you like to setup the project for mobile as well? `yes`
39
48
40
49
:::
41
50
@@ -56,8 +65,8 @@ Without the plugin being initialized and configured the example won't work.
We will compile our Node.js application to a self container binary using [pkg].
60
-
Let's install it as a development dependency:
68
+
We will compile our Node.js application to a self container binary using [pkg] among other options.
69
+
Let's install it as a development dependency into the new `sidecar-app`:
61
70
62
71
<CommandTabs
63
72
npm="npm add @yao-pkg/pkg --save-dev"
@@ -79,9 +88,9 @@ Without the plugin being initialized and configured the example won't work.
79
88
constcommand=process.argv[2];
80
89
81
90
switch (command) {
82
-
case'ping':
91
+
case'hello':
83
92
constmessage=process.argv[3];
84
-
console.log(`pong,${message}`);
93
+
console.log(`Hello${message}!`);
85
94
break;
86
95
default:
87
96
console.error(`unknown command ${command}`);
@@ -91,18 +100,24 @@ Without the plugin being initialized and configured the example won't work.
91
100
92
101
1.##### Package the Sidecar
93
102
94
-
To package our Node.js application to a self contained binary, we can run the following `pkg` command:
103
+
To package our Node.js application into a self contained binary, create a script in `package.json`:
95
104
96
-
<CommandTabs
97
-
npm="npm run pkg -- --output app"
98
-
yarn="yarn pkg --output app"
99
-
pnpm="pnpm pkg --output app"
100
-
/>
105
+
```json title="sidecar-app/package.json"
106
+
{
107
+
"scripts": {
108
+
"build": "pkg index.ts --output app"
109
+
}
110
+
}
111
+
```
112
+
113
+
<CommandTabsnpm="npm run build"yarn="yarn build"pnpm="pnpm build" />
101
114
102
115
This will create the `sidecar-app/app` binary on Linux and macOS, and a `sidecar-app/app.exe` executable on Windows.
103
-
To rename this file to the expected Tauri sidecar filename, we can use the following Node.js script:
104
116
105
-
```js
117
+
For sidecar applications, we need to ensure that the binary is named in the correct pattern, for more information read [Embedding External Binaries](https://tauri.app/develop/sidecar/)
118
+
To rename this file to the expected Tauri sidecar filename and also move to our Tauri project, we can use the following Node.js script as a starting example:
119
+
120
+
```js title="sidecar-app/rename.js"
106
121
import { execSync } from'child_process';
107
122
importfsfrom'fs';
108
123
@@ -113,12 +128,42 @@ Without the plugin being initialized and configured the example won't work.
113
128
if (!targetTriple) {
114
129
console.error('Failed to determine platform target triple');
115
130
}
131
+
// TODO: create `src-tauri/binaries` dir
116
132
fs.renameSync(
117
133
`app${ext}`,
118
134
`../src-tauri/binaries/app-${targetTriple}${ext}`
119
135
);
120
136
```
121
137
138
+
And run `node rename.js` from the `sidecar-app` directory.
139
+
140
+
At this step the `/src-tauri/binaries` directory should contain the renamed sidecar binary.
141
+
142
+
1.##### Setup plugin-shell permission
143
+
144
+
After installing the [shell plugin](/plugin/shell/) make sure you configure the required capabilities.
145
+
146
+
Note that we use `"args": true` but you can optionally provide an array `["hello"]`, [read more](/develop/sidecar/#passing-arguments).
0 commit comments