Skip to content

Commit 6186941

Browse files
committed
Migrate to ESM
Honestly this is the simplest of all my projects and the one where it's most awkward having mismatched source/runtime output, and where I completely control the runtime, so this is a no-brainer. Not sure why I didn't do it years ago.
1 parent bc4b498 commit 6186941

File tree

10 files changed

+25
-20
lines changed

10 files changed

+25
-20
lines changed
File renamed without changes.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
"homepage": "https://httptoolkit.com",
66
"private": true,
77
"description": "HTTP(S) debugging, development & testing tool",
8+
"type": "module",
89
"main": "build/index.js",
910
"scripts": {
1011
"postinstall": "electron-builder install-app-deps",
1112
"server:setup": "tsx ./setup-server.ts",
1213
"build": "npm run build:src && npm run build:electron",
1314
"build:src": "tsc",
1415
"postbuild:src": "tsx ./strip-preload-map.ts",
15-
"build:electron": "npm run server:setup && electron-builder build --config electron-builder.config.js",
16+
"build:electron": "npm run server:setup && electron-builder build --config electron-builder.config.cjs",
1617
"build:dir-only": "npm run server:setup && electron-builder --dir",
1718
"start": "npm run server:setup && npm run start:app",
1819
"start:dev": "tsx ./skip-server.ts && cross-env HTK_DEV=true APP_URL='http://localhost:8080' npm run start:app",

setup-server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import * as os from 'os';
33
import { promises as fs, createWriteStream } from 'fs'
44
import { promisify } from 'util';
55

6-
import * as _ from 'lodash';
6+
import _ from 'lodash';
77
import * as semver from 'semver';
88
import fetch from 'node-fetch';
9-
import * as targz from 'targz';
9+
import targz from 'targz';
1010
import { execSync } from 'child_process';
1111

12+
import packageJson from './package.json' with { type: 'json' };
13+
1214
const extractTarGz = promisify(targz.decompress);
1315

1416
const canAccess = (path: string) => fs.access(path).then(() => true).catch(() => false);
1517
const deleteDir = (p: string) => fs.rm(p, { recursive: true, force: true });
1618

17-
const packageJson = require('./package.json');
1819
const requiredServerVersion = 'v' + packageJson.config['httptoolkit-server-version'];
1920

2021
// For local testing of the desktop app, we need to pull the latest server and unpack it.
@@ -25,7 +26,7 @@ async function setUpLocalEnv() {
2526

2627
if (!serverVersion || semver.neq(serverVersion, requiredServerVersion)) {
2728
if (serverExists) await deleteDir('./httptoolkit-server');
28-
await insertServer(__dirname, os.platform(), os.arch());
29+
await insertServer(import.meta.dirname, os.platform(), os.arch());
2930
console.log('Server setup completed.');
3031
} else {
3132
console.log('Correct server already downloaded.');

skip-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const deleteDir = (p: string) => fs.rm(p, { recursive: true, force: true });
88
// This lets us edit both and the desktop together. We do this by creating a fake server,
99
// which doesn't exit, but otherwise does nothing.
1010
async function setUpDevEnv() {
11-
const serverFolder = path.join(__dirname, 'httptoolkit-server');
11+
const serverFolder = path.join(import.meta.dirname, 'httptoolkit-server');
1212
const serverExists = await canAccess(serverFolder);
1313

1414
if (serverExists) await deleteDir(serverFolder);

src/device.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { promisify } from 'util';
44
import { exec } from 'child_process';
55
const execAsync = promisify(exec);
66

7-
import { logError } from './errors';
7+
import { logError } from './errors.ts';
88

99
export async function getDeviceDetails(): Promise<{
1010
platform: string;

src/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ if (!DEV_MODE) {
1414
// errors unnecessarily distinct, especially on Windows.
1515
root: process.platform === 'win32'
1616
// Root must always be POSIX format, so we transform it on Windows:
17-
? __dirname
17+
? import.meta.dirname
1818
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
1919
.replace(/\\/g, '/') // replace all `\\` instances with `/`
20-
: __dirname
20+
: import.meta.dirname
2121
})
2222
]
2323
});

src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const DEV_MODE = process.env.HTK_DEV === 'true';
22

33
// Set up error handling before everything else:
4-
import { logError, addBreadcrumb } from './errors';
4+
import { logError, addBreadcrumb } from './errors.ts';
55

66
import { spawn, ChildProcess } from 'child_process';
77
import * as os from 'os';
@@ -12,22 +12,22 @@ import * as crypto from 'crypto';
1212
import * as querystring from 'querystring';
1313
import { URL } from 'url';
1414
import { app, BrowserWindow, shell, Menu, dialog, session, ipcMain } from 'electron';
15-
import * as yargs from 'yargs';
15+
import yargs from 'yargs';
1616
import * as semver from 'semver';
1717
const rmRF = (p: string) => fs.rm(p, { recursive: true, force: true });
1818

1919
import windowStateKeeper from 'electron-window-state';
2020
import { getSystemProxy } from 'os-proxy-config';
21-
import registerContextMenu = require('electron-context-menu');
21+
import registerContextMenu from 'electron-context-menu';
2222
import * as sudoPrompt from '@expo/sudo-prompt';
2323
import { getDeferred, delay, ErrorLike } from '@httptoolkit/util';
2424

25-
import { getMenu, shouldAutoHideMenu } from './menu';
26-
import { ContextMenuDefinition, openContextMenu } from './context-menu';
27-
import { stopServer } from './stop-server';
28-
import { getDeviceDetails } from './device';
25+
import { getMenu, shouldAutoHideMenu } from './menu.ts';
26+
import { ContextMenuDefinition, openContextMenu } from './context-menu.ts';
27+
import { stopServer } from './stop-server.ts';
28+
import { getDeviceDetails } from './device.ts';
2929

30-
const packageJson = require('../package.json');
30+
import packageJson from '../package.json' with { type: 'json' };
3131

3232
const isWindows = os.platform() === 'win32';
3333

@@ -81,7 +81,7 @@ const createWindow = (logStream: WriteStream) => {
8181
height: windowState.height,
8282

8383
webPreferences: {
84-
preload: path.join(__dirname, 'preload.js'),
84+
preload: path.join(import.meta.dirname, 'preload.js'),
8585
contextIsolation: true,
8686
nodeIntegration: false
8787
},

src/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { contextBridge, ipcRenderer } from 'electron';
22

3-
import type { ContextMenuDefinition } from './context-menu';
3+
import type { ContextMenuDefinition } from './context-menu.ts';
44

55
// These are technically asynchronous, but they're so fast that
66
// they're effectively sychronously available - this seems to

strip-preload-map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ function removeSourceMapAnnotation(filePath: string): void {
2020
// We have to strip the source map comment from here, otherwise when it loads
2121
// in Chrome, it tries to get preload.js.map from the real server, leaving
2222
// us with an annoying error because that's never going to work.
23-
const filePath = path.join(__dirname, 'build', 'preload.js');
23+
const filePath = path.join(import.meta.dirname, 'build', 'preload.js');
2424
removeSourceMapAnnotation(filePath);

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"rootDir": "./src",
34
"outDir": "./build",
45
"removeComments": false,
56
"preserveConstEnums": true,
@@ -15,6 +16,8 @@
1516
"noEmitHelpers": true,
1617
"module": "node20",
1718
"moduleResolution": "nodenext",
19+
"rewriteRelativeImportExtensions": true,
20+
"skipLibCheck": true,
1821
"types": ["node"],
1922
"pretty": true,
2023
"target": "ES2022",

0 commit comments

Comments
 (0)