From 79cd832cfc54b2505da3bd1f9ec435b1693bf014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Mon, 31 Aug 2020 09:50:57 +0800 Subject: [PATCH] chore: upate --- package.json | 5 ++--- src/main/index.ts | 5 ++++- src/main/preload.js | 5 +++++ src/render/App.vue | 2 +- src/render/index.html | 2 +- src/render/main.js | 5 ----- src/render/main.ts | 9 +++++++++ src/render/shims.d.ts | 4 ++++ src/render/source.d.ts | 4 ++++ typings/typings.d.ts | 5 +++++ vite.config.ts | 11 ++++++++++- 11 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/main/preload.js delete mode 100644 src/render/main.js create mode 100644 src/render/main.ts create mode 100644 src/render/shims.d.ts create mode 100644 src/render/source.d.ts create mode 100644 typings/typings.d.ts diff --git a/package.json b/package.json index 1e55b1d..5ff5e42 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "electron-vue", "version": "0.0.1", - "main": "src/main/_.js", + "main": "dist/main/_.js", "author": "草鞋没号 <308487730@qq.com>", "license": "MIT", "scripts": { @@ -17,8 +17,7 @@ "appId": "308487730@qq.com", "files": [ "!node_modules", - "src/main/**", - "src/render/dist/**" + "dist/**" ], "mac": { "artifactName": "${productName}_setup_${version}.${ext}", diff --git a/src/main/index.ts b/src/main/index.ts index aafad94..3714c68 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -15,11 +15,14 @@ function createWin() { win = new BrowserWindow({ width: 1024, height: 768, + webPreferences: { + nodeIntegration: true, + }, }) const URL = is_dev ? `http://localhost:${process.env.PORT}` // vite 启动的服务器地址 - : `file://${join(__dirname, '../render/dist/index.html')}` // vite 构建后的静态文件地址 + : `file://${join(__dirname, '../../dist/render/index.html')}` // vite 构建后的静态文件地址 win.loadURL(URL) } diff --git a/src/main/preload.js b/src/main/preload.js new file mode 100644 index 0000000..601ee49 --- /dev/null +++ b/src/main/preload.js @@ -0,0 +1,5 @@ +const { ipcRenderer } = require('electron') + +window.stopLoading = function() { + ipcRenderer.send('stop-loading-main') +} diff --git a/src/render/App.vue b/src/render/App.vue index 12d83b4..be70e71 100644 --- a/src/render/App.vue +++ b/src/render/App.vue @@ -3,7 +3,7 @@ Electron logo Vue logo - + + diff --git a/src/render/main.js b/src/render/main.js deleted file mode 100644 index 50a4dab..0000000 --- a/src/render/main.js +++ /dev/null @@ -1,5 +0,0 @@ -import { createApp } from 'vue' -import App from './App.vue' -import './index.css' - -createApp(App).mount('#app') diff --git a/src/render/main.ts b/src/render/main.ts new file mode 100644 index 0000000..b5075ac --- /dev/null +++ b/src/render/main.ts @@ -0,0 +1,9 @@ +import { createApp } from 'vue' +import App from './App.vue' +import './index.css' +// vite 会编译 import 的形式;所以 electron 及 node.js 内置模块用 require 形式 +const { ipcRenderer } = require('electron') + +console.log(ipcRenderer) + +createApp(App as any).mount('#app') diff --git a/src/render/shims.d.ts b/src/render/shims.d.ts new file mode 100644 index 0000000..d9f24fa --- /dev/null +++ b/src/render/shims.d.ts @@ -0,0 +1,4 @@ +declare module '*.vue' { + import Vue from 'vue' + export default Vue +} diff --git a/src/render/source.d.ts b/src/render/source.d.ts new file mode 100644 index 0000000..451793d --- /dev/null +++ b/src/render/source.d.ts @@ -0,0 +1,4 @@ +declare const React: string +declare module '*.json' +declare module '*.png' +declare module '*.jpg' diff --git a/typings/typings.d.ts b/typings/typings.d.ts new file mode 100644 index 0000000..5b40a42 --- /dev/null +++ b/typings/typings.d.ts @@ -0,0 +1,5 @@ + +interface Window { + /** 浏览器下开发,关闭 electron 载入动画 */ + stopLoading: () => void +} diff --git a/vite.config.ts b/vite.config.ts index 272bce5..3b48d14 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,11 +6,20 @@ import { UserConfig } from 'vite' import dotenv from 'dotenv' dotenv.config({ path: join(__dirname, '.env') }) +const root = join(__dirname, 'src/render') const config: UserConfig = { - root: join(__dirname, 'src/render'), + root, port: +process.env.PORT, base: './', + outDir: join(__dirname, 'dist/render'), + alias: { + // 别名必须以 / 开头、结尾 + '/@/': root, + }, + optimizeDeps: { + allowNodeBuiltins: ['electron-is-dev', 'electron-store', 'electron'] + }, } export default config