add preload.js script to electron main

This commit is contained in:
Enrique Hernandez 2020-10-07 13:03:01 -05:00
commit 04d694619e
8 changed files with 1847 additions and 55 deletions

3
.gitignore vendored
View file

@ -21,3 +21,6 @@ pnpm-debug.log*
*.njsproj
*.sln
*.sw?
#Electron-builder output
/dist_electron

View file

@ -6,8 +6,13 @@
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"core-js": "^3.6.5",
"vue": "^3.0.0-0",
@ -28,12 +33,16 @@
"@vue/compiler-sfc": "^3.0.0-0",
"@vue/eslint-config-typescript": "^5.0.2",
"@vue/test-utils": "^2.0.0-0",
"electron": "^9.0.0",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0-0",
"lint-staged": "^9.5.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"spectron": "^11.0.0",
"typescript": "~3.9.3",
"vue-cli-plugin-electron-builder": "~2.0.0-rc.4",
"vue-jest": "^5.0.0-0"
},
"gitHooks": {

View file

@ -11,6 +11,10 @@
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<!-- <h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>, Chromium
<span id="chrome-version"></span>, and Electron
<span id="electron-version"></span>. -->
<div id="app"></div>
<!-- built files will be auto injected -->
</body>

92
src/background.ts Normal file
View file

@ -0,0 +1,92 @@
"use strict";
import { app, protocol, BrowserWindow } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
const isDevelopment = process.env.NODE_ENV !== "production";
import * as path from "path";
// 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.
let win: BrowserWindow | null;
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: "app", privileges: { secure: true, standard: true } }
]);
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: (process.env
.ELECTRON_NODE_INTEGRATION as unknown) as boolean,
preload: path.join(__dirname, "preload.js")
}
});
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
if (!process.env.IS_TEST) win.webContents.openDevTools();
} else {
createProtocol("app");
// Load the index.html when not in development
win.loadURL("app://./index.html");
}
win.on("closed", () => {
win = null;
});
}
// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS);
} catch (e) {
console.error("Vue Devtools failed to install:", e.toString());
}
}
createWindow();
});
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === "win32") {
process.on("message", data => {
if (data === "graceful-exit") {
app.quit();
}
});
} else {
process.on("SIGTERM", () => {
app.quit();
});
}
}

27
src/preload.ts Normal file
View file

@ -0,0 +1,27 @@
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
import ipcRenderer from "electron";
process.once("loaded", () => {
window.addEventListener("message", event => {
// do something with custom event
const message = event.data;
if (message.myTypeField === "handle-audio-stream") {
ipcRenderer.send("audio-stream", message);
}
});
});
window.addEventListener("DOMContentLoaded", () => {
const replaceText = (selector: string, text: string) => {
const element = document.getElementById(selector);
if (element) {
element.innerText = text;
}
};
for (const type of ["chrome", "node", "electron"]) {
replaceText(`${type}-version`, (process.versions as any)[type]);
}
});

View file

@ -0,0 +1,32 @@
/**
* @jest-environment node
*/
import spectron from 'spectron'
import { testWithSpectron } from 'vue-cli-plugin-electron-builder'
jest.setTimeout(50000)
test('Window Loads Properly', async () => {
// Wait for dev server to start
const { app, stopServe } = await testWithSpectron(spectron)
const win = app.browserWindow
const client = app.client
// Window was created
expect(await client.getWindowCount()).toBe(1)
// It is not minimized
expect(await win.isMinimized()).toBe(false)
// Window is visible
expect(await win.isVisible()).toBe(true)
// Size is correct
const { width, height } = await win.getBounds()
expect(width).toBeGreaterThan(0)
expect(height).toBeGreaterThan(0)
// App is loaded properly
expect(
/Welcome to Your Vue\.js (\+ TypeScript )?App/.test(
await client.getHTML('#app')
)
).toBe(true)
await stopServe()
})

View file

@ -1,3 +1,8 @@
module.exports = {
lintOnSave: false
}
lintOnSave: false,
pluginOptions: {
electronBuilder: {
preload: "src/preload.ts"
}
}
};

1720
yarn.lock

File diff suppressed because it is too large Load diff