Skip to content

Commit

Permalink
feat: 搭建 electron / server / web 复用的开发模式
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteMinds committed Sep 6, 2022
1 parent 801e5dd commit 2314d41
Show file tree
Hide file tree
Showing 21 changed files with 2,068 additions and 73 deletions.
29 changes: 25 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
node_modules/
dist/
lib/
lerna-debug.log
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
lib
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"workspaces": [
"packages/*"
],
"dependencies": {
"concurrently": "^7.3.0"
},
"devDependencies": {
"lerna": "^5.4.0"
}
Expand Down
54 changes: 48 additions & 6 deletions packages/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,58 @@
"name": "@autorecord/electron",
"version": "0.0.1",
"description": "Integrate two packets http-server and web, packaged as the client",
"main": "./lib/index.js",
"main": "./dist/index.js",
"private": true,
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"watch": "tsc -w",
"lint": "eslint -c .eslintrc --ext .ts ./src",
"app:dev": "tsc && concurrently \"vite ../web\" \" electron .\" \"tsc -w\"",
"app:build": "npm run vite:build && tsc && electron-builder",
"app:preview": "npm run vite:build && tsc && electron ."
},
"build": {
"appId": "YourAppID",
"asar": true,
"directories": {
"buildResources": "assets",
"output": "release/${version}"
},
"files": [
"dist"
],
"mac": {
"artifactName": "${productName}_${version}.${ext}",
"target": [
"dmg"
]
},
"win": {
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
],
"artifactName": "${productName}_${version}.${ext}"
},
"nsis": {
"oneClick": false,
"perMachine": false,
"allowToChangeInstallationDirectory": true,
"deleteAppDataOnUninstall": false
}
},
"files": [
"lib"
],
"repository": "https://github.com/WhiteMinds/LiveAutoRecord",
"author": "WhiteMind",
"license": "LGPL"
"license": "LGPL",
"dependencies": {
"@autorecord/http-server": "^0.0.1"
},
"devDependencies": {
"electron": "^20.1.1",
"electron-builder": "^23.3.3"
}
}
53 changes: 53 additions & 0 deletions packages/electron/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
import { startServer } from '@autorecord/http-server'

const isDev = process.env.npm_lifecycle_event === 'app:dev' ? true : false

startServer()

function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// https://github.com/Yukun-Guo/vite-vue3-electron-ts-template#3-setup-electron#src/electron/preload/preload.ts
// preload: join(__dirname, '../preload/preload.js'),
},
})

// and load the index.html of the app.
if (isDev) {
mainWindow.loadURL('http://localhost:5173')
mainWindow.webContents.openDevTools() // Open the DevTools.
} else {
mainWindow.loadFile(join(__dirname, '../../index.html'))
}
// mainWindow.loadURL( //this doesn't work on macOS in build and preview mode
// isDev ?
// 'http://localhost:5173' :
// join(__dirname, '../../index.html')
// );
}

// 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.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// 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 (BrowserWindow.getAllWindows().length === 0) 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()
}
})
27 changes: 14 additions & 13 deletions packages/electron/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"compilerOptions": {
"target": "es6",
"target": "esnext",
"useDefineForClassFields": true,
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
"moduleResolution": "node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": false,
"esModuleInterop": true,
"removeComments": false,
"noUnusedParameters": false,
"noUnusedLocals": false,
"noImplicitThis": false,
"noImplicitAny": false,
"noImplicitReturns": false,
"declaration": true
"lib": ["esnext", "dom"],
"skipLibCheck": true,
"outDir": "./dist"
},
"include": ["src"]
// "references": [
// {
// "path": "./tsconfig.node.json"
// }
// ]
}
3 changes: 3 additions & 0 deletions packages/web/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["Vue.volar"]
}
16 changes: 16 additions & 0 deletions packages/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Vue 3 + TypeScript + Vite

This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.

## Recommended IDE Setup

- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)

## Type Support For `.vue` Imports in TS

Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:

1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.

You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
13 changes: 13 additions & 0 deletions packages/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
19 changes: 13 additions & 6 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
"name": "@autorecord/web",
"version": "0.0.1",
"description": "Web operation interface",
"main": "./lib/index.js",
"private": true,
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.2.37"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.1.0",
"typescript": "^4.6.4",
"vite": "^3.1.0",
"vue-tsc": "^0.40.4"
},
"files": [
"lib"
],
"repository": "https://github.com/WhiteMinds/LiveAutoRecord",
"author": "WhiteMind",
"license": "LGPL"
Expand Down
1 change: 1 addition & 0 deletions packages/web/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions packages/web/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>

<template>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
<HelloWorld msg="Vite + Vue" />
</template>

<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
1 change: 1 addition & 0 deletions packages/web/src/assets/vue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions packages/web/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>

<template>
<h1>{{ msg }}</h1>

<div class="card">
<button type="button" @click="count++">count is {{ count }}</button>
<p>
Edit
<code>components/HelloWorld.vue</code> to test HMR
</p>
</div>

<p>
Check out
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
>create-vue</a
>, the official Vue + Vite starter
</p>
<p>
Install
<a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
in your IDE for a better DX
</p>
<p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>

<style scoped>
.read-the-docs {
color: #888;
}
</style>
Empty file removed packages/web/src/index.ts
Empty file.
5 changes: 5 additions & 0 deletions packages/web/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

createApp(App).mount('#app')
Loading

0 comments on commit 2314d41

Please sign in to comment.