Skip to content

Commit

Permalink
build preload script standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
jctaoo committed Aug 27, 2024
1 parent b66468d commit 817d216
Show file tree
Hide file tree
Showing 13 changed files with 1,062 additions and 10 deletions.
20 changes: 20 additions & 0 deletions fixtures/esm-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"elecrun": "elecrun",
"dev": "elecrun --vite --preload preload.ts --esm",
"build": "elecrun build",
"clean": "elecrun clean"
},
"devDependencies": {
"@types/node": "^22.5.0",
"vite": "^2.1.5"
},
"dependencies": {
"electron": "^32.0.1",
"elecrun": "file:../../"
}
}
3 changes: 3 additions & 0 deletions fixtures/esm-demo/src/main/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add(lhs: number, rhs: number) {
return lhs + rhs;
}
35 changes: 35 additions & 0 deletions fixtures/esm-demo/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { app, BrowserWindow } from 'electron';
import path from 'path';
import { add } from './add';

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(import.meta.dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});

console.log('1 + 1 =', add(1, 1));
win.loadURL('http://localhost:3000').then();
win.webContents.openDevTools({ mode: 'detach' });
}

app.whenReady().then(() => {
createWindow();

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

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
18 changes: 18 additions & 0 deletions fixtures/esm-demo/src/main/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
contextBridge,
ipcRenderer,
IpcRendererEvent,
} from "electron";

contextBridge.exposeInMainWorld("bridge", {
sendIpc: ipcRenderer.send,
listenIpc: (
channel: string,
callBack: (event: IpcRendererEvent, arg: any) => void
) => {
ipcRenderer.on(channel, (event, arg) => {
callBack(event, arg);
});
},
});

12 changes: 12 additions & 0 deletions fixtures/esm-demo/src/main/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
"moduleDetection": "force",
"target": "ESNext",
"outDir": "../../app"
},
"include": ["../main/**/*", "../common/**/*"],
"exclude": ["../renderer/**/*"]
}
11 changes: 11 additions & 0 deletions fixtures/esm-demo/src/renderer/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
window.onload = () => {
let count = 0;

const counterLabel = document.querySelector(".label")!;
const btn = document.querySelector(".add-btn")!;

btn.addEventListener('click', () => {
count += 1;
counterLabel.textContent = `updated ${count}`;
});
}
17 changes: 17 additions & 0 deletions fixtures/esm-demo/src/renderer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta http-equiv='Content-Security-Policy'
content="default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline';">
<title>Title</title>
</head>
<body>
<div id='body'>
<h1>Hello World</h1>
<h5 class='label'>0</h5>
<button class='add-btn'>+1</button>
</div>
<script src='./app.ts' type='module'></script>
</body>
</html>
22 changes: 22 additions & 0 deletions fixtures/esm-demo/src/renderer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
],
"types": [
"vite/client"
]
},
"include": [
"../renderer/**/*",
"../common/**/*"
],
"exclude": [
"../main/**/*"
]
}
27 changes: 27 additions & 0 deletions fixtures/esm-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
"moduleDetection": "force",
"target": "ESNext",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"allowJs": true,
"checkJs": true,
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"importHelpers": true,
"sourceMap": true,
"baseUrl": "./src"
},
"exclude": ["node_modules", "app", "dist"]
}
13 changes: 13 additions & 0 deletions fixtures/esm-demo/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vite";

const rendererPath = "./src/renderer"
const outDirRenderer = "./build"

export default defineConfig({
base: "./",
root: rendererPath,
build: {
outDir: outDirRenderer,
emptyOutDir: true,
},
});
Loading

0 comments on commit 817d216

Please sign in to comment.