react-native-electron-platform 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ import { BrowserWindow, screen, session } from "electron";
2
+ import path from "path";
3
+ import fs from "fs";
4
+ import { app, dialog } from "electron";
5
+
6
+ export function createMainWindow(__dirname) {
7
+ const primaryDisplay = screen.getPrimaryDisplay();
8
+ const { x, y, width, height } = primaryDisplay.bounds;
9
+
10
+ const iconPath = path.join(app.getAppPath(), "electron/icon.ico");
11
+ const preloadPath = path.join(__dirname, "preload.mjs");
12
+
13
+ const mainWindow = new BrowserWindow({
14
+ x,
15
+ y,
16
+ width,
17
+ height,
18
+ show: false,
19
+ icon: iconPath,
20
+ frame: true,
21
+ webPreferences: {
22
+ preload: preloadPath,
23
+ nodeIntegration: false,
24
+ contextIsolation: true,
25
+ sandbox: false,
26
+ webSecurity: false,
27
+ disableBlinkFeatures: "AutoLoadIconsForPage",
28
+ nativeWindowOpen: true,
29
+ spellcheck: true,
30
+ },
31
+ });
32
+
33
+ mainWindow.removeMenu();
34
+ mainWindow.maximize();
35
+ mainWindow.show();
36
+
37
+ mainWindow.on("resize", () => {
38
+ const bounds = mainWindow.getBounds();
39
+ if (bounds?.width && bounds?.height) {
40
+ console.log(`Window resized: ${bounds.width}x${bounds.height}`);
41
+ }
42
+ });
43
+
44
+ // CORS handling
45
+ setupCorsHandling();
46
+
47
+ // Load the app
48
+ loadAppContent(mainWindow, __dirname);
49
+
50
+ // Setup dev tools shortcuts in development
51
+ if (isDevMode()) {
52
+ setupDevToolsShortcuts(mainWindow);
53
+ }
54
+
55
+ mainWindow.webContents.on("did-finish-load", () => {
56
+ console.log("Page loaded successfully");
57
+ });
58
+
59
+ return mainWindow;
60
+ }
61
+
62
+ function setupCorsHandling() {
63
+ session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
64
+ const responseHeaders = { ...details.responseHeaders };
65
+
66
+ delete responseHeaders["access-control-allow-origin"];
67
+ delete responseHeaders["access-control-allow-headers"];
68
+ delete responseHeaders["access-control-allow-methods"];
69
+ delete responseHeaders["Access-Control-Allow-Origin"];
70
+ delete responseHeaders["Access-Control-Allow-Headers"];
71
+ delete responseHeaders["Access-Control-Allow-Methods"];
72
+
73
+ callback({
74
+ responseHeaders: {
75
+ ...responseHeaders,
76
+ "Access-Control-Allow-Origin": ["*"],
77
+ "Access-Control-Allow-Headers": ["*"],
78
+ "Access-Control-Allow-Methods": ["GET, POST, PUT, DELETE, OPTIONS"],
79
+ },
80
+ });
81
+ });
82
+ }
83
+
84
+ function isDevMode() {
85
+ return process.argv.includes("--enable-remote-module") ||
86
+ process.env.NODE_ENV === "development";
87
+ }
88
+
89
+ function loadAppContent(mainWindow, __dirname) {
90
+ const isDev = isDevMode();
91
+
92
+ if (isDev) {
93
+ mainWindow.loadURL("http://localhost:5001");
94
+ console.log("DEV MODE: http://localhost:5001");
95
+ } else {
96
+ const possiblePaths = [
97
+ path.join(__dirname, "web-build/index.html"),
98
+ path.join(__dirname, "../web-build/index.html"),
99
+ path.join(__dirname, "../../web-build/index.html"),
100
+ path.join(app.getAppPath(), "web-build/index.html"),
101
+ ];
102
+
103
+ let indexPath = null;
104
+ for (const p of possiblePaths) {
105
+ if (fs.existsSync(p)) {
106
+ indexPath = p;
107
+ break;
108
+ }
109
+ }
110
+
111
+ console.log("Loading:", indexPath);
112
+
113
+ if (!indexPath) {
114
+ dialog.showErrorBox(
115
+ "Application Error",
116
+ `web-build/index.html not found. Tried: ${possiblePaths.join(", ")}`
117
+ );
118
+ app.quit();
119
+ return;
120
+ }
121
+
122
+ mainWindow.loadFile(indexPath);
123
+ }
124
+ }
125
+
126
+ function setupDevToolsShortcuts(mainWindow) {
127
+ mainWindow.webContents.on("before-input-event", (event, input) => {
128
+ if (input.control && input.shift && input.key.toLowerCase() === "i") {
129
+ mainWindow.webContents.toggleDevTools();
130
+ event.preventDefault();
131
+ } else if (input.key === "F12") {
132
+ mainWindow.webContents.toggleDevTools();
133
+ event.preventDefault();
134
+ } else if (
135
+ input.key === "F5" ||
136
+ (input.control && input.key.toLowerCase() === "r")
137
+ ) {
138
+ mainWindow.webContents.reload();
139
+ event.preventDefault();
140
+ }
141
+ });
142
+ }
@@ -2,7 +2,7 @@ import path from 'path';
2
2
  import { fileURLToPath } from 'url';
