Making a Speedrun Timer: Chapter 4
Tags: javascript, webdev, vue, devjournal
Published: 2023-04-18 07:38:36

## 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](https://dev.to/kevthedev/making-a-speedrun-timer-chapter-3-f8k), 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](https://www.typescriptlang.org/)!

## 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](https://github.com/kdevcse/SpeedrunTimerClient) 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:

```TypeScript
// forge.config.ts

// Taken from https://github.com/caoxiemeihao/electron-forge-vite/blob/main/vite-typescript/tmpl/forge.config.ts 
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;

```

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

```TypeScript
// 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;
```

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

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

  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/renderer/renderer.ts"></script>
  </body>
</html>
```

### 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:
```json
{
  "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"]
}
```

## Conclusion
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](https://github.com/caoxiemeihao/electron-forge-vite) for guiding me on how to make this happen.

If you'd like a detailed list of changes, you can look [here](https://github.com/kdevcse/SpeedrunTimerClient/commit/14dc2d848b3858e0acf2983ede9e1b997a2ce989#diff-fe372c44570e4c8f3bc21566f850113f849b912635a5d80b219bca6ce33fc779).

UPDATE: In a [follow-up change](https://github.com/kdevcse/SpeedrunTimerClient/commit/661f0cdceab9ff06cbfa5726fecc7a1d7c9e4d21), we fixed some IDE issues and added a linter.

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