Skip to content

Commit d9629e9

Browse files
committed
improve automatic typescript support
1 parent 79a3487 commit d9629e9

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

README.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Vue CLI Plugin Electron Builder
32

43
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:
6463
To see avalible options, check out [Electron Builder Configuration Options](https://www.electron.build/configuration/configuration)
6564

6665
They can be placed under the `builderOptions` key in vue-cli-plugin-electron-builder's plugin options in `vue.config.js`
66+
6767
```javascript
6868
// vue.config.js
6969

70-
module.exports= {
70+
module.exports = {
7171
pluginOptions: {
7272
electronBuilder: {
7373
builderOptions: {
@@ -76,8 +76,10 @@ module.exports= {
7676
}
7777
}
7878
}
79-
```
79+
```
80+
8081
### Webpack configuration:
82+
8183
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.
8284

8385
**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 = {
9193
},
9294
pluginOptions: {
9395
electronBuilder: {
94-
chainWebpackMainProcess: (config) => {
96+
chainWebpackMainProcess: config => {
9597
// Chain webpack config for electron main process only
9698
},
9799
mainProcessFile: 'src/myBackgroundFile.js'
98100
}
99101
}
100-
};
102+
}
101103
```
104+
102105
### Handling static assets:
106+
103107
#### Renderer process (main app):
108+
104109
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:
105110

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.
108113

109114
**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+
110116
#### Main process (background.js):
117+
111118
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+
112120
#### Examples:
121+
113122
```html
114123
<!-- Renderer process only -->
115124
<!-- This image will be processed by webpack and placed under img/ -->
@@ -133,8 +142,10 @@ console.log(fileContents)
133142
```
134143

135144
### Changing the output directory:
145+
136146
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.
137147
**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+
138149
```javascript
139150
// vue.config.js
140151

@@ -144,13 +155,15 @@ module.exports = {
144155
outputDir: 'electron-builder-output-dir'
145156
}
146157
}
147-
};
158+
}
148159
```
160+
149161
### Cli Options:
150162

151163
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.
152164

153165
### TypeScript Support:
166+
154167
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:
155168

156169
```javascript
@@ -159,26 +172,30 @@ Typescript support is automatic and requires no configuration, just add the `@vu
159172
module.exports = {
160173
pluginOptions: {
161174
electronBuilder: {
162-
// option: default // description
175+
// option: default // description
163176
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).
164177
mainProcessTypeChecking: false // Manually enable type checking during webpck bundling for background file.
165178
}
166179
}
167-
};
180+
}
168181
```
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.
170184

171185
## How it works:
186+
172187
### Build command:
188+
173189
The build command consists of three main phases: render build, main build, and electron-builder build:
174190

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).
178194

179195
### Serve command:
196+
180197
The serve command also consists of 3 main phases: main build, dev server launch, and electron launch:
181198

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.

generator/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ module.exports = api => {
1616
// Write updated files
1717
fs.writeFileSync(api.resolve('./public/index.html'), index)
1818
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
19+
if (api.hasPlugin('typescript')) {
20+
let background
21+
if (fs.existsSync(api.resolve('./src/background.js'))) {
22+
background = fs.readFileSync(api.resolve('./src/background.js'), 'utf8')
23+
fs.unlinkSync(api.resolve('./src/background.js'))
24+
} else {
25+
background = fs.readFileSync(api.resolve('./src/background.ts'), 'utf8')
26+
}
27+
background = background.replace(
28+
'process.env.WEBPACK_DEV_SERVER_URL',
29+
'process.env.WEBPACK_DEV_SERVER_URL as string'
30+
)
31+
background = background.replace('let mainWindow', 'let mainWindow: any')
32+
fs.writeFileSync(api.resolve('./src/background.ts'), background)
33+
}
1934
})
2035
api.extendPackage({
2136
scripts: {

generator/template/src/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { app, protocol, BrowserWindow } from 'electron'
44
import * as path from 'path'
55
import { format as formatUrl } from 'url'
6-
import createProtocol from 'vue-cli-plugin-electron-builder/lib/createProtocol'
6+
import createProtocol from 'vue-cli-plugin-electron-builder/lib/createProtocol.js'
77

88
const isDevelopment = process.env.NODE_ENV !== 'production'
99

lib/createProtocol.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function(scheme: string): void

0 commit comments

Comments
 (0)