Skip to content

akawahuynh/astro-electron-ts

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

48 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ astro-electron-ts

Build cross-platform desktop applications with Astro and Electron. This integration seamlessly incorporates Electron into your Astro projects, handling all the setup and configuration automatically so you can focus on building your app.

preview

✨ Features

  • πŸ”Œ Easy integration of Electron with Astro projects
  • ⚑️ Automatic CLI setup and configuration
  • βš™οΈ Customizable Electron configuration with defaults
  • πŸ› οΈ Support for both new projects and existing Astro projects

πŸ€” Why astro-electron-ts?

  • Actively maintained by the community and aim to support latest Electron versions and Astro
  • Supports both TypeScript and JavaScript out of the box
  • I aim to fix and close any issues as soon as possible

πŸ“¦ Quick Start

Manual Installation (Recommended)

If you prefer to set things up manually, follow these steps:

  1. Install the dependencies:
<package-manager> add astro-electron-ts electron
  1. Add the integration to your astro.config.ts:
import { defineConfig } from 'astro/config';
import electron from 'astro-electron-ts';

export default defineConfig({
  integrations: [electron()],
});
  1. Add the entry point to your package.json:
{
  "main": "dist-electron/main.js"
}
  1. Add to your .gitignore:
# Electron
dist-electron/
  1. Create electron files:
// electron/main.ts
import { app, BrowserWindow } from 'electron';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

process.env.APP_ROOT = path.join(__dirname, '..');

export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL'];
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron');
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist');

process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
  ? path.join(process.env.APP_ROOT, 'public')
  : RENDERER_DIST;

let win: BrowserWindow | null;

function createWindow() {
  win = new BrowserWindow({
    width: 1000,
    height: 800,
    webPreferences: {
      preload: path.join(__dirname, 'preload.mjs'),
    },
  });

  // Test active push message to Renderer-process.
  win.webContents.on('did-finish-load', () => {
    win?.webContents.send('main-process-message', new Date().toLocaleString());
  });

  if (VITE_DEV_SERVER_URL) {
    win.loadURL(VITE_DEV_SERVER_URL);
  } else {
    win.loadFile(path.join(RENDERER_DIST, 'index.html'));
  }
}

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
    win = null;
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

app.whenReady().then(createWindow);
// electron/preload.ts
console.log('preload.ts');

Using the CLI (Unstable)

The easiest way to get started is by using our CLI. But beware: The CLI is really new and is massively being worked on, so it might be unstable or not work at all and probably have bugs.

Steps:

# You run the same command for both new and existing projects
npx astro-electron-ts@latest

The CLI will:

  1. Auto-detect your project setup
  2. Install necessary dependencies using your preferred package manager
  3. Add required configuration files
  4. Set up Electron with TypeScript support

βš™οΈ Configuration

Customize your Electron setup with these configuration options:

export default defineConfig({
  integrations: [
    electron({
      main: {
        entry: 'electron/main.ts', // Path to your Electron main file
        vite: {}, // Vite-specific configurations
      },
      preload: {
        input: 'electron/preload.ts', // Path to your Electron preload file
        vite: {}, // Vite-specific configurations
      },
      renderer: {
        // Renderer-specific configurations
      },
    }),
  ],
});

For more configuration options, check out the vite-plugin-electron docs πŸ“š

🎨 Static Assets

To use static assets (fonts, videos, etc.) in your Electron app:

  • Use the /public directory in your paths explicitly
  • For images, use Image from astro:assets

πŸ—οΈ Building and Publishing

While this integration focuses on development setup, we recommend using Electron Forge for building and publishing your app.

πŸ“„ License

MIT License - do whatever you want with it! πŸŽ‰

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 63.3%
  • JavaScript 26.1%
  • Astro 10.6%