src -> packages

This commit is contained in:
草鞋没号 2022-02-08 10:34:14 +08:00
parent c0b257f794
commit 9d702430f0
22 changed files with 25 additions and 19 deletions

View File

@ -38,11 +38,11 @@ You can learn more details by looking at the source code. Trust me, this templat
## Directory ## Directory
Once `dev` or `build` npm-script executed will be generate named `dist` folder. It has children dir of same as `src` folder, the purpose of this design can ensure the correct path calculation. Once `dev` or `build` npm-script executed will be generate named `dist` folder. It has children dir of same as `packages` folder, the purpose of this design can ensure the correct path calculation.
```tree ```tree
├── dist After build, it's generated according to the "src" directory ├── dist After build, it's generated according to the "packages" directory
├ ├── main ├ ├── main
├ ├── preload ├ ├── preload
├ ├── renderer ├ ├── renderer
@ -52,7 +52,7 @@ Once `dev` or `build` npm-script executed will be generate named `dist` folder.
├ ├── vite.config.mjs Marin-process, Preload-script vite-config ├ ├── vite.config.mjs Marin-process, Preload-script vite-config
├ ├── watch.mjs Develop script, for -> npm run dev ├ ├── watch.mjs Develop script, for -> npm run dev
├── src ├── packages
├ ├── main Main-process source code ├ ├── main Main-process source code
├ ├── preload Preload-script source code ├ ├── preload Preload-script source code
├ ├── renderer Renderer-process source code ├ ├── renderer Renderer-process source code
@ -66,7 +66,7 @@ Once `dev` or `build` npm-script executed will be generate named `dist` folder.
#### All Electron, NodeJs API invoke passed `Preload-script` #### All Electron, NodeJs API invoke passed `Preload-script`
* **src/preload/index.ts** * **packages/preload/index.ts**
```typescript ```typescript
import fs from 'fs' import fs from 'fs'
@ -77,7 +77,7 @@ Once `dev` or `build` npm-script executed will be generate named `dist` folder.
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer) contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
``` ```
* **src/renderer/src/global.d.ts** * **packages/renderer/src/global.d.ts**
```typescript ```typescript
// Defined on the window // Defined on the window
@ -87,7 +87,7 @@ Once `dev` or `build` npm-script executed will be generate named `dist` folder.
} }
``` ```
* **src/renderer/src/main.ts** * **packages/renderer/src/main.ts**
```typescript ```typescript
// Use Electron, NodeJs API in Renderer-process // Use Electron, NodeJs API in Renderer-process

View File

@ -38,11 +38,11 @@
## 目录结构 ## 目录结构
  一旦启动或打包脚本执行过,会在根目录产生 **`dist` 文件夹,里面的文件夹同 `src` 一模一样**;在使用一些路径计算时,尤其是相对路径计算;`dist``src` 里面保持相同的目录结构能避开好多问题   一旦启动或打包脚本执行过,会在根目录产生 **`dist` 文件夹,里面的文件夹同 `packages` 一模一样**;在使用一些路径计算时,尤其是相对路径计算;`dist``packages` 里面保持相同的目录结构能避开好多问题
```tree ```tree
├── dist 构建后,根据 src 目录生成 ├── dist 构建后,根据 packages 目录生成
├ ├── main ├ ├── main
├ ├── preload ├ ├── preload
├ ├── renderer ├ ├── renderer
@ -52,7 +52,7 @@
├ ├── vite.config.mjs 主进程, 预加载脚本源码 vite 配置 ├ ├── vite.config.mjs 主进程, 预加载脚本源码 vite 配置
├ ├── watch.mjs 项目开发脚本,对应 npm run dev ├ ├── watch.mjs 项目开发脚本,对应 npm run dev
├── src ├── packages
├ ├── main 主进程源码 ├ ├── main 主进程源码
├ ├── preload 预加载脚本源码 ├ ├── preload 预加载脚本源码
├ ├── renderer 渲染进程源码 ├ ├── renderer 渲染进程源码
@ -78,7 +78,7 @@ electron-builder 打包时候会将 dependencies 中的包打包到 app.asar 里
**推荐所有的 NodeJs、Electron API 通过 `Preload-script` 注入到 渲染进程中,例如:** **推荐所有的 NodeJs、Electron API 通过 `Preload-script` 注入到 渲染进程中,例如:**
* **src/preload/index.ts** * **packages/preload/index.ts**
```typescript ```typescript
import fs from 'fs' import fs from 'fs'
@ -89,7 +89,7 @@ electron-builder 打包时候会将 dependencies 中的包打包到 app.asar 里
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer) contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)
``` ```
* **src/renderer/src/global.d.ts** * **packages/renderer/src/global.d.ts**
```typescript ```typescript
// Defined on the window // Defined on the window
@ -99,7 +99,13 @@ electron-builder 打包时候会将 dependencies 中的包打包到 app.asar 里
} }
``` ```
* **src/renderer/main.ts** * **packages/renderer/src/main.ts**
```typescript
// Use Electron, NodeJs API in Renderer-process
console.log('fs', window.fs)
console.log('ipcRenderer', window.ipcRenderer)
```
```typescript ```typescript
// Use Electron, NodeJs API in Renderer-process // Use Electron, NodeJs API in Renderer-process

View File

@ -6,7 +6,7 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "node scripts/watch.mjs", "dev": "node scripts/watch.mjs",
"build": "vue-tsc --project src/renderer/tsconfig.json --noEmit && node scripts/build.mjs && electron-builder" "build": "vue-tsc --project packages/renderer/tsconfig.json --noEmit && node scripts/build.mjs && electron-builder"
}, },
"engines": { "engines": {
"node": ">=14.17.0" "node": ">=14.17.0"

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

@ -8,7 +8,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
await build({ await build({
configFile: 'scripts/vite.config.mjs', configFile: 'scripts/vite.config.mjs',
root: join(__dirname, '../src/main'), root: join(__dirname, '../packages/main'),
build: { build: {
outDir: '../../dist/main', outDir: '../../dist/main',
}, },
@ -16,10 +16,10 @@ await build({
await build({ await build({
configFile: 'scripts/vite.config.mjs', configFile: 'scripts/vite.config.mjs',
root: join(__dirname, '../src/preload'), root: join(__dirname, '../packages/preload'),
build: { build: {
outDir: '../../dist/preload', outDir: '../../dist/preload',
}, },
}) })
await build({ configFile: 'src/renderer/vite.config.ts' }) await build({ configFile: 'packages/renderer/vite.config.ts' })

View File

@ -22,7 +22,7 @@ function watchMain() {
return build({ return build({
configFile: 'scripts/vite.config.mjs', configFile: 'scripts/vite.config.mjs',
root: join(__dirname, '../src/main'), root: join(__dirname, '../packages/main'),
build: { build: {
outDir: '../../dist/main', outDir: '../../dist/main',
watch: true, watch: true,
@ -46,7 +46,7 @@ function watchMain() {
function watchPreload(server) { function watchPreload(server) {
return build({ return build({
configFile: 'scripts/vite.config.mjs', configFile: 'scripts/vite.config.mjs',
root: join(__dirname, '../src/preload'), root: join(__dirname, '../packages/preload'),
build: { build: {
outDir: '../../dist/preload', outDir: '../../dist/preload',
watch: true, watch: true,
@ -61,7 +61,7 @@ function watchPreload(server) {
} }
// bootstrap // bootstrap
const server = await createServer({ configFile: 'src/renderer/vite.config.ts' }) const server = await createServer({ configFile: 'packages/renderer/vite.config.ts' })
await server.listen() await server.listen()
await watchPreload(server) await watchPreload(server)