Skip to content

Commit c1c72eb

Browse files
committed
modify normal build process instead of using electron-webpack
0 parents  commit c1c72eb

File tree

10 files changed

+7090
-0
lines changed

10 files changed

+7090
-0
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Vue CLI Plugin Electron Builder
2+
3+
A Vue Cli 3 plugin for Electron with no required configuration that uses [Electron Builder](https://www.electron.build/) and [Electron Webpack](https://webpack.electron.build/).
4+
5+
**IMPORTANT: Your app must be created with Vue-CLI 3 (vue create my-app), will not work with Vue-CLI 2 (vue init webpack my-app)!**
6+
7+
## Quick Start:
8+
9+
Open a terminal in the directory of your app created with Vue-CLI 3.
10+
11+
Then, install and invoke the generator of vue-cli-plugin-electron-builder by running:
12+
13+
`vue add electron-builder`
14+
15+
That's It! Your ready to go!
16+
17+
### To start a development server:
18+
19+
If you use [Yarn](https://yarnpkg.com/en/) (strongly recommended):
20+
21+
`yarn serve:electron`
22+
23+
or if you use NPM:
24+
25+
`npm run serve:electron`
26+
27+
### To build your app:
28+
29+
With yarn:
30+
31+
`yarn build:electron`
32+
33+
or with NPM:
34+
35+
`npm run build:electron`
36+
37+
### Folder Structure:
38+
39+
```
40+
├── dist/ # where electron-webpack outputs compiled files (this will overwrite normal build files)
41+
42+
│ └── ...
43+
44+
├── dist_electron/
45+
46+
│ ├── [target platform]-unpacked # unpacked Electron app (main exe and supporting files)
47+
48+
│ ├── [application name] setup [version].[target binary (exe|dmg|rpm...)] # installer for Electron app
49+
50+
│ └── ...
51+
52+
├── src/
53+
54+
│ ├─── main/
55+
56+
│ │ └── [main|index].[js|ts] # Electron entry file (for Electron's main process)
57+
58+
│ ├── [main|index].[js|ts] # your app's entry file (for Electron's render process)
59+
60+
│ └── ...
61+
62+
├── electron-builder.[json|yml] # electron-builder configuration options (can also be placed in package.json under the "build" key)
63+
64+
├── electron-webpack.[json|yml] # electron-webpack configuration options (can also be placed in package.json under the "electronWebpack" key)
65+
66+
├── package.json # your app's package.json file
67+
68+
├── ...
69+
```
70+
71+
## Further Configuration:
72+
73+
Initial configuration is already set for you in your app's package.json, but if you want to modify it:
74+
75+
### CLI Options:
76+
77+
When building your app, any arguments will be passed to electron-builder. To pass an argument/arguments to electron-webpack, place them after --webpack.
78+
79+
**Example:**
80+
81+
`yarn build:electron [electron-builder options] --webpack [electron-webpack options]`
82+
83+
### Electron Builder:
84+
85+
To see avalible options, check out [Electron Builder Configuration Options](https://www.electron.build/configuration/configuration)
86+
87+
As per Electron Builder's documentation, they can be applied:
88+
89+
> * in the `package.json` file of your project using the `build` key on the top level:
90+
>
91+
> ```
92+
> "build": {
93+
> "appId": "com.example.app"
94+
> }
95+
> ```
96+
>
97+
> * or through the `--config <path/to/yml-or-json5-or-toml>` option (defaults to `electron-builder.yml`(or `json`, or [json5](http://json5.org/), or [toml](https://github.com/toml-lang/toml))).
98+
>
99+
> ```
100+
> appId: "com.example.app"
101+
> ```
102+
>
103+
> If you want to use [toml](https://en.wikipedia.org/wiki/TOML), please install `yarn add toml --dev`.
104+
>
105+
> Most of the options accept `null` — for example, to explicitly set that DMG icon must be default volume icon from the OS and default rules must be not applied (i.e. use application icon as DMG icon), set `dmg.icon` to `null`
106+
107+
### Electron Webpack:
108+
109+
To see avalible options, check out [Electron Webpack Configuration Options](https://webpack.electron.build/configuration)
110+
111+
As per Electron Webpack's documentation, they can be applied:
112+
113+
> in `package.json` at `electronWebpack` or in a separate `electron-webpack.(json|json5|yml)`.
114+
115+
To modify the webpack config for electron builds only, use the webpackConfig object under vue-cli-plugin-electron-builder's plugin options in `vue.config.js`.
116+
117+
```
118+
// vue.config.js
119+
120+
module.exports = {
121+
configureWebpack: {
122+
// Non-electron build/serve configuration
123+
// Aliases will be automatically copied from standard config to electron config
124+
},
125+
pluginOptions: {
126+
electronBuilder: {
127+
webpackConfig: {
128+
// your electron-only webpack config
129+
}
130+
}
131+
}
132+
};
133+
```
134+
135+
#### Adding TypeScript Support:
136+
137+
When you invoke/add vue-cli-plugin-electron-builder, it will ask you if you use TypeScript and configure accordingly. However, if you answered no and decide to add TypeScript later on, you must install electron-webpack-ts: `yarn add electron-webpack-ts --dev` (or with NPM: `npm install --save-dev electron-webpack-ts`).

background.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
import { app, BrowserWindow } from 'electron'
4+
import * as path from 'path'
5+
import { format as formatUrl } from 'url'
6+
7+
const isDevelopment = process.env.NODE_ENV !== 'production'
8+
9+
// global reference to mainWindow (necessary to prevent window from being garbage collected)
10+
let mainWindow
11+
12+
function createMainWindow () {
13+
const window = new BrowserWindow()
14+
15+
if (isDevelopment) {
16+
window.webContents.openDevTools()
17+
window.loadURL(`http://localhost:${process.env.WEBPACK_DEV_SERVER_URL}`)
18+
} else {
19+
window.loadURL(
20+
formatUrl({
21+
pathname: path.join(__dirname, 'index.html'),
22+
protocol: 'file',
23+
slashes: true
24+
})
25+
)
26+
}
27+
28+
window.on('closed', () => {
29+
mainWindow = null
30+
})
31+
32+
window.webContents.on('devtools-opened', () => {
33+
window.focus()
34+
setImmediate(() => {
35+
window.focus()
36+
})
37+
})
38+
39+
return window
40+
}
41+
42+
// quit application when all windows are closed
43+
app.on('window-all-closed', () => {
44+
// on macOS it is common for applications to stay open until the user explicitly quits
45+
if (process.platform !== 'darwin') {
46+
app.quit()
47+
}
48+
})
49+
50+
app.on('activate', () => {
51+
// on macOS it is common to re-create a window even after all windows have been closed
52+
if (mainWindow === null) {
53+
mainWindow = createMainWindow()
54+
}
55+
})
56+
57+
// create main BrowserWindow when electron is ready
58+
app.on('ready', () => {
59+
mainWindow = createMainWindow()
60+
})

generator/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = api => {
2+
api.render('./template')
3+
api.extendPackage({
4+
scripts: {
5+
'build:electron': 'vue-cli-service build:electron',
6+
'serve:electron': 'vue-cli-service serve:electron'
7+
},
8+
devDependencies: {
9+
electron: '^2.0.2'
10+
},
11+
main: 'dist/background.js'
12+
})
13+
}

generator/template/_gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
extend: '@vue/cli-service/generator/template/_gitignore'
3+
replace: !!js/regexp /\/dist/
4+
---
5+
6+
/dist
7+
/dist_electron

generator/template/src/background.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
import { app, BrowserWindow } from 'electron'
4+
import * as path from 'path'
5+
import { format as formatUrl } from 'url'
6+
7+
const isDevelopment = process.env.NODE_ENV !== 'production'
8+
9+
// global reference to mainWindow (necessary to prevent window from being garbage collected)
10+
let mainWindow
11+
12+
function createMainWindow () {
13+
const window = new BrowserWindow()
14+
15+
if (isDevelopment) {
16+
window.webContents.openDevTools()
17+
window.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
18+
} else {
19+
window.loadURL(
20+
formatUrl({
21+
pathname: path.join(__dirname, 'index.html'),
22+
protocol: 'file',
23+
slashes: true
24+
})
25+
)
26+
}
27+
28+
window.on('closed', () => {
29+
mainWindow = null
30+
})
31+
32+
window.webContents.on('devtools-opened', () => {
33+
window.focus()
34+
setImmediate(() => {
35+
window.focus()
36+
})
37+
})
38+
39+
return window
40+
}
41+
42+
// quit application when all windows are closed
43+
app.on('window-all-closed', () => {
44+
// on macOS it is common for applications to stay open until the user explicitly quits
45+
if (process.platform !== 'darwin') {
46+
app.quit()
47+
}
48+
})
49+
50+
app.on('activate', () => {
51+
// on macOS it is common to re-create a window even after all windows have been closed
52+
if (mainWindow === null) {
53+
mainWindow = createMainWindow()
54+
}
55+
})
56+
57+
// create main BrowserWindow when electron is ready
58+
app.on('ready', () => {
59+
mainWindow = createMainWindow()
60+
})

0 commit comments

Comments
 (0)