From ea0abebedb995b5ccad570be9addc4a5d4eb4930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A3=AB=E5=AD=90=E2=98=80=EF=B8=8F?= <84996057@qq.com> Date: Sun, 14 Jan 2024 01:14:02 +0800 Subject: [PATCH] add plugin --- package.json | 3 +- packages/main/package.json | 11 +-- packages/{ => main}/plugin/.eslintrc.cjs | 0 packages/{ => main}/plugin/.gitignore | 0 packages/{ => main}/plugin/README.md | 0 packages/{ => main}/plugin/package.json | 2 +- packages/{ => main}/plugin/src/App.scss | 0 packages/{ => main}/plugin/src/App.tsx | 10 ++- .../{ => main}/plugin/src/assets/logo.png | Bin packages/{ => main}/plugin/src/index.scss | 0 packages/{ => main}/plugin/src/main.tsx | 10 --- packages/{ => main}/plugin/src/types.ts | 0 packages/{ => main}/plugin/src/vite-env.d.ts | 0 packages/{ => main}/plugin/tsconfig.json | 0 packages/{ => main}/plugin/tsconfig.node.json | 0 packages/{ => main}/plugin/vite.config.ts | 4 +- packages/main/{script => scripts}/build.ts | 0 packages/main/{script => scripts}/config.ts | 0 packages/main/{script => scripts}/dev.ts | 38 ++++++---- packages/main/{script => scripts}/release.ts | 0 packages/main/{script => scripts}/utils.ts | 0 packages/main/src/devReload.ts | 37 ---------- packages/main/src/services/WebviewService.ts | 4 +- packages/mobile/index.html | 8 +-- pnpm-lock.yaml | 65 +++++++++++++++++- 25 files changed, 106 insertions(+), 86 deletions(-) rename packages/{ => main}/plugin/.eslintrc.cjs (100%) rename packages/{ => main}/plugin/.gitignore (100%) rename packages/{ => main}/plugin/README.md (100%) rename packages/{ => main}/plugin/package.json (96%) rename packages/{ => main}/plugin/src/App.scss (100%) rename packages/{ => main}/plugin/src/App.tsx (86%) rename packages/{ => main}/plugin/src/assets/logo.png (100%) rename packages/{ => main}/plugin/src/index.scss (100%) rename packages/{ => main}/plugin/src/main.tsx (63%) rename packages/{ => main}/plugin/src/types.ts (100%) rename packages/{ => main}/plugin/src/vite-env.d.ts (100%) rename packages/{ => main}/plugin/tsconfig.json (100%) rename packages/{ => main}/plugin/tsconfig.node.json (100%) rename packages/{ => main}/plugin/vite.config.ts (83%) rename packages/main/{script => scripts}/build.ts (100%) rename packages/main/{script => scripts}/config.ts (100%) rename packages/main/{script => scripts}/dev.ts (75%) rename packages/main/{script => scripts}/release.ts (100%) rename packages/main/{script => scripts}/utils.ts (100%) delete mode 100644 packages/main/src/devReload.ts diff --git a/package.json b/package.json index 1a39d6d..3f9ab5d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/packages/main/package.json b/packages/main/package.json index ff171a7..dddc822 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -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", @@ -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", diff --git a/packages/plugin/.eslintrc.cjs b/packages/main/plugin/.eslintrc.cjs similarity index 100% rename from packages/plugin/.eslintrc.cjs rename to packages/main/plugin/.eslintrc.cjs diff --git a/packages/plugin/.gitignore b/packages/main/plugin/.gitignore similarity index 100% rename from packages/plugin/.gitignore rename to packages/main/plugin/.gitignore diff --git a/packages/plugin/README.md b/packages/main/plugin/README.md similarity index 100% rename from packages/plugin/README.md rename to packages/main/plugin/README.md diff --git a/packages/plugin/package.json b/packages/main/plugin/package.json similarity index 96% rename from packages/plugin/package.json rename to packages/main/plugin/package.json index 6ecf965..a898074 100644 --- a/packages/plugin/package.json +++ b/packages/main/plugin/package.json @@ -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" @@ -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", diff --git a/packages/plugin/src/App.scss b/packages/main/plugin/src/App.scss similarity index 100% rename from packages/plugin/src/App.scss rename to packages/main/plugin/src/App.scss diff --git a/packages/plugin/src/App.tsx b/packages/main/plugin/src/App.tsx similarity index 86% rename from packages/plugin/src/App.tsx rename to packages/main/plugin/src/App.tsx index eeb8e4c..1f2d0a3 100644 --- a/packages/plugin/src/App.tsx +++ b/packages/main/plugin/src/App.tsx @@ -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([]); @@ -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); }; }, []); @@ -38,7 +36,7 @@ function App() { label: ( { - window?.ipcRenderer.invoke("add-download-item", { + ipcRenderer.invoke("add-download-item", { name: item.name, url: item.url, type: item.type, diff --git a/packages/plugin/src/assets/logo.png b/packages/main/plugin/src/assets/logo.png similarity index 100% rename from packages/plugin/src/assets/logo.png rename to packages/main/plugin/src/assets/logo.png diff --git a/packages/plugin/src/index.scss b/packages/main/plugin/src/index.scss similarity index 100% rename from packages/plugin/src/index.scss rename to packages/main/plugin/src/index.scss diff --git a/packages/plugin/src/main.tsx b/packages/main/plugin/src/main.tsx similarity index 63% rename from packages/plugin/src/main.tsx rename to packages/main/plugin/src/main.tsx index 7a42b1c..82a1d6f 100644 --- a/packages/plugin/src/main.tsx +++ b/packages/main/plugin/src/main.tsx @@ -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"; @@ -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(); }); diff --git a/packages/plugin/src/types.ts b/packages/main/plugin/src/types.ts similarity index 100% rename from packages/plugin/src/types.ts rename to packages/main/plugin/src/types.ts diff --git a/packages/plugin/src/vite-env.d.ts b/packages/main/plugin/src/vite-env.d.ts similarity index 100% rename from packages/plugin/src/vite-env.d.ts rename to packages/main/plugin/src/vite-env.d.ts diff --git a/packages/plugin/tsconfig.json b/packages/main/plugin/tsconfig.json similarity index 100% rename from packages/plugin/tsconfig.json rename to packages/main/plugin/tsconfig.json diff --git a/packages/plugin/tsconfig.node.json b/packages/main/plugin/tsconfig.node.json similarity index 100% rename from packages/plugin/tsconfig.node.json rename to packages/main/plugin/tsconfig.node.json diff --git a/packages/plugin/vite.config.ts b/packages/main/plugin/vite.config.ts similarity index 83% rename from packages/plugin/vite.config.ts rename to packages/main/plugin/vite.config.ts index d3a1972..5da7a75 100644 --- a/packages/plugin/vite.config.ts +++ b/packages/main/plugin/vite.config.ts @@ -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"], diff --git a/packages/main/script/build.ts b/packages/main/scripts/build.ts similarity index 100% rename from packages/main/script/build.ts rename to packages/main/scripts/build.ts diff --git a/packages/main/script/config.ts b/packages/main/scripts/config.ts similarity index 100% rename from packages/main/script/config.ts rename to packages/main/scripts/config.ts diff --git a/packages/main/script/dev.ts b/packages/main/scripts/dev.ts similarity index 75% rename from packages/main/script/dev.ts rename to packages/main/scripts/dev.ts index 7040c61..c8fc101 100644 --- a/packages/main/script/dev.ts +++ b/packages/main/scripts/dev.ts @@ -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; @@ -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, @@ -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 { diff --git a/packages/main/script/release.ts b/packages/main/scripts/release.ts similarity index 100% rename from packages/main/script/release.ts rename to packages/main/scripts/release.ts diff --git a/packages/main/script/utils.ts b/packages/main/scripts/utils.ts similarity index 100% rename from packages/main/script/utils.ts rename to packages/main/scripts/utils.ts diff --git a/packages/main/src/devReload.ts b/packages/main/src/devReload.ts deleted file mode 100644 index 83d81fe..0000000 --- a/packages/main/src/devReload.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* eslint-env browser */ -interface ScriptProps { - src?: string; - textContent?: string; -} -function insertModuleScript({ src, textContent }: ScriptProps) { - const script = document.createElement("script"); - script.type = "module"; - if (src) { - script.src = src; - } - if (textContent) { - script.textContent = textContent; - } - document.body.appendChild(script); -} - -document.addEventListener("DOMContentLoaded", () => { - // refresh - insertModuleScript({ - textContent: `import RefreshRuntime from "http://localhost:5173/@react-refresh" -RefreshRuntime.injectIntoGlobalHook(window) -window.$RefreshReg$ = () => {} -window.$RefreshSig$ = () => (type) => type -window.__vite_plugin_react_preamble_installed__ = true -`, - }); - // vite client - insertModuleScript({ - src: "http://localhost:5173/@vite/client", - }); - // components - insertModuleScript({ - textContent: `import { mount } from "http://localhost:5173/src/main.tsx" -mount()`, - }); -}); diff --git a/packages/main/src/services/WebviewService.ts b/packages/main/src/services/WebviewService.ts index 645f8ed..4712c04 100644 --- a/packages/main/src/services/WebviewService.ts +++ b/packages/main/src/services/WebviewService.ts @@ -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"); diff --git a/packages/mobile/index.html b/packages/mobile/index.html index a888544..f411d0f 100644 --- a/packages/mobile/index.html +++ b/packages/mobile/index.html @@ -1,10 +1,10 @@ - - - - Vite App + + + + media download
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d6b1da..6149144 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,9 @@ importers: '@waline/client': specifier: ^2.15.5 version: 2.15.8 + cross-env: + specifier: ^7.0.3 + version: 7.0.3 vitepress: specifier: 1.0.0-beta.3 version: 1.0.0-beta.3(@algolia/client-search@4.22.1)(search-insights@2.13.0) @@ -164,6 +167,9 @@ importers: typescript: specifier: ^5.1.3 version: 5.1.6 + vite: + specifier: ^5.0.11 + version: 5.0.11(@types/node@20.11.0) packages/main/app: dependencies: @@ -178,6 +184,55 @@ importers: specifier: ^25.2.0 version: 25.9.8 + packages/main/plugin: + dependencies: + antd: + specifier: ^5.12.8 + version: 5.12.8(react-dom@18.2.0)(react@18.2.0) + react: + specifier: ^18.2.0 + version: 18.2.0 + react-dom: + specifier: ^18.2.0 + version: 18.2.0(react@18.2.0) + devDependencies: + '@types/node': + specifier: ^20.3.1 + version: 20.11.0 + '@types/react': + specifier: ^18.2.43 + version: 18.2.47 + '@types/react-dom': + specifier: ^18.2.17 + version: 18.2.18 + '@typescript-eslint/eslint-plugin': + specifier: ^6.14.0 + version: 6.18.1(@typescript-eslint/parser@6.18.1)(eslint@8.56.0)(typescript@5.1.6) + '@typescript-eslint/parser': + specifier: ^6.14.0 + version: 6.18.1(eslint@8.56.0)(typescript@5.1.6) + '@vitejs/plugin-react': + specifier: ^4.2.1 + version: 4.2.1(vite@5.0.11) + electron: + specifier: 25.9.8 + version: 25.9.8 + eslint: + specifier: ^8.55.0 + version: 8.56.0 + eslint-plugin-react-hooks: + specifier: ^4.6.0 + version: 4.6.0(eslint@8.56.0) + eslint-plugin-react-refresh: + specifier: ^0.4.5 + version: 0.4.5(eslint@8.56.0) + typescript: + specifier: ^5.1.3 + version: 5.1.6 + vite: + specifier: ^5.0.8 + version: 5.0.11(@types/node@20.11.0) + packages/mobile: dependencies: axios: @@ -275,6 +330,9 @@ importers: specifier: ^18.2.0 version: 18.2.0(react@18.2.0) devDependencies: + '@types/node': + specifier: ^20.3.1 + version: 20.11.0 '@types/react': specifier: ^18.2.43 version: 18.2.47 @@ -307,7 +365,7 @@ importers: version: 5.1.6 vite: specifier: ^5.0.8 - version: 5.0.11 + version: 5.0.11(@types/node@20.11.0) packages/renderer: dependencies: @@ -3054,7 +3112,7 @@ packages: '@babel/plugin-transform-react-jsx-source': 7.23.3(@babel/core@7.23.7) '@types/babel__core': 7.20.5 react-refresh: 0.14.0 - vite: 5.0.11 + vite: 5.0.11(@types/node@20.11.0) transitivePeerDependencies: - supports-color dev: true @@ -9602,7 +9660,7 @@ packages: fsevents: 2.3.3 dev: true - /vite@5.0.11: + /vite@5.0.11(@types/node@20.11.0): resolution: {integrity: sha512-XBMnDjZcNAw/G1gEiskiM1v6yzM4GE5aMGvhWTlHAYYhxb7S3/V1s3m2LDHa8Vh6yIWYYB0iJwsEaS523c4oYA==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -9630,6 +9688,7 @@ packages: terser: optional: true dependencies: + '@types/node': 20.11.0 esbuild: 0.19.11 postcss: 8.4.33 rollup: 4.9.5