把现有的typesctipt+react项目接入到electron

前端架构师
• 阅读 3891
项目地址 ts-react-electron

之前有发过一个typesctipt+react的简单模板,写起来很舒服.考虑到以后的需要,先把它接入到electron,供备用!

先来讲一下一些差异点:

  • webpack配置target为electron-renderer(否则热更新之类的功能会出问题)

  • 启动本地调试服务器时不打开浏览器

  • 开发环境electron loadURL打开localhost(视为远程地址),生产环境为本地文件

  • 控制面板的切换

具体实现

  • webpack配置target的方法(与plugin同级)

     plugins: [],
     target: 'electron-renderer'
    
  • 修改build/dev-server.js

     // if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
       // opn(uri)
     // }

    实现不自动打开浏览器

  • 这个项目是我copy electron的quick starter下来改的, 直接修改main.js(不使用ts)

       const path = require('path')
       const url = require('url')
       const { app, BrowserWindow, ipcMain } = require('electron')
       const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer')
       
       // Keep a global reference of the window object, if you don't, the window will
       // be closed automatically when the JavaScript object is garbage collected.
       let mainWindow
       
       function createWindow () {
         // Create the browser window.
         mainWindow = new BrowserWindow({width: 1000, height: 800})
       
         if (process.env.NODE_ENV !== 'production') {
           mainWindow.webContents.openDevTools()
           installExtension(REACT_DEVELOPER_TOOLS)
           .then((name) => {
             console.log(`Added Extension:  ${name}`)
             mainWindow.loadURL("http://localhost:3333")
           })
           .catch((err) => console.log('An error occurred: ', err))
         } else {
           mainWindow.loadURL(url.format({
             pathname: path.join(__dirname, './index.html'),
             protocol: 'file:',
             slashes: true
           }));
         }
       
         // Emitted when the window is closed.
         mainWindow.on('closed', function () {
           // Dereference the window object, usually you would store windows
           // in an array if your app supports multi windows, this is the time
           // when you should delete the corresponding element.
           mainWindow = null
         })
       }
       
       // 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.on('ready', createWindow)
       
       // Quit when all windows are closed.
       app.on('window-all-closed', function () {
         // On OS X it is common for applications and their menu bar
         // to stay active until the user quits explicitly with Cmd + Q
         app.quit()
       })
       
       app.on('activate', function () {
         // On OS X 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 (mainWindow === null) {
           createWindow()
         }
       })
       
       // 控制调试面板
       ipcMain.on('toggle-devtool', (event, args) => {
         mainWindow.webContents.toggleDevTools()
       })
    

    安装electron-devtools-installer(非必须)
    调用调试面板的方法参考项目中的bingGlobalFunc.ts

对现阶段来说, 这个工程算是可以满足到自己了,最重要的是每一次都能学习到好多东西,有需要的朋友们可以参考,也希望能获取更多的意见和建议, 谢谢

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这