DEV Community

Kev the Dev
Kev the Dev

Posted on

Making a Speedrun Timer: Chapter 4

Another migration.. I'm sorry 😅

I know, it's the last thing you want to see. But I'm hoping this will be the last major one! Since migrating to an Electron.js in the previous post, our code has essentially been broken up into two parts:

  1. Node.js code
  2. Chromium browser code

The chromium code houses the frontend responsible for displaying our timer (the Vue.js stuff). The Node.js code is responsible for our communication to the operating system. However, Node.js handles the importing of modules differently from the V8 engine that powers the chromium browser. Not only that, but a lot of APIs are different between the two.

As a result of this migration, our code is starting to become a bit messy and all over the place. Furthermore, it would be nice to share code between the Node.js API and the Chromium browser API without having to worry about the entanglement of importing strategies. Therefore, I think it would be wise to migrate our app to TypeScript!

Migrating To TypeScript

Once again, I don't really want to go over EVERY single change that went into this. You'll have to checkout the GitHub repo for that.

The Obvious Changes

First thing we want to do is change all of our .js file extensions to .ts. Next, we need to run npm i --save-dev typescript ts-node to install the packages we need.

These changes might seem obvious, but I think the next few changes might be more helpful to those migrating from a Vite + Vue 3 + Electron.js vanilla JavaScript app to a TypeScript version.

The Major Changes

Quite a few changes occur in our forge.config.ts file. We'll be switching a lot of code over to module imports:

// forge.config.ts

// Taken from 
import type { ForgeConfig } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
import { MakerDeb } from '@electron-forge/maker-deb';
import { MakerRpm } from '@electron-forge/maker-rpm';
import { VitePlugin } from '@electron-forge/plugin-vite';

const config: ForgeConfig = {
  packagerConfig: {},
  rebuildConfig: {},
  makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
  plugins: [
    new VitePlugin(
      // `build` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.
      // If you are familiar with Vite configuration, it will look really familiar.
      build: [
          // `entry` is just an alias for `build.lib.entry` in the corresponding file of `config`.
          entry: 'src/main/main.ts',
          config: 'vite.main.config.mts',
          entry: 'src/main/preload.ts',
          config: 'vite.preload.config.mts',
      renderer: [
          name: 'main_window',
          config: 'vite.renderer.config.mts',

export default config;

Enter fullscreen mode Exit fullscreen mode

Another major change we needed to make was exposing certain types at the top of our main.ts file:

// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Vite
// plugin that tells the Electron app where to look for the Vite-bundled app code (depending on
// whether you're running in development or production).
declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string;
declare const MAIN_WINDOW_VITE_NAME: string;
Enter fullscreen mode Exit fullscreen mode

Lastly, we just want to redirect our renderer reference in our index.html file

<!DOCTYPE html>
    <meta charset="UTF-8" />
    <title>Speedrun Timer</title>

    <div id="app"></div>
    <script type="module" src="/src/renderer/renderer.ts"></script>
Enter fullscreen mode Exit fullscreen mode

The Final Changes

Beyond that, most of the remaining code changes modify the Node.js code to use import statements instead of require, adds declarative types to our code, and explicitly informs the vue files to use TypeScript in the script code via <script setup lang="ts">.

Here's our tsconfig.json for reference:

  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "node",
    "target": "ESNext",
    "outDir": "../../dist",
    "strict": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "declaration": true,
    "skipLibCheck": true
  "include": ["**/*.ts", "vite.renderer.config.mts", "vite.renderer.config.mts", "vite.preload.config.mts", "vite.preload.config.mts", "vite.main.config.mts", "vite.main.config.mts"]
Enter fullscreen mode Exit fullscreen mode


It was pretty hard to figure out how to migrate this project TypeScript, as it didn't seem like there was a lot of documentation on my specific configuration. Hopefully somebody finds this useful. I'd like to shoutout this repo for guiding me on how to make this happen.

If you'd like a detailed list of changes, you can look here.

UPDATE: In a follow-up change, we fixed some IDE issues and added a linter.

Next chapter we'll look to finally write new stuff instead of migrating everything!

Top comments (2)

Sloan, the sloth mascot
Comment deleted
kevthedev profile image
Kev the Dev

lol yeah me too 😂