项目作者: soulehshaikh99

项目描述 :
Discover the easiest way to get started with the blend of Sapper and Electron JS App
高级语言: JavaScript
项目地址: git://github.com/soulehshaikh99/create-sapper-electron-app.git
创建时间: 2020-08-05T09:18:37Z
项目社区:https://github.com/soulehshaikh99/create-sapper-electron-app

开源协议:MIT License

下载



Electron Sapper Crossover Banner



The boilerplate code to get started creating Cross-platform Desktop Apps with Electron and Sapper as front-end technology.






forthebadge forthebadge


forthebadge forthebadge forthebadge

js-standard-style

✒️ Overview

The aim of this project is to provide Web Developers using sapper the power to create cross-platform desktop apps using electron.

🧐 What packages does the project use?

electron enables you to create desktop applications with pure JavaScript by providing a runtime with rich native (operating system) APIs. You could see it as a variant of the Node.js runtime that is focused on desktop applications instead of web servers.

electron-builder is used as a complete solution to package and build a ready for distribution (supports Numerous target formats) Electron app with “auto update” support out of the box.

electron-serve is used for Static file serving for Electron apps.

sapper is a framework for building web applications of all sizes, with a beautiful development experience and flexible filesystem-based routing. Unlike single-page apps, Sapper doesn’t compromise on SEO, progressive enhancement or the initial load experience — but unlike traditional server-rendered apps, navigation is instantaneous for that app-like feel.

concurrently is used to run multiple commands concurrently.

wait-on is used as it can wait for sockets, and http(s) resources to become available.

🚀 Getting Started

Note: If you wish to use npm over yarn then modify package.json by replacing yarn with npm in electron-dev and preelectron-pack scripts.
But I strongly recommend using yarn as it is a better choice when compared to npm.

🤓 Use this boilerplate

  1. # Clone the Project
  2. # Use degit scaffolding tool
  3. $ npx degit soulehshaikh99/create-sapper-electron-app create-sapper-electron-app
  4. # or GitHub CLI Users
  5. $ gh repo clone https://github.com/soulehshaikh99/create-sapper-electron-app.git
  6. # or Normal Git Users
  7. $ git clone https://github.com/soulehshaikh99/create-sapper-electron-app.git
  8. # Switch location to the cloned directory
  9. $ cd create-sapper-electron-app
  10. # Install dependencies
  11. $ yarn # or npm install
  12. # Run your app
  13. $ yarn electron-dev # or npm run electron-dev
  14. # Package Your App
  15. $ yarn electron-pack # or npm run electron-pack

💫 Create this boilerplate from scratch (Manual Setup)

1) Use degit scaffolding tool to get started with sapper-template.

  1. $ npx degit "sveltejs/sapper-template#webpack" create-sapper-electron-app

2) Switch to project directory

  1. $ cd create-sapper-electron-app

3) Move all dependencies to devDependencies using IDE / Text Editor

  1. # It should look something like this
  2. "dependencies": {},
  3. "devDependencies": {
  4. "compression": "^1.7.1",
  5. "npm-run-all": "^4.1.5",
  6. "polka": "next",
  7. "sapper": "^0.28.0",
  8. "sirv": "^1.0.0",
  9. "svelte": "^3.17.3",
  10. "svelte-loader": "^2.9.0",
  11. "webpack": "^4.7.0",
  12. "webpack-modules": "^1.0.0"
  13. }

4) Install Dependencies

  1. $ yarn # or npm install

5) Install Development Dependencies

  1. $ yarn add --dev electron electron-builder wait-on concurrently
  2. # npm i -D electron electron-builder wait-on concurrently

6) Install Production Dependency

  1. $ yarn add electron-serve # or npm i electron-serve

7) Your dependencies should look something like this

  1. "dependencies": {
  2. "electron-serve": "^1.0.0"
  3. },
  4. "devDependencies": {
  5. "compression": "^1.7.1",
  6. "concurrently": "^5.3.0",
  7. "electron": "^9.2.1",
  8. "electron-builder": "^22.8.0",
  9. "npm-run-all": "^4.1.5",
  10. "polka": "next",
  11. "sapper": "^0.28.0",
  12. "sirv": "^1.0.0",
  13. "svelte": "^3.17.3",
  14. "svelte-loader": "^2.9.0",
  15. "wait-on": "^5.2.0",
  16. "webpack": "^4.7.0",
  17. "webpack-modules": "^1.0.0"
  18. }

8) Download the app icon

favicon.png | logo-192.png | logo-512.png and place it in the static directory, overwriting the original ones.

9) Create main.js file (serves as entry point for Electron App’s Main Process)

  1. # Windows Users
  2. $ fsutil file createnew main.js 0
  3. # notepad main.js
  4. # Linux and macOS Users
  5. $ touch main.js

