Skip to content

Commit

Permalink
can run webpack dev server and electron
Browse files Browse the repository at this point in the history
wired up electron index.js so that we load from server when in dev
began adding deps for i18n in this template
  • Loading branch information
reZach committed Jan 4, 2020
1 parent 4a32f1d commit 72952a2
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 12 deletions.
21 changes: 14 additions & 7 deletions app/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
session
} = require("electron");
const path = require("path");
const isDev = process.env.NODE_ENV === "development";
const port = 40992; // Hardcoded; needs to match webpack.development.js and package.json
const selfHost = `http://localhost:${port}`;

// Keep a global reference of the window object, if you don"t, the window will
// be closed automatically when the JavaScript object is garbage collected.
Expand All @@ -15,7 +18,7 @@ function createWindow() {
width: 800,
height: 600,
webPreferences: {
devTools: true, // todo set to false in prod
devTools: isDev,
nodeIntegration: false,
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
Expand All @@ -26,7 +29,12 @@ function createWindow() {
});

// Load app
win.loadFile(path.join(__dirname, "../dist/index.html"));
if (isDev) {
win.loadURL(selfHost);
win.webContents.openDevTools();
} else {
win.loadFile(path.join(__dirname, "../dist/index.html"));
}

// Emitted when the window is closed.
win.on("closed", () => {
Expand All @@ -46,7 +54,7 @@ function createWindow() {
callback(true); // Approve permission request
} else {
console.error(`The application tried to request permission for '${permission}'. This permission was not whitelisted and has been blocked.`);

callback(false); // Deny
}
});
Expand Down Expand Up @@ -78,7 +86,7 @@ app.on("activate", () => {
app.on("web-contents-created", (event, contents) => {
contents.on("will-navigate", (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl);
const validOrigins = [];
const validOrigins = [selfHost];

// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
if (!validOrigins.includes(parsedUrl.origin)) {
Expand All @@ -88,6 +96,7 @@ app.on("web-contents-created", (event, contents) => {
return;
}
});

contents.on("will-redirect", (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl);
const validOrigins = [];
Expand All @@ -100,10 +109,8 @@ app.on("web-contents-created", (event, contents) => {
return;
}
});
});

// https://electronjs.org/docs/tutorial/security#13-disable-or-limit-creation-of-new-windows
app.on("web-contents-created", (event, contents) => {
// https://electronjs.org/docs/tutorial/security#13-disable-or-limit-creation-of-new-windows
contents.on("new-window", async (event, navigationUrl) => {

// Log and prevent opening up a new window
Expand Down
127 changes: 125 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
"private": true,
"main": "app/electron/main.js",
"scripts": {
"dev": "concurrently \"webpack-dev-server --config ./webpack.development.js\" \"opener http://localhost:40992\"",
"electron": "npx webpack --config ./webpack.production.js && electron ."
"dev-server": "cross-env NODE_ENV=development webpack-dev-server --config ./webpack.development.js",
"open-local": "opener http://localhost:40992",
"dev-slim": "concurrently \"npm run dev-server\" \"npm run open-local\"",
"dev": "concurrently \"npm run dev-server\" \"cross-env NODE_ENV=development electron .\"",
"prod": "cross-env NODE_ENV=production npx webpack --mode=production --config ./webpack.production.js && electron ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/reZach/secure-electron-template.git"
},
"keywords": [],
"author": "",
"author": "reZach",
"license": "GPL-3.0-only",
"bugs": {
"url": "https://github.com/reZach/secure-electron-template/issues"
Expand All @@ -27,6 +30,7 @@
"babel-loader": "^8.0.6",
"babel-plugin-module-resolver": "^4.0.0",
"concurrently": "^5.0.2",
"cross-env": "^6.0.3",
"csp-html-webpack-plugin": "^3.0.4",
"electron": "^7.1.7",
"html-loader": "^0.5.5",
Expand All @@ -40,8 +44,10 @@
"dependencies": {
"@reduxjs/toolkit": "^1.2.1",
"connected-react-router": "^6.6.1",
"i18next-node-fs-backend": "^2.1.3",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-i18next": "^11.2.7",
"react-redux": "^7.1.3",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
Expand Down

0 comments on commit 72952a2

Please sign in to comment.