关灯
开启左侧

使用electron把前端代码打包成EXE

[复制链接]
doubleyong 发表于 2018-11-26 11:24:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
 
EXE文件打包方法:
1,全局安装electron  npm install electron -g
2,检查electron的版本
3,需要package.json文件、main.js文件并且主页HTML文件名为index.htnl (这三个文件需要放在根目录下)
4,运行electron-packager . appTest --platform=win32 --arch=x64 --electron-version=1.8.4
(.appTest是生产exe的子文件名称,version版本号要填写本机electron的版本号)
5,最后打包完成的文件在appTest

  1. package.json
  2. {
  3. "name" : "app-name",
  4. "version" : "0.1.0",
  5. "main" : "main.js"
  6. }
复制代码
main.js
  1. const {app, BrowserWindow} = require('electron')
  2. const path = require('path')
  3. const url = require('url')
  4. // Keep a global reference of the window object, if you don't, the window will
  5. // be closed automatically when the JavaScript object is garbage collected.
  6. let win
  7. function createWindow () {
  8. // Create the browser window.
  9. win = new BrowserWindow({width: 800, height: 600})
  10. // and load the index.html of the app.
  11. win.loadURL(url.format({
  12. pathname: path.join(__dirname, 'index.html'),
  13. protocol: 'file:',
  14. slashes: true
  15. }))
  16. // Open the DevTools.
  17. // win.webContents.openDevTools()
  18. // Emitted when the window is closed.
  19. win.on('closed', () => {
  20. // Dereference the window object, usually you would store windows
  21. // in an array if your app supports multi windows, this is the time
  22. // when you should delete the corresponding element.
  23. win = null
  24. })
  25. }
  26. // This method will be called when Electron has finished
  27. // initialization and is ready to create browser windows.
  28. // Some APIs can only be used after this event occurs.
  29. app.on('ready', createWindow)
  30. // Quit when all windows are closed.
  31. app.on('window-all-closed', () => {
  32. // On macOS it is common for applications and their menu bar
  33. // to stay active until the user quits explicitly with Cmd + Q
  34. if (process.platform !== 'darwin') {
  35. app.quit()
  36. }
  37. })
  38. app.on('activate', () => {
  39. // On macOS it's common to re-create a window in the app when the
  40. // dock icon is clicked and there are no other windows open.
  41. if (win === null) {
  42. createWindow()
  43. }
  44. })
  45. // In this file you can include the rest of your app's specific main process
  46. // code. You can also put them in separate files and require them here.
复制代码
转自:http://bugshouji.com/shareweb/t630
 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

排行榜
关闭

站长推荐上一条 /1 下一条

官方微信

全国服务热线:

400-0708-360

公司地址:国家西部信息安全产业基地(成都市高新区云华路333号)

邮编:610000    Email:2908503813@qq.com

Copyright   ©2015-2016  EOIT论坛Powered by©Discuz!    ( 蜀ICP备11000634号-7 )