10) Paste the below code in main.js file

  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, dialog } = require('electron');
  3. const path = require('path');
  4. const serve = require('electron-serve');
  5. const loadURL = serve({ directory: '__sapper__/export' });
  6. // Keep a global reference of the window object, if you don't, the window will
  7. // be closed automatically when the JavaScript object is garbage collected.
  8. let mainWindow;
  9. function isDev() {
  10. return !app.isPackaged;
  11. }
  12. function createWindow() {
  13. // Create the browser window.
  14. mainWindow = new BrowserWindow({
  15. width: 800,
  16. height: 600,
  17. webPreferences: {
  18. nodeIntegration: true
  19. },
  20. // Use this in development mode.
  21. icon: isDev() ? path.join(process.cwd(), 'static/logo-512.png') : path.join(__dirname, '__sapper__/export/logo-512.png'),
  22. // Use this in production mode.
  23. // icon: path.join(__dirname, '__sapper__/export/logo-512.png'),
  24. show: false
  25. });
  26. // This block of code is intended for development purpose only.
  27. // Delete this entire block of code when you are ready to package the application.
  28. if (isDev()) {
  29. mainWindow.loadURL('http://localhost:3000/');
  30. } else {
  31. loadURL(mainWindow);
  32. }
  33. // Uncomment the following line of code when app is ready to be packaged.
  34. // loadURL(mainWindow);
  35. // Open the DevTools and also disable Electron Security Warning.
  36. // process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true;
  37. // mainWindow.webContents.openDevTools();
  38. // Emitted when the window is closed.
  39. mainWindow.on('closed', function () {
  40. // Dereference the window object, usually you would store windows
  41. // in an array if your app supports multi windows, this is the time
  42. // when you should delete the corresponding element.
  43. mainWindow = null
  44. });
  45. // Emitted when the window is ready to be shown
  46. // This helps in showing the window gracefully.
  47. mainWindow.once('ready-to-show', () => {
  48. mainWindow.show()
  49. });
  50. }
  51. // This method will be called when Electron has finished
  52. // initialization and is ready to create browser windows.
  53. // Some APIs can only be used after this event occurs.
  54. app.on('ready', createWindow);
  55. // Quit when all windows are closed.
  56. app.on('window-all-closed', function () {
  57. // On macOS it is common for applications and their menu bar
  58. // to stay active until the user quits explicitly with Cmd + Q
  59. if (process.platform !== 'darwin') app.quit()
  60. });
  61. app.on('activate', function () {
  62. // On macOS it's common to re-create a window in the app when the
  63. // dock icon is clicked and there are no other windows open.
  64. if (mainWindow === null) createWindow()
  65. });
  66. // In this file you can include the rest of your app's specific main process
  67. // code. You can also put them in separate files and require them here.

11) Update the script section of package.json

  1. # Add this scripts
  2. "electron": "wait-on http://localhost:3000 && electron .",
  3. "electron-dev": "concurrently \"yarn run dev\" \"yarn run electron\"",
  4. "preelectron-pack": "yarn run export",
  5. "electron-pack": "electron-builder"
  6. # You should end up with something similar
  7. "scripts": {
  8. "dev": "sapper dev",
  9. "build": "sapper build",
  10. "export": "sapper export",
  11. "start": "node __sapper__/build",
  12. "cy:run": "cypress run",
  13. "cy:open": "cypress open",
  14. "test": "run-p --race dev cy:run",
  15. "electron": "wait-on http://localhost:3000 && electron .",
  16. "electron-dev": "concurrently \"yarn run dev\" \"yarn run electron\"",
  17. "preelectron-pack": "yarn run export",
  18. "electron-pack": "electron-builder"
  19. }

12) Add the following configuration in package.json

Note: build configuration is used by electron-builder, modify it if you wish to add more packaging and native distribution options for different OS Platforms.

  1. "name": "create-sapper-electron-app", # By default it is TODO
  2. "main": "main.js", # Application Entry Point, please verify entry point is set to main.js
  3. "build": {
  4. "icon": "static/logo-512.png",
  5. "productName": "Sapper and Electron App",
  6. "files": [
  7. "__sapper__/export/**/*",
  8. "main.js"
  9. ],
  10. "win": {}, # Windows Specific Configuration
  11. "linux": {}, # Linux Specific Configuration
  12. "mac": {} # MacOs Specific Configuration
  13. }

14) Test drive your app

  1. # Run your app
  2. $ yarn electron-dev # or npm run electron-dev
  3. # Package Your App
  4. $ yarn electron-pack # or npm run electron-pack

💯 Result


Electron Sapper Window Screeenshot

😍 Made with ❤️ from Souleh

forthebadge

📋 License:


Licensed under the MIT License.