fix: 重构模板

This commit is contained in:
NICE CODE BY DEV 2024-04-09 11:23:19 +08:00
parent f146d3dca7
commit 4cf5a6d2f6
32 changed files with 5972 additions and 3494 deletions

105
.gitignore vendored
View File

@ -1,92 +1,13 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
.DS_Store
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# Webpack
.webpack/
# Vite
.vite/
# Electron-Forge
out/
/node_modules
/.env.local
/.umirc.local.ts
/config/config.local.ts
/src/.umi
/src/.umi-production
/src/.umi-test
/dist
.swc
.electron
.electron-production
yarn-error.log
pnpm-lock.yaml

6
.npmrc
View File

@ -1,4 +1,4 @@
registry="https://registry.npmmirror.com"
registry=https://registry.npmmirror.com
strict-peer-dependencies=false
ignore-workspace-root-check=true
electron_use_remote_checksums=1
electron-mirror=https://registry.npmmirror.com/-/binary/electron/
electron-builder-binaries-mirror=https://registry.npmmirror.com/binary.html?path=electron-builder-binaries/

21
.umirc.ts Normal file
View File

@ -0,0 +1,21 @@
import { defineConfig } from 'umi';
import { Platform, createTargets } from '@umijs/plugin-electron';
// example: mac & windows
const targets = createTargets([Platform.WINDOWS]);
// example: mac m1
// const targets = Platform.MAC.createTarget(['dmg'], Arch.arm64);
export default defineConfig({
npmClient: 'yarn',
plugins: ['@umijs/plugin-electron'],
electron: {
builder: {
targets
},
extraDevFiles: {
// 'xxxx.js' : fs.readFileSync('xxxx.js','utf-8'),
}
},
});

1
README.md Normal file
View File

@ -0,0 +1 @@
notice: run `pnpm install clear-module lodash chokidar` while using pnpm.

View File

@ -1,10 +0,0 @@
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var require_app_css = __commonJS({
"app.css.js"(exports) {
exports.styles = [];
}
});
export default require_app_css();

View File

@ -1,2 +0,0 @@
Promise.resolve("./app.css.js").then(() => {
});

View File

@ -1,54 +0,0 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import type { ForgeConfig } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
import { MakerDeb } from '@electron-forge/maker-deb';
import { MakerRpm } from '@electron-forge/maker-rpm';
import { AutoUnpackNativesPlugin } from '@electron-forge/plugin-auto-unpack-natives';
import { WebpackPlugin } from '@electron-forge/plugin-webpack';
import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { FuseV1Options, FuseVersion } from '@electron/fuses';
import { mainConfig } from './webpack.main.config';
import { rendererConfig } from './webpack.renderer.config';
const config: ForgeConfig = {
packagerConfig: {
asar: true,
},
rebuildConfig: {},
makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
plugins: [
new AutoUnpackNativesPlugin({}),
new WebpackPlugin({
mainConfig,
renderer: {
config: rendererConfig,
entryPoints: [
{
html: './src/index.html',
js: './src/renderer.tsx',
name: 'main_window',
preload: {
js: './src/preload.ts',
},
},
],
},
}),
// Fuses are used to enable/disable various Electron functionality
// at package time, before code signing the application
new FusesPlugin({
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false,
[FuseV1Options.EnableCookieEncryption]: true,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
[FuseV1Options.OnlyLoadAppFromAsar]: true,
}),
],
};
export default config;

View File

