You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-17Lines changed: 34 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,3 @@
1
-
2
1
# Vue CLI Plugin Electron Builder
3
2
4
3
A Vue Cli 3 plugin for Electron with no required configuration that uses [Electron Builder](https://www.electron.build/).
@@ -64,10 +63,11 @@ or with NPM:
64
63
To see avalible options, check out [Electron Builder Configuration Options](https://www.electron.build/configuration/configuration)
65
64
66
65
They can be placed under the `builderOptions` key in vue-cli-plugin-electron-builder's plugin options in `vue.config.js`
66
+
67
67
```javascript
68
68
// vue.config.js
69
69
70
-
module.exports= {
70
+
module.exports= {
71
71
pluginOptions: {
72
72
electronBuilder: {
73
73
builderOptions: {
@@ -76,8 +76,10 @@ module.exports= {
76
76
}
77
77
}
78
78
}
79
-
```
79
+
```
80
+
80
81
### Webpack configuration:
82
+
81
83
Your regular config is used for bundling the renderer process (your app). To modify the webpack config for the electron main process only, use the `chainWebpackMainProcess` function under vue-cli-plugin-electron-builder's plugin options in `vue.config.js`. To learn more about webpack chaining, see [webpack-chain](https://github.com/mozilla-neutrino/webpack-chain). The function should take a config argument, modify it through webpack-chain, and then return it.
82
84
83
85
**Note: Do NOT change the webpack output directory for the main process! See changing output directory below for more info. To change the entry point for the main process, use the `mainProcessFile` key, DO NOT modify it in through chaining.**
@@ -91,25 +93,32 @@ module.exports = {
91
93
},
92
94
pluginOptions: {
93
95
electronBuilder: {
94
-
chainWebpackMainProcess:(config)=> {
96
+
chainWebpackMainProcess:config=> {
95
97
// Chain webpack config for electron main process only
96
98
},
97
99
mainProcessFile:'src/myBackgroundFile.js'
98
100
}
99
101
}
100
-
};
102
+
}
101
103
```
104
+
102
105
### Handling static assets:
106
+
103
107
#### Renderer process (main app):
108
+
104
109
In the renderer process, static assets work similarly to a regular app. Read Vue CLI's documentation [here](https://cli.vuejs.org/guide/html-and-static-assets.html) before continuing. However, there are a few changes made:
105
110
106
-
- The `__static` global variable is added. It provides a path to your public directory in both development and production. Use this to read/write files in your app's public directory.
107
-
- In production, the `process.env.BASE_URL` is replaced with the path to your app's files.
111
+
- The `__static` global variable is added. It provides a path to your public directory in both development and production. Use this to read/write files in your app's public directory.
112
+
- In production, the `process.env.BASE_URL` is replaced with the path to your app's files.
108
113
109
114
**Note: `__static` is not available in regular build/serve. It should only be used in electron to read/write files on disk. To import a file (img, script, etc...) and not have it be transpiled by webpack, use the `process.env.BASE_URL` instead.**
115
+
110
116
#### Main process (background.js):
117
+
111
118
The main process won't have access to `process.env.BASE_URL` or `src/assets`. However, you can still use `__static` to get a path to your public directory in development and production.
119
+
112
120
#### Examples:
121
+
113
122
```html
114
123
<!-- Renderer process only -->
115
124
<!-- This image will be processed by webpack and placed under img/ -->
@@ -133,8 +142,10 @@ console.log(fileContents)
133
142
```
134
143
135
144
### Changing the output directory:
145
+
136
146
If you don't want your files outputted into dist_electron, you can choose a custom folder in vue-cli-plugin-electron-builder's plugin options.
137
147
**Note: after changing this, you MUST update the main field of your `package.json` to `[new dir]/bundled/background.js`. It is also recommended to add the new directory to your .gitignore file.**
148
+
138
149
```javascript
139
150
// vue.config.js
140
151
@@ -144,13 +155,15 @@ module.exports = {
144
155
outputDir:'electron-builder-output-dir'
145
156
}
146
157
}
147
-
};
158
+
}
148
159
```
160
+
149
161
### Cli Options:
150
162
151
163
Arguments passed to `build:electron` are sent to electron-builder. To see available cli options, see [electron-builder's cli options](https://www.electron.build/cli). `serve:electron` takes no arguments.
152
164
153
165
### TypeScript Support:
166
+
154
167
Typescript support is automatic and requires no configuration, just add the `@vue/typescript` cli plugin. There are a few options for configuring typescript if necessary:
155
168
156
169
```javascript
@@ -159,26 +172,30 @@ Typescript support is automatic and requires no configuration, just add the `@vu
159
172
module.exports= {
160
173
pluginOptions: {
161
174
electronBuilder: {
162
-
// option: default // description
175
+
// option: default // description
163
176
disableMainProcessTypescript:false, // Manually disable typescript plugin for main process. Enable if you want to use regular js for the main process (src/background.js by default).
164
177
mainProcessTypeChecking:false// Manually enable type checking during webpck bundling for background file.
165
178
}
166
179
}
167
-
};
180
+
}
168
181
```
169
-
You may also want to set `mainWindow`'s type to `any` and change `process.env.WEBPACK_DEV_SERVER_URL` to `process.env.WEBPACK_DEV_SERVER_URL as string` to fix type errors.
182
+
183
+
If you are adding typescript before vue-cli-plugin-electron-builder, you may also want to set `mainWindow`'s type to `any` and change `process.env.WEBPACK_DEV_SERVER_URL` to `process.env.WEBPACK_DEV_SERVER_URL as string` to fix type errors. If you add typescript first, this will be done automatically.
170
184
171
185
## How it works:
186
+
172
187
### Build command:
188
+
173
189
The build command consists of three main phases: render build, main build, and electron-builder build:
174
190
175
-
1. Render build: This phase calls `vue-cli-service build` with some custom configuration so it works properly with electron. (The render process is your standard app.)
176
-
2. Main build: This phase is where vue-cli-plugin-electron-builder bundles your background file for the main process (`src/background.js`).
177
-
3. Electron-builder build: This phase uses [electron-builder](https://www.electron.build) to turn your web app code into an desktop app powered by [Electron](https://electronjs.org).
191
+
1. Render build: This phase calls `vue-cli-service build` with some custom configuration so it works properly with electron. (The render process is your standard app.)
192
+
2. Main build: This phase is where vue-cli-plugin-electron-builder bundles your background file for the main process (`src/background.js`).
193
+
3. Electron-builder build: This phase uses [electron-builder](https://www.electron.build) to turn your web app code into an desktop app powered by [Electron](https://electronjs.org).
178
194
179
195
### Serve command:
196
+
180
197
The serve command also consists of 3 main phases: main build, dev server launch, and electron launch:
181
198
182
-
1. Main build: This phase, like in the build command, bundles your app's main process, but in development mode.
183
-
2. Dev server launch: This phase starts the built in dev server with a few modifications to work properly with electron.
184
-
3. Electron launch: This phase launches electron and tells it to load the url of the above dev server.
199
+
1. Main build: This phase, like in the build command, bundles your app's main process, but in development mode.
200
+
2. Dev server launch: This phase starts the built in dev server with a few modifications to work properly with electron.
201
+
3. Electron launch: This phase launches electron and tells it to load the url of the above dev server.
0 commit comments