From eb849a7b3d08eed235b1b6a274939d731d24a817 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 31 Jul 2025 18:41:04 -0700 Subject: [PATCH] Switch to "type": "module". Signed-off-by: Anders Kaseorg --- app/main/index.ts | 4 +-- app/renderer/js/main.ts | 2 +- i18next-parser.config.js | 5 ++-- package.json | 3 ++- tests/index.js | 9 +++---- tests/package.json | 3 ++- tests/setup.js | 29 +++++++++------------ tests/test-add-organization.js | 9 +++---- tests/test-new-organization.js | 9 +++---- vite.config.ts | 46 +++++++++++++++++++++++----------- xo.config.cjs | 7 +----- 11 files changed, 65 insertions(+), 61 deletions(-) diff --git a/app/main/index.ts b/app/main/index.ts index 26cbe691..8ee97b94 100644 --- a/app/main/index.ts +++ b/app/main/index.ts @@ -32,7 +32,7 @@ import {sentryInit} from "./sentry.ts"; import {setAutoLaunch} from "./startup.ts"; import {ipcMain, send} from "./typed-ipc-main.ts"; -import "gatemaker/electron-setup"; // eslint-disable-line import/no-unassigned-import +import "gatemaker/electron-setup.js"; // eslint-disable-line import/no-unassigned-import // eslint-disable-next-line @typescript-eslint/naming-convention const {GDK_BACKEND} = process.env; @@ -87,7 +87,7 @@ function createMainWindow(): BrowserWindow { minWidth: 500, minHeight: 400, webPreferences: { - preload: path.join(bundlePath, "renderer.js"), + preload: path.join(bundlePath, "renderer.cjs"), sandbox: false, webviewTag: true, }, diff --git a/app/renderer/js/main.ts b/app/renderer/js/main.ts index 0836371e..723a3b6e 100644 --- a/app/renderer/js/main.ts +++ b/app/renderer/js/main.ts @@ -412,7 +412,7 @@ export class ServerManagerView { await this.openNetworkTroubleshooting(index); }, onTitleChange: this.updateBadge.bind(this), - preload: url.pathToFileURL(path.join(bundlePath, "preload.js")).href, + preload: url.pathToFileURL(path.join(bundlePath, "preload.cjs")).href, unsupportedMessage: DomainUtil.getUnsupportedMessage(server), }), }); diff --git a/i18next-parser.config.js b/i18next-parser.config.js index 94c4afe7..59a168db 100644 --- a/i18next-parser.config.js +++ b/i18next-parser.config.js @@ -1,6 +1,4 @@ -"use strict"; - -module.exports = { +const config = { createOldCatalogs: false, defaultValue: (locale, namespace, key, value) => locale === "en" ? key : value, @@ -15,3 +13,4 @@ module.exports = { output: "public/translations/$LOCALE.json", sort: (a, b) => (a < b ? -1 : a > b ? 1 : 0), }; +export default config; diff --git a/package.json b/package.json index 9825e202..5c95133f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "zulip", "productName": "Zulip", "version": "5.12.0", - "main": "./dist-electron", + "main": "./dist-electron/index.cjs", "description": "Zulip Desktop App", "license": "Apache-2.0", "copyright": "Kandra Labs, Inc.", @@ -17,6 +17,7 @@ "bugs": { "url": "https://github.com/zulip/zulip-desktop/issues" }, + "type": "module", "engines": { "node": ">=18" }, diff --git a/tests/index.js b/tests/index.js index 7b24f220..e8e32473 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,12 +1,11 @@ -"use strict"; -const {chan, put, take} = require("medium"); -const test = require("tape"); +import {chan, put, take} from "medium"; +import test from "tape"; -const setup = require("./setup.js"); +import * as setup from "./setup.js"; test("app runs", async (t) => { t.timeoutAfter(10e3); - setup.resetTestDataDir(); + setup.resetTestDataDirectory(); const app = await setup.createApp(); try { const windows = chan(); diff --git a/tests/package.json b/tests/package.json index 56762b29..2a24e361 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,5 +1,6 @@ { "version": "5.9.3", "productName": "ZulipTest", - "main": "../dist-electron" + "type": "module", + "main": "../dist-electron/index.cjs" } diff --git a/tests/setup.js b/tests/setup.js index c0e1a64d..10707d5a 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -1,28 +1,23 @@ -"use strict"; -const fs = require("node:fs"); -const path = require("node:path"); -const process = require("node:process"); +import fs from "node:fs"; +import path from "node:path"; +import process from "node:process"; -const {_electron} = require("playwright-core"); +import {_electron} from "playwright-core"; -const testsPkg = require("./package.json"); - -module.exports = { - createApp, - endTest, - resetTestDataDir: resetTestDataDirectory, -}; +const testsPackage = JSON.parse( + fs.readFileSync(new URL("package.json", import.meta.url), "utf8"), +); // Runs Zulip Desktop. // Returns a promise that resolves to an Electron Application once the app has loaded. -function createApp() { +export function createApp() { return _electron.launch({ - args: [path.join(__dirname)], // Ensure this dir has a package.json file with a 'main' entry point + args: [import.meta.dirname], // Ensure this dir has a package.json file with a 'main' entry point }); } // Quit the app, end the test -async function endTest(app) { +export async function endTest(app) { await app.close(); } @@ -52,11 +47,11 @@ function getAppDataDirectory() { } console.log("Detected App Data Dir base:", base); - return path.join(base, testsPkg.productName); + return path.join(base, testsPackage.productName); } // Resets the test directory, containing domain.json, window-state.json, etc -function resetTestDataDirectory() { +export function resetTestDataDirectory() { const appDataDirectory = getAppDataDirectory(); fs.rmSync(appDataDirectory, {force: true, recursive: true}); } diff --git a/tests/test-add-organization.js b/tests/test-add-organization.js index 2ea134c6..afe2d161 100644 --- a/tests/test-add-organization.js +++ b/tests/test-add-organization.js @@ -1,12 +1,11 @@ -"use strict"; -const {chan, put, take} = require("medium"); -const test = require("tape"); +import {chan, put, take} from "medium"; +import test from "tape"; -const setup = require("./setup.js"); +import * as setup from "./setup.js"; test("add-organization", async (t) => { t.timeoutAfter(50e3); - setup.resetTestDataDir(); + setup.resetTestDataDirectory(); const app = await setup.createApp(); try { const windows = chan(); diff --git a/tests/test-new-organization.js b/tests/test-new-organization.js index 8287b8ac..52cb4f0e 100644 --- a/tests/test-new-organization.js +++ b/tests/test-new-organization.js @@ -1,14 +1,13 @@ -"use strict"; -const {chan, put, take} = require("medium"); -const test = require("tape"); +import {chan, put, take} from "medium"; +import test from "tape"; -const setup = require("./setup.js"); +import * as setup from "./setup.js"; // Create new org link should open in the default browser [WIP] test("new-org-link", async (t) => { t.timeoutAfter(50e3); - setup.resetTestDataDir(); + setup.resetTestDataDirectory(); const app = await setup.createApp(); try { const windows = chan(); diff --git a/vite.config.ts b/vite.config.ts index 3d6baaf6..0e002469 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import * as path from "node:path"; - import {defineConfig} from "vite"; import electron from "vite-plugin-electron"; @@ -9,14 +7,20 @@ export default defineConfig({ plugins: [ electron([ { - entry: { - index: "app/main", - }, vite: { build: { + lib: { + entry: { + index: "app/main", + }, + formats: ["cjs"], + }, sourcemap: true, rollupOptions: { external: ["electron", /^electron\//, /^gatemaker\//], + output: { + entryFileNames: "[name].cjs", + }, }, ssr: true, }, @@ -31,27 +35,39 @@ export default defineConfig({ }, }, { - entry: { - preload: "app/renderer/js/preload.ts", - }, vite: { build: { + lib: { + entry: { + preload: "app/renderer/js/preload.ts", + }, + formats: ["cjs"], + }, sourcemap: "inline", rollupOptions: { external: ["electron", /^electron\//], + output: { + entryFileNames: "[name].cjs", + }, }, }, }, }, { - entry: { - renderer: "app/renderer/js/main.ts", - }, vite: { build: { + lib: { + entry: { + renderer: "app/renderer/js/main.ts", + }, + formats: ["cjs"], + }, sourcemap: true, rollupOptions: { external: ["electron", /^electron\//], + output: { + entryFileNames: "[name].cjs", + }, }, }, resolve: { @@ -68,10 +84,10 @@ export default defineConfig({ sourcemap: true, rollupOptions: { input: { - renderer: path.join(__dirname, "app/renderer/main.html"), - network: path.join(__dirname, "app/renderer/network.html"), - about: path.join(__dirname, "app/renderer/about.html"), - preference: path.join(__dirname, "app/renderer/preference.html"), + renderer: "app/renderer/main.html", + network: "app/renderer/network.html", + about: "app/renderer/about.html", + preference: "app/renderer/preference.html", }, }, }, diff --git a/xo.config.cjs b/xo.config.cjs index a6a744b1..edf47cc0 100644 --- a/xo.config.cjs +++ b/xo.config.cjs @@ -128,12 +128,7 @@ module.exports = { }, }, { - files: [ - "**.cjs", - "i18next-parser.config.js", - "scripts/win-sign.js", - "tests/**/*.js", - ], + files: ["**.cjs"], parserOptions: { sourceType: "script", },