Basic template
This commit is contained in:
		
							
								
								
									
										37
									
								
								electron-builder.json5
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								electron-builder.json5
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @see https://www.electron.build/configuration/configuration
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  "appId": "YourAppID",
 | 
				
			||||||
 | 
					  "asar": true,
 | 
				
			||||||
 | 
					  "directories": {
 | 
				
			||||||
 | 
					    "output": "release/${version}"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "files": [
 | 
				
			||||||
 | 
					    "dist-electron",
 | 
				
			||||||
 | 
					    "dist"
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
 | 
					  "mac": {
 | 
				
			||||||
 | 
					    "artifactName": "${productName}_${version}.${ext}",
 | 
				
			||||||
 | 
					    "target": [
 | 
				
			||||||
 | 
					      "dmg"
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "win": {
 | 
				
			||||||
 | 
					    "target": [
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        "target": "nsis",
 | 
				
			||||||
 | 
					        "arch": [
 | 
				
			||||||
 | 
					          "x64"
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "artifactName": "${productName}_${version}.${ext}"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "nsis": {
 | 
				
			||||||
 | 
					    "oneClick": false,
 | 
				
			||||||
 | 
					    "perMachine": false,
 | 
				
			||||||
 | 
					    "allowToChangeInstallationDirectory": true,
 | 
				
			||||||
 | 
					    "deleteAppDataOnUninstall": false
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										40
									
								
								electron/main.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								electron/main.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					import { app, BrowserWindow, ipcMain } from 'electron';
 | 
				
			||||||
 | 
					import path from 'path';
 | 
				
			||||||
 | 
					import fs from 'fs/promises';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function createWindow() {
 | 
				
			||||||
 | 
					    const mainWindow = new BrowserWindow({
 | 
				
			||||||
 | 
					        webPreferences: {
 | 
				
			||||||
 | 
					            preload: path.join(__dirname, 'preload.js'),
 | 
				
			||||||
 | 
					            contextIsolation: true,
 | 
				
			||||||
 | 
					            autoplayPolicy: 'no-user-gesture-required',
 | 
				
			||||||
 | 
					            backgroundThrottling: false,
 | 
				
			||||||
 | 
					            nodeIntegration: false,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    mainWindow.loadFile(path.join(__dirname, '../dist/index.html'));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app.on('ready', createWindow);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ipcMain.handle('read-file', async (event: any, filePath: string) => {
 | 
				
			||||||
 | 
					    const userDataPath = app.getPath('userData');
 | 
				
			||||||
 | 
					    const fullPath = path.resolve(userDataPath, filePath);
 | 
				
			||||||
 | 
					    if (!fullPath.startsWith(userDataPath)) throw new Error('Access denied');
 | 
				
			||||||
 | 
					    return fs.readFile(fullPath, { encoding: 'utf-8' });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ipcMain.handle('write-file', async (event: any, filePath: string, content: any) => {
 | 
				
			||||||
 | 
					    const userDataPath = app.getPath('userData');
 | 
				
			||||||
 | 
					    const fullPath = path.resolve(userDataPath, filePath);
 | 
				
			||||||
 | 
					    if (!fullPath.startsWith(userDataPath)) throw new Error('Access denied');
 | 
				
			||||||
 | 
					    await fs.writeFile(fullPath, content, { encoding: 'utf-8' });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ipcMain.handle('list-files', async (event: any, dirPath: string) => {
 | 
				
			||||||
 | 
					    const userDataPath = app.getPath('userData');
 | 
				
			||||||
 | 
					    const fullPath = path.resolve(userDataPath, dirPath);
 | 
				
			||||||
 | 
					    if (!fullPath.startsWith(userDataPath)) throw new Error('Access denied');
 | 
				
			||||||
 | 
					    return fs.readdir(fullPath);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										7
									
								
								electron/preload.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								electron/preload.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					import { contextBridge, ipcRenderer } from 'electron';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					contextBridge.exposeInMainWorld('electronAPI', {
 | 
				
			||||||
 | 
					    writeFile: (filePath: string, content: string) => ipcRenderer.invoke('write-file', filePath, content),
 | 
				
			||||||
 | 
					    readFile: (filePath: string) => ipcRenderer.invoke('read-file', filePath),
 | 
				
			||||||
 | 
					    listFiles: (dirPath: string) => ipcRenderer.invoke('list-files', dirPath)
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										13
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE html>
 | 
				
			||||||
 | 
					<html lang="en">
 | 
				
			||||||
 | 
					  <head>
 | 
				
			||||||
 | 
					    <meta charset="UTF-8" />
 | 
				
			||||||
 | 
					    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
 | 
				
			||||||
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 | 
				
			||||||
 | 
					    <title>Vite + TS</title>
 | 
				
			||||||
 | 
					  </head>
 | 
				
			||||||
 | 
					  <body>
 | 
				
			||||||
 | 
					    <div id="app"></div>
 | 
				
			||||||
 | 
					    <script type="module" src="./src/index.ts"></script>
 | 
				
			||||||
 | 
					  </body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
							
								
								
									
										4806
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										4806
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										29
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "name": "electron-game-quickstart",
 | 
				
			||||||
 | 
					  "version": "1.0.0",
 | 
				
			||||||
 | 
					  "description": "",
 | 
				
			||||||
 | 
					  "main": "dist-electron/main.js",
 | 
				
			||||||
 | 
					  "scripts": {
 | 
				
			||||||
 | 
					    "dev": "vite",
 | 
				
			||||||
 | 
					    "build": "tsc --noEmit && vite build",
 | 
				
			||||||
 | 
					    "preview": "vite preview",
 | 
				
			||||||
 | 
					    "start": "npm run build && electron ./dist",
 | 
				
			||||||
 | 
					    "pack": "electron-builder --dir",
 | 
				
			||||||
 | 
					    "dist": "npm run build && electron-builder"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "keywords": [],
 | 
				
			||||||
 | 
					  "author": "",
 | 
				
			||||||
 | 
					  "license": "ISC",
 | 
				
			||||||
 | 
					  "devDependencies": {
 | 
				
			||||||
 | 
					    "@types/node": "^20.11.26",
 | 
				
			||||||
 | 
					    "electron": "^29.1.2",
 | 
				
			||||||
 | 
					    "electron-builder": "^24.13.3",
 | 
				
			||||||
 | 
					    "electron-reload": "^2.0.0-alpha.1",
 | 
				
			||||||
 | 
					    "ts-node": "^10.9.2",
 | 
				
			||||||
 | 
					    "typescript": "^5.4.2",
 | 
				
			||||||
 | 
					    "vite": "^5.1.6"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "vite-plugin-electron": "^0.28.3"
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										11
									
								
								src/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE html>
 | 
				
			||||||
 | 
					<html lang="en">
 | 
				
			||||||
 | 
					<head>
 | 
				
			||||||
 | 
					    <meta charset="UTF-8">
 | 
				
			||||||
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
				
			||||||
 | 
					    <title>Game template</title>
 | 
				
			||||||
 | 
					</head>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					    <script type="module" src="index.ts"></script>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
							
								
								
									
										1
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					alert(`Hi from Electron`);
 | 
				
			||||||
							
								
								
									
										19
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "compilerOptions": {
 | 
				
			||||||
 | 
					    "target": "ESNext",
 | 
				
			||||||
 | 
					    "useDefineForClassFields": true,
 | 
				
			||||||
 | 
					    "module": "ESNext",
 | 
				
			||||||
 | 
					    "lib": [
 | 
				
			||||||
 | 
					      "ESNext",
 | 
				
			||||||
 | 
					      "DOM"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "moduleResolution": "Node",
 | 
				
			||||||
 | 
					    "strict": true,
 | 
				
			||||||
 | 
					    "sourceMap": true,
 | 
				
			||||||
 | 
					    "resolveJsonModule": true,
 | 
				
			||||||
 | 
					    "esModuleInterop": true,
 | 
				
			||||||
 | 
					    "noEmit": true,
 | 
				
			||||||
 | 
					    "noImplicitReturns": true,
 | 
				
			||||||
 | 
					    "skipLibCheck": true
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								vite.config.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								vite.config.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
 | 
					import electron from 'vite-plugin-electron'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default defineConfig({
 | 
				
			||||||
 | 
					  plugins: [
 | 
				
			||||||
 | 
					    electron([
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        // Main-Process entry file of the Electron App.
 | 
				
			||||||
 | 
					        entry: 'electron/main.ts',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        entry: 'electron/preload.ts',
 | 
				
			||||||
 | 
					        onstart(options) {
 | 
				
			||||||
 | 
					          // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete, 
 | 
				
			||||||
 | 
					          // instead of restarting the entire Electron App.
 | 
				
			||||||
 | 
					          options.reload()
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    ]),
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
		Reference in New Issue
	
	Block a user