refactor(structure): src directory
This commit is contained in:
		
							parent
							
								
									2273453449
								
							
						
					
					
						commit
						2dd8f3bf5a
					
				| 
						 | 
					@ -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)
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1 +0,0 @@
 | 
				
			||||||
console.log('index.win1.ts')
 | 
					 | 
				
			||||||
| 
						 | 
					@ -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)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -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()
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
						 | 
					@ -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',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
import fs from 'fs'
 | 
					import fs from 'fs'
 | 
				
			||||||
import path from 'path'
 | 
					import path from 'path'
 | 
				
			||||||
import { contextBridge, ipcRenderer } from 'electron'
 | 
					import { contextBridge, ipcRenderer } from 'electron'
 | 
				
			||||||
import { domReady, injectWsCode } from './utils'
 | 
					import { domReady } from './utils'
 | 
				
			||||||
import { useLoading } from './loading'
 | 
					import { useLoading } from './loading'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const isDev = process.env.NODE_ENV === 'development'
 | 
					const isDev = process.env.NODE_ENV === 'development'
 | 
				
			||||||
| 
						 | 
					@ -11,10 +11,6 @@ const { appendLoading, removeLoading } = useLoading()
 | 
				
			||||||
  await domReady()
 | 
					  await domReady()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  appendLoading()
 | 
					  appendLoading()
 | 
				
			||||||
  isDev && injectWsCode({
 | 
					 | 
				
			||||||
    host: '127.0.0.1',
 | 
					 | 
				
			||||||
    port: process.env.PORT_WS as string,
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ---------------------------------------------------
 | 
					// ---------------------------------------------------
 | 
				
			||||||
| 
						 | 
					@ -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)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -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',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
		 Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB  | 
| 
		 Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB  | 
| 
		 Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB  | 
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  "extends": "../paths.json",
 | 
					  "extends": "../../paths.json",
 | 
				
			||||||
  "compilerOptions": {
 | 
					  "compilerOptions": {
 | 
				
			||||||
    "target": "ESNext",
 | 
					    "target": "ESNext",
 | 
				
			||||||
    "useDefineForClassFields": true,
 | 
					    "useDefineForClassFields": true,
 | 
				
			||||||
| 
						 | 
					@ -1,13 +1,15 @@
 | 
				
			||||||
import { join } from 'path'
 | 
					import { join } from 'path'
 | 
				
			||||||
import { defineConfig } from 'vite'
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
import react from '@vitejs/plugin-react'
 | 
					import react from '@vitejs/plugin-react'
 | 
				
			||||||
import pkg from '../package.json'
 | 
					import pkg from '../../package.json'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://vitejs.dev/config/
 | 
					// https://vitejs.dev/config/
 | 
				
			||||||
export default defineConfig({
 | 
					export default defineConfig({
 | 
				
			||||||
 | 
					  root: __dirname,
 | 
				
			||||||
  plugins: [react()],
 | 
					  plugins: [react()],
 | 
				
			||||||
  base: './',
 | 
					  base: './',
 | 
				
			||||||
  build: {
 | 
					  build: {
 | 
				
			||||||
 | 
					    emptyOutDir: true,
 | 
				
			||||||
    minify: false,
 | 
					    minify: false,
 | 
				
			||||||
    outDir: join(process.cwd(), 'dist/react-ts'),
 | 
					    outDir: join(process.cwd(), 'dist/react-ts'),
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
		Loading…
	
		Reference in New Issue