3
3
  import { dirname } from 'path';
4
4
  import fs from 'fs';
5
- import { WEB_UNSUPPORTED_PACKAGES } from '../test/electron/nonmodules.mjs';
5
+ import { WEB_UNSUPPORTED_PACKAGES } from '../../../electron/nonmodules.mjs';
6
6
 
7
7
  const __dirname = dirname(fileURLToPath(import.meta.url));
8
8
 
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "my-electron-app",
3
+ "version": "1.0.0",
4
+ "description": "A React Native app with Electron support using react-native-electron-platform",
5
+ "private": true,
6
+ "main": "node_modules/react-native-electron-platform/index.mjs",
7
+ "scripts": {
8
+ "android": "react-native run-android",
9
+ "ios": "react-native run-ios",
10
+ "lint": "eslint .",
11
+ "start": "react-native start",
12
+ "test": "jest",
13
+ "web": "webpack serve --config node_modules/react-native-electron-platform/webpack.config.mjs --mode development",
14
+ "web:build": "webpack --config node_modules/react-native-electron-platform/webpack.config.mjs --mode production",
15
+ "electron": "cross-env NODE_ENV=development concurrently \"npm run web\" \"wait-on http://localhost:5001 && electron .\"",
16
+ "electron:dev": "cross-env NODE_ENV=development electron . --enable-remote-module",
17
+ "electron:build": "npm run web:build && electron-builder --config node_modules/react-native-electron-platform/electron-builder.json",
18
+ "electron:build:nsis": "npm run web:build && electron-builder --config node_modules/react-native-electron-platform/electron-builder.json --win nsis --publish never",
19
+ "electron:build:msi": "npm run web:build && electron-builder --config node_modules/react-native-electron-platform/electron-builder.json --win msi --publish never"
20
+ },
21
+ "dependencies": {
22
+ "react": "^18.0.0",
23
+ "react-native": "^0.71.0",
24
+ "react-native-electron-platform": "^0.0.12",
25
+ "cross-env": "^7.0.3",
26
+ "concurrently": "^8.0.0",
27
+ "wait-on": "^7.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "webpack": "^5.0.0",
31
+ "webpack-cli": "^5.0.0",
32
+ "webpack-dev-server": "^4.0.0",
33
+ "html-webpack-plugin": "^5.0.0",
34
+ "@babel/core": "^7.0.0",
35
+ "@babel/preset-react": "^7.0.0",
36
+ "@babel/preset-env": "^7.0.0",
37
+ "babel-loader": "^9.0.0",
38
+ "electron": "^25.0.0",
39
+ "electron-builder": "^24.0.0",
40
+ "eslint": "^8.0.0",
41
+ "jest": "^29.0.0"
42
+ }
43
+ }