Basic template
parent
d11fc1c967
commit
41545ea763
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
});
|
|
@ -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)
|
||||
});
|
|
@ -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>
|
File diff suppressed because it is too large
Load Diff
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -0,0 +1 @@
|
|||
alert(`Hi from Electron`);
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
},
|
||||
},
|
||||
]),
|
||||
],
|
||||
})
|
Loading…
Reference in New Issue