Igloczek / astro-electron

Build cross-platform desktop apps with Astro and Electron
20 stars 3 forks source link

TypeScript? #4

Open ultimateshadsform opened 4 weeks ago

ultimateshadsform commented 4 weeks ago

It would be nice to convert to typescript so both ts and js can work. Currently using TypeScript in a project.

ultimateshadsform commented 4 weeks ago

Here is my example config:

import fs from 'fs/promises';
import path from 'path';
import vitePluginElectron from 'vite-plugin-electron/simple';
import type { AstroIntegration, AstroConfig, RouteData } from 'astro';
import type { UserConfig as ViteUserConfig } from 'vite';

interface ElectronIntegrationConfig {
  main?: {
    entry?: string;
    vite?: Partial<ViteUserConfig>;
  };
  preload?: {
    input?: string;
    vite?: Partial<ViteUserConfig>;
  };
  renderer?: Partial<ViteUserConfig>;
}

export default (
  integrationConfig: ElectronIntegrationConfig = {},
): AstroIntegration => ({
  name: 'astro-electron',
  hooks: {
    'astro:config:setup': ({
      config,
      command,
      updateConfig,
    }: {
      config: AstroConfig;
      command: string;
      updateConfig: (newConfig: Partial<AstroConfig>) => void;
    }) => {
      if (command === 'build') {
        updateConfig({
          base: '/',
        });
      }

      // Add Vite plugin for Electron
      updateConfig({
        vite: {
          plugins: [
            vitePluginElectron({
              main: {
                entry: integrationConfig?.main?.entry || 'src/electron/main.ts',
                vite: integrationConfig?.main?.vite || config.vite,
              },
              preload: {
                input:
                  integrationConfig?.preload?.input ||
                  'src/electron/preload.ts',
                vite: integrationConfig?.preload?.vite || config.vite,
              },
              renderer: integrationConfig?.renderer || undefined,
            }),
          ],
        },
      });
    },
    'astro:build:done': async ({
      dir,
      routes,
    }: {
      dir: URL;
      routes: RouteData[];
      // ... other properties
    }) => {
      await Promise.all(
        routes.map(async (route) => {
          if (route.distURL) {
            const filePath = new URL(route.distURL).pathname;
            const file = await fs.readFile(filePath, 'utf-8');
            const localDir = path.dirname(filePath);
            const relativePath = path.relative(localDir, new URL(dir).pathname);

            await fs.writeFile(
              route.distURL,
              file.replaceAll(
                /\/(astro-electron|public)/g,
                relativePath || '.',
              ),
            );
          }
        }),
      );
    },
  },
});
Igloczek commented 3 weeks ago

Hey, that's a good idea, can you make a PR that will handle all the necessary changes?

ultimateshadsform commented 3 weeks ago

Yeah sure! #5