Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript? #4

Open
ultimateshadsform opened this issue Sep 21, 2024 · 3 comments
Open

TypeScript? #4

ultimateshadsform opened this issue Sep 21, 2024 · 3 comments

Comments

@ultimateshadsform
Copy link

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

@ultimateshadsform
Copy link
Author

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
Copy link
Owner

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

@ultimateshadsform
Copy link
Author

ultimateshadsform commented Sep 21, 2024

Yeah sure! #5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants