refactor(structure): src directory

This commit is contained in:
草鞋没号 2021-11-08 19:17:39 +08:00
parent 2273453449
commit 2dd8f3bf5a
23 changed files with 103 additions and 86 deletions

View File

@ -1,29 +0,0 @@
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
const WINDOWS: Record<string, BrowserWindow | null> = {
main: null,
win1: null,
}
function mainWin() {
WINDOWS.main = new BrowserWindow({
title: 'Main window',
webPreferences: {
preload: join(__dirname, '../preload/index.main')
},
})
if (app.isPackaged) {
WINDOWS.main.loadFile(join(__dirname, '../react-ts/index.html'))
} else {
WINDOWS.main.loadURL(`http://127.0.0.1:${process.env.PORT}`)
WINDOWS.main.maximize()
}
}
app.whenReady().then(mainWin)
app.on('window-all-closed', () => {
Object.keys(WINDOWS).forEach((key) => WINDOWS[key] = null)
})

View File

@ -1 +0,0 @@
console.log('index.win1.ts')

View File

@ -1,49 +0,0 @@
/** docoment ready */
export function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
return new Promise(resolve => {
if (condition.includes(document.readyState)) {
resolve(true)
} else {
document.addEventListener('readystatechange', () => {
if (condition.includes(document.readyState)) {
resolve(true)
}
})
}
})
}
/** Inject ws related code */
export function injectWsCode(options: {
host: string
port: string | number
}) {
const oScript = document.createElement('script')
oScript.id = 'ws-preload-hot-reload'
oScript.innerHTML = `
${__ws_hot_reload_for_preload.toString()}
${__ws_hot_reload_for_preload.name}(${JSON.stringify(options)})
`
document.body.appendChild(oScript)
}
function __ws_hot_reload_for_preload(options: { host: string; port: string | number }) {
const ws = new WebSocket(`ws://${options.host}:${options.port}`)
ws.onmessage = function (ev) {
try {
console.log('[preload] ws.onmessage:', ev.data)
const data = JSON.parse(ev.data) // { "cmd": "string", data: "string|number" }
if (data.cmd === 'reload') {
setTimeout(() => window.location.reload(), 999)
}
} catch (error) {
console.warn(`ws.onmessage should be accept "JSON.string" formatted string.`)
console.error(error)
}
}
}

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

@ -0,0 +1,29 @@
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
let win: BrowserWindow | null = null
async function mainWin() {
win = new BrowserWindow({
title: 'Main window',
webPreferences: {
preload: join(__dirname, '../preload/index.cjs')
},
})
if (app.isPackaged) {
win.loadFile(join(__dirname, '../react-ts/index.html'))
} else {
const pkg = await import('../../package.json')
const url = `http://${pkg.env.HOST || '127.0.0.1'}:${pkg.env.PORT}`
win.loadURL(url)
win.maximize()
}
}
app.whenReady().then(mainWin)
app.on('window-all-closed', () => {
win = null
app.quit()
})

27
src/main/vite.config.ts Normal file
View File

@ -0,0 +1,27 @@
import { join } from 'path'
import { builtinModules } from 'module'
import { defineConfig } from 'vite'
export default defineConfig({
mode: process.env.NODE_ENV,
root: __dirname,
build: {
outDir: join(process.cwd(), 'dist/main'),
lib: {
entry: 'index.ts',
formats: ['cjs'],
},
sourcemap: false,
minify: false,
emptyOutDir: true,
rollupOptions: {
external: [
...builtinModules,
'electron',
],
output: {
entryFileNames: '[name].cjs',
},
},
},
})

View File

@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'
import { contextBridge, ipcRenderer } from 'electron'
import { domReady, injectWsCode } from './utils'
import { domReady } from './utils'
import { useLoading } from './loading'
const isDev = process.env.NODE_ENV === 'development'
@ -11,10 +11,6 @@ const { appendLoading, removeLoading } = useLoading()
await domReady()
appendLoading()
isDev && injectWsCode({
host: '127.0.0.1',
port: process.env.PORT_WS as string,
})
})();
// ---------------------------------------------------

15
src/preload/utils.ts Normal file
View File

@ -0,0 +1,15 @@
/** docoment ready */
export function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
return new Promise(resolve => {
if (condition.includes(document.readyState)) {
resolve(true)
} else {
document.addEventListener('readystatechange', () => {
if (condition.includes(document.readyState)) {
resolve(true)
}
})
}
})
}

View File

@ -0,0 +1,27 @@
import { join } from 'path'
import { builtinModules } from 'module'
import { defineConfig } from 'vite'
export default defineConfig({
mode: process.env.NODE_ENV,
root: __dirname,
build: {
outDir: join(process.cwd(), 'dist/preload'),
lib: {
entry: 'index.ts',
formats: ['cjs'],
},
sourcemap: false,
minify: false,
emptyOutDir: true,
rollupOptions: {
external: [
...builtinModules,
'electron',
],
output: {
entryFileNames: '[name].cjs',
},
},
},
})

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: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -1,5 +1,5 @@
{
"extends": "../paths.json",
"extends": "../../paths.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,

View File

@ -1,13 +1,15 @@
import { join } from 'path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import pkg from '../package.json'
import pkg from '../../package.json'
// https://vitejs.dev/config/
export default defineConfig({
root: __dirname,
plugins: [react()],
base: './',
build: {
emptyOutDir: true,
minify: false,
outDir: join(process.cwd(), 'dist/react-ts'),
},