Skip to content

Commit d42b752

Browse files
committed
Bundle with Vite.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 2f41032 commit d42b752

20 files changed

+1196
-200
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
.transifexrc
99

1010
# Compiled binary build directory
11-
dist/
11+
/dist/
12+
/dist-electron/
1213

1314
#snap generated files
1415
snap/parts
@@ -39,6 +40,3 @@ config.gypi
3940
# tests/package.json
4041

4142
.python-version
42-
43-
# Ignore all the typescript compiled files
44-
app/**/*.js

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/app/**/*.js
21
/dist
2+
/dist-electron
33
/public/translations/*.json

app/common/paths.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import path from "node:path";
2+
import process from "node:process";
3+
import url from "node:url";
4+
5+
export const bundlePath = __dirname;
6+
7+
export const publicPath = import.meta.env.DEV
8+
? path.join(bundlePath, "../public")
9+
: bundlePath;
10+
11+
export const bundleUrl = import.meta.env.DEV
12+
? process.env.VITE_DEV_SERVER_URL
13+
: url.pathToFileURL(__dirname).href + "/";
14+
15+
export const publicUrl = bundleUrl;

app/common/translation-util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import path from "node:path";
33
import i18n from "i18n";
44

55
import * as ConfigUtil from "./config-util.js";
6+
import {publicPath} from "./paths.js";
67

78
i18n.configure({
8-
directory: path.join(__dirname, "../../public/translations/"),
9+
directory: path.join(publicPath, "translations/"),
910
updateFiles: false,
1011
});
1112

app/main/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as remoteMain from "@electron/remote/main";
77
import windowStateKeeper from "electron-window-state";
88

99
import * as ConfigUtil from "../common/config-util.js";
10+
import {bundlePath, bundleUrl, publicPath} from "../common/paths.js";
1011
import type {RendererMessage} from "../common/typed-ipc.js";
1112
import type {MenuProps} from "../common/types.js";
1213

@@ -35,13 +36,13 @@ let badgeCount: number;
3536

3637
let isQuitting = false;
3738

38-
// Load this url in main window
39-
const mainUrl = "file://" + path.join(__dirname, "../renderer", "main.html");
39+
// Load this file in main window
40+
const mainUrl = new URL("app/renderer/main.html", bundleUrl).href;
4041

4142
const permissionCallbacks = new Map<number, (grant: boolean) => void>();
4243
let nextPermissionCallbackId = 0;
4344

44-
const appIcon = path.join(__dirname, "../../public/resources", "Icon");
45+
const appIcon = path.join(publicPath, "resources/Icon");
4546

4647
const iconPath = (): string =>
4748
appIcon + (process.platform === "win32" ? ".ico" : ".png");
@@ -74,7 +75,7 @@ function createMainWindow(): BrowserWindow {
7475
minWidth: 500,
7576
minHeight: 400,
7677
webPreferences: {
77-
preload: require.resolve("../renderer/js/main"),
78+
preload: path.join(bundlePath, "renderer.js"),
7879
sandbox: false,
7980
webviewTag: true,
8081
},

app/main/sentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {app} from "electron/main";
22

3-
import * as Sentry from "@sentry/electron";
3+
import * as Sentry from "@sentry/electron/main"; // eslint-disable-line n/file-extension-in-import
44

55
import {getConfigItem} from "../common/config-util.js";
66

app/renderer/about.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<meta charset="UTF-8" />
3+
<link rel="stylesheet" href="css/about.css" />
4+
5+
<!-- Initially hidden to prevent FOUC -->
6+
<div class="about" hidden>
7+
<img class="logo" src="../resources/zulip.png" />
8+
<p class="detail" id="version"></p>
9+
<div class="maintenance-info">
10+
<p class="detail maintainer">
11+
Maintained by
12+
<a href="https://zulip.com" target="_blank" rel="noopener noreferrer"
13+
>Zulip</a
14+
>
15+
</p>
16+
<p class="detail license">
17+
Available under the
18+
<a
19+
href="https://github.com/zulip/zulip-desktop/blob/main/LICENSE"
20+
target="_blank"
21+
rel="noopener noreferrer"
22+
>Apache 2.0 License</a
23+
>
24+
</p>
25+
</div>
26+
</div>

app/renderer/js/components/webview.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type {WebContents} from "electron/main";
22
import fs from "node:fs";
3-
import path from "node:path";
43
import process from "node:process";
54

65
import * as remote from "@electron/remote";
@@ -11,6 +10,7 @@ import type {Html} from "../../../common/html.js";
1110
import {html} from "../../../common/html.js";
1211
import type {RendererMessage} from "../../../common/typed-ipc.js";
1312
import type {TabRole} from "../../../common/types.js";
13+
import preloadCss from "../../css/preload.css?raw"; // eslint-disable-line n/file-extension-in-import
1414
import {ipcRenderer} from "../typed-ipc-renderer.js";
1515
import * as SystemUtil from "../utils/system-util.js";
1616

@@ -210,10 +210,7 @@ export default class WebView {
210210
this.focus();
211211
this.props.onTitleChange();
212212
// Injecting preload css in webview to override some css rules
213-
(async () =>
214-
this.getWebContents().insertCSS(
215-
fs.readFileSync(path.join(__dirname, "/../../css/preload.css"), "utf8"),
216-
))();
213+
(async () => this.getWebContents().insertCSS(preloadCss))();
217214

218215
// Get customCSS again from config util to avoid warning user again
219216
const customCss = ConfigUtil.getConfigItem("customCSS", null);

app/renderer/js/main.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {clipboard} from "electron/common";
22
import fs from "node:fs";
33
import path from "node:path";
44
import process from "node:process";
5+
import url from "node:url";
56

67
import {Menu, app, dialog, session} from "@electron/remote";
78
import * as remote from "@electron/remote";
@@ -15,6 +16,7 @@ import * as EnterpriseUtil from "../../common/enterprise-util.js";
1516
import * as LinkUtil from "../../common/link-util.js";
1617
import Logger from "../../common/logger-util.js";
1718
import * as Messages from "../../common/messages.js";
19+
import {bundlePath, bundleUrl} from "../../common/paths.js";
1820
import type {NavItem, ServerConf, TabData} from "../../common/types.js";
1921

2022
import FunctionalTab from "./components/functional-tab.js";
@@ -45,12 +47,13 @@ const logger = new Logger({
4547
file: "errors.log",
4648
});
4749

48-
const rendererDirectory = path.resolve(__dirname, "..");
4950
type ServerOrFunctionalTab = ServerTab | FunctionalTab;
5051

5152
const rootWebContents = remote.getCurrentWebContents();
5253

53-
const dingSound = new Audio("../../public/resources/sounds/ding.ogg");
54+
const dingSound = new Audio(
55+
new URL("resources/sounds/ding.ogg", bundleUrl).href,
56+
);
5457

5558
export class ServerManagerView {
5659
$addServerButton: HTMLButtonElement;
@@ -401,7 +404,7 @@ export class ServerManagerView {
401404
await this.openNetworkTroubleshooting(index);
402405
},
403406
onTitleChange: this.updateBadge.bind(this),
404-
preload: "js/preload.js",
407+
preload: url.pathToFileURL(path.join(bundlePath, "preload.js")).href,
405408
}),
406409
}),
407410
);
@@ -628,7 +631,7 @@ export class ServerManagerView {
628631
reconnectUtil.pollInternetAndReload();
629632
await webview
630633
.getWebContents()
631-
.loadURL(`file://${rendererDirectory}/network.html`);
634+
.loadURL(new URL("app/renderer/network.html", bundleUrl).href);
632635
}
633636

634637
async activateLastTab(index: number): Promise<void> {

app/renderer/js/pages/about.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
11
import {app} from "@electron/remote";
22

3-
import {html} from "../../../common/html.js";
3+
import {bundleUrl} from "../../../common/paths.js";
44

55
export class AboutView {
66
static async create(): Promise<AboutView> {
7-
return new AboutView();
7+
return new AboutView(
8+
await (await fetch(new URL("app/renderer/about.html", bundleUrl))).text(),
9+
);
810
}
911

1012
readonly $view: HTMLElement;
1113

12-
private constructor() {
14+
private constructor(templateHtml: string) {
1315
this.$view = document.createElement("div");
1416
const $shadow = this.$view.attachShadow({mode: "open"});
15-
$shadow.innerHTML = html`
16-
<link rel="stylesheet" href="css/about.css" />
17-
<!-- Initially hidden to prevent FOUC -->
18-
<div class="about" hidden>
19-
<img class="logo" src="../resources/zulip.png" />
20-
<p class="detail" id="version">v${app.getVersion()}</p>
21-
<div class="maintenance-info">
22-
<p class="detail maintainer">
23-
Maintained by
24-
<a
25-
href="https://zulip.com"
26-
target="_blank"
27-
rel="noopener noreferrer"
28-
>Zulip</a
29-
>
30-
</p>
31-
<p class="detail license">
32-
Available under the
33-
<a
34-
href="https://github.com/zulip/zulip-desktop/blob/main/LICENSE"
35-
target="_blank"
36-
rel="noopener noreferrer"
37-
>Apache 2.0 License</a
38-
>
39-
</p>
40-
</div>
41-
</div>
42-
`.html;
17+
$shadow.innerHTML = templateHtml;
18+
$shadow.querySelector("#version")!.textContent = `v${app.getVersion()}`;
4319
}
4420

4521
destroy() {

0 commit comments

Comments
 (0)