2 Star 0 Fork 0

longxiaobaiWJ / electron-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 3.95 KB
一键复制 编辑 原始数据 按行查看 历史

electron-learn

Project create

vue create electron-learn

vue add electron-builder

yarn add three@0.134 troisjs

Project setup

yarn install

Compiles and hot-reloads for development

yarn serve

Compiles and minifies for production

yarn build

Lints and fixes files

yarn lint

Customize configuration

See Configuration Reference.

Project build

yarn config set registry https://registry.npm.taobao.org

yarn config set electron_mirror https://npm.taobao.org/mirrors/electron/

yarn config set electron_builder_binaries_mirror https://npm.taobao.org/mirrors/electron-builder-binaries/

create preload.ts

// src/preload.ts
import { contextBridge } from 'electron'
// import { remote } from 'electron'
import { createRightMenu } from '@/electron'
import { nativeImage, ipcRenderer } from 'electron'
const remote = require('@electron/remote')

const handler = createRightMenu()

contextBridge.exposeInMainWorld('electron', {
  nativeImage,
  remote,
  ipcRenderer
})

// src/background.ts
const win = new BrowserWindow({
    width: 1024,
    height: 600,
    webPreferences: {
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: (process.env
        .ELECTRON_NODE_INTEGRATION as unknown) as boolean,
      enableRemoteModule: true,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
      preload: path.join(__dirname, 'preload.js')
    }
  })

// vue.config.js
{
    "electronBuilder": {
        preload: path.join(__dirname, 'src/preload.ts')
    }
}

declare globals import

// src/global.d.ts
interface WinOpts {
  payload?: Record<any, any>;
  windows?: Record<any, any>;
  route: string;
  callback?: (data?: any) => void;
}

interface ElectronAPI {
  remote: {
    dialog: Electron.Dialog;
    Menu: Electron.Menu;
    shell: Electron.Shell;
    MenuItem: Electron.MenuItem;
  };
  ipcRenderer: Electron.IpcRenderer;
  nativeImage: Electron.nativeImage;
  defineCreateMenu: (remote: any) => any[];
  getGlobal: (key: string) => any;
  getCurrentWindow: () => Electron.BrowserWindow;
  updateOpts: (options?: any[]) => void;
  createSmartWindow: (options: WinOpts) => void;
  triggerNotification: (options?: any) => void;
  transferChannel: (channel: string, fnCall: (data: any) => void) => void;
}

declare interface Window {
  version: string;
  electron: ElectronAPI;
}
    
// .eslintrc.js
{
    globals: {
        electron: 'readonly',
    },
}

import library into global declare

// shims-usage.d.ts
declare namespace fetchSetting {
  interface fetchOptions {
      method?: 'GET' | 'POST';
      credentials?: string;
      MeshPublicInterface?: import('troisjs').MeshPublicInterface
      [params: string]: any;
  }
  type defineUpdate = (state: any) => void;
  function fnCall(str: string): void;
  type MeshPublicInterface = import('troisjs').MeshPublicInterface
}

type MeshPublicInterface = import('troisjs').MeshPublicInterface

// usage-import.ts
/* eslint-disable @typescript-eslint/no-unused-vars */
const options: fetchSetting.fetchOptions = {
  method: 'GET',
  credentials: 'include'
}

const fn: fetchSetting.defineUpdate = () => {
  console.log('xxxx')
}

let mesh: MeshPublicInterface

let log: typeof Electron.NodeEventEmitter

asar

npm i -g asar

# /release/win-unpacked/resources
asar extract app.asar ./app-folder

electron-icon-builder

# .npmrc
# phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs/
yarn add -D electron-icon-builder

# electron-icon-builder --input=./public/uni.png --output=dist_electron --flatten

Electron execute JavaScript Code

// code example ./execute.js
console.log('execute callback')
const stand = new Promise((resolve) => {
  resolve('This is a example')
})
stand.then(function (a) { return a })
1
https://gitee.com/frontend_site/electron-learn.git
git@gitee.com:frontend_site/electron-learn.git
frontend_site
electron-learn
electron-learn
master

搜索帮助