@ -1,55 +1,44 @@
{
"name": "electron-template",
"productName": "template-App",
"version": "1.0.0",
"description": "My Electron application description",
"main": ".webpack/main",
"version": "1.0.2",
"author": {
"name": "dev"
},
"description": "eggKnife-electron的客户端模板",
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "eslint --ext .ts,.tsx ."
"dev": "umi dev",
"build": "umi build",
"postinstall": "umi setup",
"setup": "umi setup",
"start": "npm run dev"
},
"dependencies": {
"antd": "^5.16.1",
"chokidar": "^3.6.0",
"clear-module": "^4.1.2",
"lodash": "^4.17.21",
"umi": "^4.0.42"
},
"devDependencies": {
"@electron-forge/cli": "^7.3.1",
"@electron-forge/maker-deb": "^7.3.1",
"@electron-forge/maker-rpm": "^7.3.1",
"@electron-forge/maker-squirrel": "^7.3.1",
"@electron-forge/maker-zip": "^7.3.1",
"@electron-forge/plugin-auto-unpack-natives": "^7.3.1",
"@electron-forge/plugin-fuses": "^7.2.0",
"@electron-forge/plugin-webpack": "^7.3.1",
"@electron-forge/shared-types": "^7.3.1",
"@electron/fuses": "^1.7.0",
"@types/node": "^20.12.2",
"@types/react": "^18.2.73",
"@types/react-dom": "^18.2.23",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@vercel/webpack-asset-relocator-loader": "1.7.3",
"css-loader": "^6.0.0",
"@tsconfig/node21": "^21.0.3",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@umijs/plugin-electron": "^0.2.0",
"electron": "29.1.6",
"eslint": "^8.0.1",
"eslint-plugin-import": "^2.25.0",
"fork-ts-checker-webpack-plugin": "^7.2.13",
"node-loader": "^2.0.0",
"style-loader": "^3.0.0",
"ts-loader": "^9.2.2",
"ts-node": "^10.0.0",
"typescript": "~4.5.4",
"webpack": "^5.91.0"
"typescript": "^4.1.2"
},
"keywords": [],
"author": {
"name": "dev",
"email": "jzx710328466@gmail.com"
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"license": "MIT",
"dependencies": {
"antd": "^5.16.0",
"electron-squirrel-startup": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
"files": [
"src/**/*",
"tsconfig.json",
".npmrc",
".gitignore",
"README.md",
".umirc.ts",
"package.json",
"typings.d.ts"
]
}

File diff suppressed because it is too large Load Diff

BIN
src/assets/avatar.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

12
src/config.ts Normal file
View File

@ -0,0 +1,12 @@
import { BrowserWindowConstructorOptions } from 'electron';
export default {
browserWindow: {
titleBarStyle: process.platform === 'win32' ? 'hidden' : 'hiddenInset',
maximizable: false,
webPreferences: {
contextIsolation: false,
enableRemoteModule: true
}
} as BrowserWindowConstructorOptions
};

View File

@ -2,8 +2,4 @@ body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
Arial, sans-serif;
margin: auto;
}
webview {
height: 100vh;
}

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>形体结构化视频数据分析系统</title>
</head>
<body>
<div id="app"></div>
<webview id="" src="http://10.0.0.96:30003/" />
</body>
</html>

View File

@ -1,64 +0,0 @@
import { app, BrowserWindow, Menu, ipcMain } from 'electron';
import { uploadFile } from './utils/upload';
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit();
}
const createWindow = (): void => {
// Menu.setApplicationMenu(null)
const mainWindow = new BrowserWindow({
height: 600,
width: 800,
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
webviewTag: true
},
});
ipcMain.on('set-title', (event, title) => {
const webContents = event.sender
const win = BrowserWindow.fromWebContents(webContents)
win.setTitle(title)
})
ipcMain.on('upload', (event, filePaths: string[]) => {
// 在后台线程中处理文件上传
uploadFile(filePaths, event.sender);
});
// and load the index.html of the app.
mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
// 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', createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X 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 (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

10
src/layouts/index.less Normal file
View File

@ -0,0 +1,10 @@
.navs {
ul {
padding: 0;
list-style: none;
display: flex;
}
li {
margin-right: 1em;
}
}

18
src/layouts/index.tsx Normal file
View File

@ -0,0 +1,18 @@
import { Link, Outlet } from 'umi';
import styles from './index.less';
export default function Layout() {
return (
<div className={styles.navs}>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/docs">upLoad</Link>
</li>
</ul>
<Outlet />
</div>
);
}

1
src/main/index.ts Normal file
View File

@ -0,0 +1 @@
getBrowserWindowRuntime().webContents.openDevTools();

9
src/main/ipc/platform.ts Normal file
View File

@ -0,0 +1,9 @@
import { ipcMain } from 'electron';
ipcMain.handle('getPlatform', () => {
return `hi, i'm from ${process.platform}`;
});
// node: () => process.versions.node,
// chrome: () => process.versions.chrome,
// electron: () => process.versions.electron,

View File

@ -1,19 +1,19 @@
import { app } from 'electron';
import { app, ipcMain } from 'electron';
import fs from 'fs';
import path from 'node:path';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const uploadFile = (filePaths: string[], _sender: Electron.WebContents) => {
ipcMain.on('uploadFile', (event, filePaths: string[]) => {
// 获取用户当前文件夹路径
const saveDirectoryPath = app.getPath('downloads');
for (let i = 0; i < filePaths.length; i++) {
const fileName = path.basename(filePaths[i]);
const targetFilePath = path.join(saveDirectoryPath, fileName);
const fileStream = fs.createWriteStream(targetFilePath);
fileStream.write(fs.readFileSync(filePaths[i]));
fileStream.end();
console.log('Uploaded file saved at:', targetFilePath);
}
// console.log('app', app.getPath('userData'))
// const saveDirectoryPath = await app.getPath('downloads');
for (let i = 0; i < filePaths.length; i++) {
const fileName = path.basename(filePaths[i]);
const targetFilePath = path.join(__dirname, fileName);
const fileStream = fs.createWriteStream(targetFilePath);
fileStream.write(fs.readFileSync(filePaths[i]));
fileStream.end();
console.log('Uploaded file saved at:', targetFilePath);
}
// const fileSize = fs.statSync(filePath).size;
// let uploadedSize = 0;
@ -32,7 +32,8 @@ export const uploadFile = (filePaths: string[], _sender: Electron.WebContents) =
// readStream.on('error', (err) => {
// sender.send('upload-error', err.message);
// });
}
});
export const singleUpload = (file: File) => {
const path = file.path; //文件本地路径

8
src/main/preload.ts Normal file
View File

@ -0,0 +1,8 @@
import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('$api', {
getPlatform: async () => {
return await ipcRenderer.invoke('getPlatform');
},
uploadFile: (filePaths: string[]) => ipcRenderer.send('uploadFile', filePaths),
});

3
src/main/tsconfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node21"
}

