mirror of
https://github.com/LukeHagar/sveltekit-adapters.git
synced 2025-12-06 04:21:32 +00:00
chore: Update package dependencies and configuration for Electron adapter
- Removed package manager and engines from package.json. - Updated pnpm-lock.yaml to reflect new versions of dependencies. - Modified pnpm-workspace.yaml to include only built dependencies for Electron. - Adjusted electron-builder.yaml to exclude unnecessary files and added output directory. - Deleted obsolete electron.vite.config.ts file. - Updated main entry point in examples/electron/package.json to use CommonJS format. - Refactored scripts in examples/electron/package.json for better development experience. - Enhanced error handling and logging in game logic within examples/electron/src/routes/sverdle/+page.server.ts. - Updated adapter-electron to support new protocol handling and build processes.
This commit is contained in:
@@ -6,13 +6,16 @@ files:
|
||||
- "!**/.vscode/*"
|
||||
- "!src/*"
|
||||
- "!electron.vite.config.{js,ts,mjs,cjs}"
|
||||
- "!vite.electron.config.ts"
|
||||
- "!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}"
|
||||
- "!{.env,.env.*,.npmrc,pnpm-lock.yaml}"
|
||||
- "!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}"
|
||||
- "out/**/*"
|
||||
asarUnpack:
|
||||
- resources/**
|
||||
# afterSign: build/notarize.js
|
||||
win:
|
||||
target: ["portable"]
|
||||
executableName: electron-sveltekit
|
||||
nsis:
|
||||
artifactName: ${name}-${version}-setup.${ext}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { defineConfig, defineViteConfig } from 'electron-vite';
|
||||
import config from './vite.config';
|
||||
|
||||
export default defineConfig({
|
||||
main: defineViteConfig({}),
|
||||
preload: defineViteConfig({}),
|
||||
renderer: config
|
||||
});
|
||||
@@ -8,11 +8,11 @@
|
||||
"email": "lukeslakemail@gmail.com"
|
||||
},
|
||||
"homepage": "https://github.com/lukehagar/sveltekit-adapters",
|
||||
"main": "./out/main/index.js",
|
||||
"main": "./out/main/index.cjs",
|
||||
"scripts": {
|
||||
"start": "electron-vite preview",
|
||||
"dev": "svelte-kit sync && electron-vite dev",
|
||||
"build": "electron-vite build",
|
||||
"start": "vite preview",
|
||||
"dev": "svelte-kit sync && concurrently \"vite dev\" \"electron .\" --names \"sveltekit,electron\" --prefix-colors \"#ff3e00,blue\"",
|
||||
"build": "vite build",
|
||||
"build:all": "npm run build && electron-builder -mwl --config",
|
||||
"build:win": "npm run build && electron-builder --win --config",
|
||||
"build:mac": "npm run build && electron-builder --mac --config",
|
||||
@@ -24,7 +24,7 @@
|
||||
"@sveltejs/kit": "^2.22.2",
|
||||
"@sveltejs/vite-plugin-svelte": "^5.1.0",
|
||||
"@types/eslint": "9.6.1",
|
||||
"@types/node": "^24.0.10",
|
||||
"@types/node": "^24.0.11",
|
||||
"@typescript-eslint/eslint-plugin": "^8.35.1",
|
||||
"@typescript-eslint/parser": "^8.35.1",
|
||||
"adapter-electron": "workspace:*",
|
||||
@@ -36,7 +36,7 @@
|
||||
"electron-is-dev": "^3.0.1",
|
||||
"electron-log": "^5.4.1",
|
||||
"electron-util": "^0.18.1",
|
||||
"electron-vite": "^3.1.0",
|
||||
"esbuild": "^0.25.0",
|
||||
"eslint": "^9.30.1",
|
||||
"eslint-config-prettier": "^10.1.5",
|
||||
"eslint-plugin-svelte": "^3.10.1",
|
||||
|
||||
57
examples/electron/src/main.ts
Normal file
57
examples/electron/src/main.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { app, BrowserWindow } from 'electron';
|
||||
import { setupHandler, getPreloadPath, registerAppScheme } from 'adapter-electron/functions/setupHandler';
|
||||
import log from 'electron-log/main';
|
||||
|
||||
console.log = log.log;
|
||||
|
||||
let mainWindow: BrowserWindow | null = null;
|
||||
let stopIntercept: (() => void) | undefined;
|
||||
|
||||
process.on('SIGTERM', () => process.exit(0));
|
||||
process.on('SIGINT', () => process.exit(0));
|
||||
|
||||
// First register the app scheme
|
||||
registerAppScheme();
|
||||
|
||||
|
||||
async function createWindow() {
|
||||
// Create the browser window
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
webPreferences: {
|
||||
// Second configure the preload script
|
||||
preload: getPreloadPath(),
|
||||
contextIsolation: true,
|
||||
devTools: true
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.once('ready-to-show', () => mainWindow?.webContents.openDevTools());
|
||||
|
||||
mainWindow.on('closed', () => {
|
||||
mainWindow = null;
|
||||
stopIntercept?.();
|
||||
});
|
||||
|
||||
await app.whenReady();
|
||||
|
||||
// Third setup the handler
|
||||
stopIntercept = await setupHandler(mainWindow);
|
||||
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
app.on('ready', createWindow);
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
app.on('activate', async () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0 && !mainWindow) {
|
||||
await createWindow();
|
||||
}
|
||||
});
|
||||
@@ -1,71 +0,0 @@
|
||||
import { app, BrowserWindow } from 'electron';
|
||||
import { start, load } from 'adapter-electron/functions';
|
||||
import isDev from 'electron-is-dev';
|
||||
import log from 'electron-log/main';
|
||||
import nodePath from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
// Handle __dirname in ES modules
|
||||
const __dirname = nodePath.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
log.info('Starting Electron app with SvelteKit protocol integration...');
|
||||
|
||||
// Initialize the protocol manager
|
||||
const port = await start();
|
||||
|
||||
async function createWindow() {
|
||||
// Create the browser window
|
||||
const mainWindow = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
webPreferences: {
|
||||
preload: nodePath.join(__dirname, '../preload/index.mjs'),
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
webSecurity: true
|
||||
}
|
||||
});
|
||||
|
||||
// Load the app - all routing is handled by protocol interception
|
||||
load(mainWindow);
|
||||
|
||||
if (isDev) {
|
||||
mainWindow.webContents.openDevTools();
|
||||
}
|
||||
|
||||
// Handle window events
|
||||
mainWindow.webContents.on('did-finish-load', () => {
|
||||
log.info('Window loaded successfully');
|
||||
});
|
||||
|
||||
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
|
||||
log.error('Window failed to load:', errorDescription);
|
||||
});
|
||||
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
log.info('App is ready');
|
||||
|
||||
log.info('Creating window...');
|
||||
await createWindow();
|
||||
|
||||
app.on('activate', async () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
await createWindow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
// Handle render process crashes
|
||||
app.on('render-process-gone', (event, webContents, details) => {
|
||||
log.error('Render process crashed:', details.reason);
|
||||
// You could restart the window here if needed
|
||||
});
|
||||
1
examples/electron/src/preload.ts
Normal file
1
examples/electron/src/preload.ts
Normal file
@@ -0,0 +1 @@
|
||||
console.log('Preload loaded');
|
||||
@@ -1,10 +0,0 @@
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const replaceText = (selector, text) => {
|
||||
const element = document.getElementById(selector);
|
||||
if (element) element.innerText = text;
|
||||
};
|
||||
|
||||
for (const type of ['chrome', 'node', 'electron']) {
|
||||
replaceText(`${type}-version`, process.versions[type]);
|
||||
}
|
||||
});
|
||||
@@ -3,25 +3,35 @@ import { Game } from './game';
|
||||
import type { PageServerLoad, Actions } from './$types';
|
||||
|
||||
export const load = (({ cookies }) => {
|
||||
const game = new Game(cookies.get('sverdle'));
|
||||
console.log("Loading game, getting cookie");
|
||||
|
||||
return {
|
||||
/**
|
||||
* The player's guessed words so far
|
||||
*/
|
||||
guesses: game.guesses,
|
||||
try {
|
||||
const game = new Game(cookies.get('sverdle'));
|
||||
|
||||
/**
|
||||
* An array of strings like '__x_c' corresponding to the guesses, where 'x' means
|
||||
* an exact match, and 'c' means a close match (right letter, wrong place)
|
||||
*/
|
||||
answers: game.answers,
|
||||
const gameState = {
|
||||
/**
|
||||
* The player's guessed words so far
|
||||
*/
|
||||
guesses: game.guesses,
|
||||
|
||||
/**
|
||||
* The correct answer, revealed if the game is over
|
||||
*/
|
||||
answer: game.answers.length >= 6 ? game.answer : null
|
||||
};
|
||||
/**
|
||||
* An array of strings like '__x_c' corresponding to the guesses, where 'x' means
|
||||
* an exact match, and 'c' means a close match (right letter, wrong place)
|
||||
*/
|
||||
answers: game.answers,
|
||||
|
||||
/**
|
||||
* The correct answer, revealed if the game is over
|
||||
*/
|
||||
answer: game.answers.length >= 6 ? game.answer : null
|
||||
}
|
||||
|
||||
console.log("Returning game state", gameState);
|
||||
|
||||
return gameState
|
||||
} catch (e) {
|
||||
console.log("Error getting cookie", e);
|
||||
}
|
||||
}) satisfies PageServerLoad;
|
||||
|
||||
export const actions = {
|
||||
@@ -30,6 +40,7 @@ export const actions = {
|
||||
* is available, this will happen in the browser instead of here
|
||||
*/
|
||||
update: async ({ request, cookies }) => {
|
||||
console.log("Updating game, getting cookie");
|
||||
const game = new Game(cookies.get('sverdle'));
|
||||
|
||||
const data = await request.formData();
|
||||
@@ -43,7 +54,9 @@ export const actions = {
|
||||
game.guesses[i] += key;
|
||||
}
|
||||
|
||||
cookies.set('sverdle', game.toString(), { path: '/' });
|
||||
const gameString = game.toString();
|
||||
console.log("Setting cookie", gameString);
|
||||
cookies.set('sverdle', gameString, { path: '/' });
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -51,19 +64,27 @@ export const actions = {
|
||||
* the server, so that people can't cheat by peeking at the JavaScript
|
||||
*/
|
||||
enter: async ({ request, cookies }) => {
|
||||
const game = new Game(cookies.get('sverdle'));
|
||||
console.log("Entering guess, getting cookie");
|
||||
try {
|
||||
const game = new Game(cookies.get('sverdle'));
|
||||
|
||||
const data = await request.formData();
|
||||
const guess = data.getAll('guess') as string[];
|
||||
const data = await request.formData();
|
||||
const guess = data.getAll('guess') as string[];
|
||||
|
||||
if (!game.enter(guess)) {
|
||||
return fail(400, { badGuess: true });
|
||||
if (!game.enter(guess)) {
|
||||
return fail(400, { badGuess: true });
|
||||
}
|
||||
|
||||
const gameString = game.toString();
|
||||
console.log("Setting cookie", gameString);
|
||||
cookies.set('sverdle', gameString, { path: '/' });
|
||||
} catch (e) {
|
||||
console.log("Error entering guess", e);
|
||||
}
|
||||
|
||||
cookies.set('sverdle', game.toString(), { path: '/' });
|
||||
},
|
||||
|
||||
restart: async ({ cookies }) => {
|
||||
console.log("Restarting game, deleting cookie");
|
||||
cookies.delete('sverdle', { path: '/' });
|
||||
}
|
||||
} satisfies Actions;
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { sveltekit } from '@sveltejs/kit/vite';
|
||||
import { defineConfig } from 'vite';
|
||||
import { electronPlugin } from 'adapter-electron';
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
target: 'chrome107'
|
||||
},
|
||||
logLevel: 'info',
|
||||
plugins: [sveltekit()]
|
||||
plugins: [
|
||||
sveltekit(),
|
||||
electronPlugin({
|
||||
// The plugin will auto-detect src/main.ts and src/preload.ts
|
||||
// You can override these paths if needed:
|
||||
// mainEntry: 'src/main.ts',
|
||||
// preloadEntry: 'src/preload.ts',
|
||||
// mainOut: 'out/main/index.js',
|
||||
// preloadOut: 'out/preload/index.js'
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user