Skip to content

Commit

Permalink
add plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caorushizi committed Jan 13, 2024
1 parent 136a787 commit ea0abeb
Show file tree
Hide file tree
Showing 25 changed files with 106 additions and 86 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build:plugin": "pnpm -F plugin run build",
"build:main": "pnpm -F main run build",
"build:renderer": "pnpm -F renderer run build",
"build:server": "cd internal && go build -o ../packages/main/bin/win32/server.exe",
"build:server": "cd internal && cross-env GIN_MODE=release go build -o ../packages/main/bin/win32/server.exe",
"pack": "pnpm -F media-downloader run pack",
"release": "pnpm run build && pnpm -F main run release",
"docs:dev": "vitepress dev docs",
Expand All @@ -28,6 +28,7 @@
},
"devDependencies": {
"@waline/client": "^2.15.5",
"cross-env": "^7.0.3",
"vitepress": "1.0.0-beta.3",
"vue": "^3.3.4",
"zx": "^7.2.3"
Expand Down
11 changes: 6 additions & 5 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"main": "main/index.js",
"scripts": {
"postinstall": "electron-rebuild -f -w better-sqlite3",
"dev": "cross-env NODE_ENV=development ts-node script/dev.ts",
"build": "cross-env NODE_ENV=production ts-node script/build.ts",
"pack": "cross-env NODE_ENV=development ts-node script/release.ts",
"dev": "cross-env NODE_ENV=development VITE_CJS_IGNORE_WARNING=true ts-node scripts/dev.ts",
"build": "cross-env NODE_ENV=production ts-node scripts/build.ts",
"pack": "cross-env NODE_ENV=development ts-node scripts/release.ts",
"types": "tsc --emitDeclarationOnly",
"release": "cross-env NODE_ENV=production ts-node script/release.ts"
"release": "cross-env NODE_ENV=production ts-node scripts/release.ts"
},
"author": "",
"license": "MIT",
Expand Down Expand Up @@ -38,7 +38,8 @@
"prebuild-install": "^7.1.1",
"prettier": "2.8.8",
"semver": "^7.5.2",
"typescript": "^5.1.3"
"typescript": "^5.1.3",
"vite": "^5.0.11"
},
"dependencies": {
"@cliqz/adblocker-electron": "^1.26.6",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
Expand All @@ -15,6 +14,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^20.3.1",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions packages/plugin/src/App.tsx → packages/main/plugin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FloatButton, Button, Dropdown, Space } from "antd";
import "./App.scss";
import logo from "./assets/logo.png";
import { WebSource } from "./types";
import { ipcRenderer } from "electron/renderer";

function App() {
const [items, setItems] = useState<WebSource[]>([]);
Expand All @@ -14,13 +15,10 @@ function App() {
};

useEffect(() => {
window?.ipcRenderer.on("webview-link-message", receiveMessage);
ipcRenderer.on("webview-link-message", receiveMessage);

return () => {
window?.ipcRenderer.removeListener(
"webview-link-message",
receiveMessage
);
ipcRenderer.removeListener("webview-link-message", receiveMessage);
};
}, []);

Expand All @@ -38,7 +36,7 @@ function App() {
label: (
<Space
onClick={() => {
window?.ipcRenderer.invoke("add-download-item", {
ipcRenderer.invoke("add-download-item", {
name: item.name,
url: item.url,
type: item.type,
Expand Down
File renamed without changes
File renamed without changes.
10 changes: 0 additions & 10 deletions packages/plugin/src/main.tsx → packages/main/plugin/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "./index.scss";

declare global {
interface Window {
ipcRenderer: import("electron").IpcRenderer;
}
}

export function mount() {
const root = document.createElement("div");
root.id = "MediagoRoot";
Expand All @@ -21,9 +15,5 @@ export function mount() {
}

document.addEventListener("DOMContentLoaded", async () => {
if (import.meta.env.NODE_ENV === "production") {
const { ipcRenderer } = await import("electron/renderer");
window.ipcRenderer = ipcRenderer;
}
mount();
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export default defineConfig({
lib: {
entry: path.resolve(__dirname, "./src/main.tsx"),
name: "plugin",
fileName: () => "plugin.js",
fileName: () => "index.js",
formats: ["cjs"],
},
outDir: path.resolve(__dirname, "../main/app/plugin"),
outDir: path.resolve(__dirname, "../../main/app/plugin"),
emptyOutDir: true,
rollupOptions: {
external: ["electron/renderer"],
Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 24 additions & 14 deletions packages/main/script/dev.ts → packages/main/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as esbuild from "esbuild";
import chokidar from "chokidar";
import { loadDotEnvRuntime, mainResolve, log, copyResource } from "./utils";
import { external } from "./config";
import { build } from "vite";
import path from "path";

let electronProcess: ChildProcessWithoutNullStreams | null = null;

Expand Down Expand Up @@ -54,6 +56,18 @@ function startElectron() {
});
}

function restartElectron() {
if (electronProcess && electronProcess.pid) {
if (process.platform === "darwin") {
spawn("kill", ["-9", String(electronProcess.pid)]);
} else {
process.kill(electronProcess.pid);
}
electronProcess = null;
startElectron();
}
}

async function start() {
const mainContext = await esbuild.context({
...buildConfig,
Expand All @@ -62,28 +76,24 @@ async function start() {

const preloadContext = await esbuild.context({
...buildConfig,
entryPoints: [
mainResolve("src/preload.ts"),
mainResolve("src/devReload.ts"),
],
entryPoints: [mainResolve("src/preload.ts")],
platform: "browser",
});

const watcher = chokidar.watch("./src");

watcher.on("change", async () => {
await mainContext.rebuild();
await preloadContext.rebuild();
log("watch build succeed.");
if (electronProcess && electronProcess.pid) {
if (process.platform === "darwin") {
spawn("kill", ["-9", String(electronProcess.pid)]);
} else {
process.kill(electronProcess.pid);
}
electronProcess = null;
startElectron();
}
restartElectron();
});

const pluginRoot = path.resolve(__dirname, "../plugin");
const watcher2 = chokidar.watch(pluginRoot);
watcher2.on("change", async () => {
console.log("plugin changed");
await build({ root: pluginRoot });
restartElectron();
});

try {
Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 0 additions & 37 deletions packages/main/src/devReload.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/main/src/services/WebviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ export default class WebviewService {
this.view = new BrowserView({
webPreferences: {
partition: PERSIST_WEBVIEW,
preload: isDev
? path.resolve(__dirname, "./devReload.js")
: path.resolve(__dirname, "../../plugin/plugin.js"),
preload: path.resolve(__dirname, "../../plugin/index.js"),
},
});
this.view.setBackgroundColor("#fff");
Expand Down
8 changes: 4 additions & 4 deletions packages/mobile/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>media download</title>
</head>
<body>
<div id="app"></div>
Expand Down
65 changes: 62 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit ea0abeb

Please sign in to comment.