5
src/main/typing.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import { BrowserWindow } from 'electron';
declare global {
export function getBrowserWindowRuntime(): BrowserWindow;
}

View File

@ -1,4 +1,4 @@
import { Button, message, Upload } from 'antd'
import { Button, message, Space, Upload } from 'antd'
import { useEffect, useState } from 'react';
import type { GetProp, UploadFile, UploadProps } from 'antd';
@ -22,7 +22,7 @@ export default () => {
setUploading(true);
const filePaths = fileList.map((_file: any) => _file.path)
console.log('filePaths', filePaths)
window.baseAPI.uploadFile(filePaths)
window.$api.uploadFile(filePaths)
setUploading(false)
};
@ -52,14 +52,14 @@ export default () => {
};
return (
<div>
<Space direction='vertical' style={{ width: '600px' }}>
<Dragger {...uploadProps}>
<p className="ant-upload-drag-icon">
{progress}
</p>
<p className="ant-upload-text"></p>
</Dragger>
<Button block onClick={() => handleUpload()} ></Button>
</div>
<Button type='primary' block onClick={() => handleUpload()} ></Button>
</Space>
)
}

22
src/pages/index.tsx Normal file
View File

@ -0,0 +1,22 @@
import avatar from '../assets/avatar.jpg';
export default function HomePage() {
return (
<div>
<h2>Welcome to nicecode electron!</h2>
<p>
<img src={avatar} width="388" />
</p>
<p>
To get started, edit <code>pages/index.tsx</code> and save to reload.
</p>
<button
onClick={async () => {
window.alert(await window.$api.getPlatform());
}}
>
what is my platform?
</button>
</div>
);
}

View File

@ -1,19 +0,0 @@
// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import { contextBridge, ipcRenderer } from 'electron'
contextBridge.exposeInMainWorld('versions', {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
})
contextBridge.exposeInMainWorld('baseAPI', {
setTitle: (title: string) => ipcRenderer.send('set-title', title),
uploadFile: (filePaths: string[]) => ipcRenderer.send('upload', filePaths),
onUploadProgress: (cb: (progress: string) => void) => ipcRenderer.on('upload-progress',
(_event, progress) => {
cb?.(progress)
}
)
})

View File

@ -1,6 +0,0 @@
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './pages/home'
const root = createRoot(document.getElementById('app'));
root.render(<App />);

View File

@ -1,20 +1,3 @@
{
"compilerOptions": {
"target": "ES6",
"allowJs": true,
"module": "commonjs",
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true,
"sourceMap": true,
"baseUrl": ".",
"outDir": "dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"jsx": "react-jsx",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*", "*.d.ts", "**/*.ts"]
"extends": "./src/.umi/tsconfig.json"
}

7
typings.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
import 'umi/typings';
declare global {
interface Window {
$api: any;
}
}

View File

@ -1,20 +0,0 @@
import type { Configuration } from 'webpack';
import { rules } from './webpack.rules';
import { plugins } from './webpack.plugins';
export const mainConfig: Configuration = {
/**
* This is the main entry point for your application, it's the first file
* that runs in the main process.
*/
entry: './src/index.ts',
// Put your normal webpack config below here
module: {
rules,
},
plugins,
resolve: {
extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
},
};

View File

@ -1,10 +0,0 @@
import type IForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ForkTsCheckerWebpackPlugin: typeof IForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
export const plugins = [
new ForkTsCheckerWebpackPlugin({
logger: 'webpack-infrastructure',
}),
];

View File

@ -1,19 +0,0 @@
import type { Configuration } from 'webpack';
import { rules } from './webpack.rules';
import { plugins } from './webpack.plugins';
rules.push({
test: /\.css$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
});
export const rendererConfig: Configuration = {
module: {
rules,
},
plugins,
resolve: {
extensions: ['.js', '.ts', '.jsx', '.tsx', '.css'],
},
};

View File

@ -1,31 +0,0 @@
import type { ModuleOptions } from 'webpack';
export const rules: Required<ModuleOptions>['rules'] = [
// Add support for native node modules
{
// We're specifying native_modules in the test because the asset relocator loader generates a
// "fake" .node file which is really a cjs file.
test: /native_modules[/\\].+\.node$/,
use: 'node-loader',
},
{
test: /[/\\]node_modules[/\\].+\.(m?js|node)$/,
parser: { amd: false },
use: {
loader: '@vercel/webpack-asset-relocator-loader',
options: {
outputAssetBase: 'native_modules',
},
},
},
{
test: /\.tsx?$/,
exclude: /(node_modules|\.webpack)/,
use: {
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
},
];