Skip to content

Commit c068159

Browse files
committed
feat(build/serve): Remove need for custom vue-cli-service
1 parent 7d9eab9 commit c068159

File tree

7 files changed

+798
-518
lines changed

7 files changed

+798
-518
lines changed

__tests__/commands.spec.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ const testWithSpectron = pluginIndex.testWithSpectron
33
const Config = require('webpack-chain')
44
const webpack = require('webpack')
55
const builder = require('electron-builder')
6-
const buildRenderer = require('@vue/cli-service/lib/commands/build').build
7-
const serve = require('@vue/cli-service/lib/commands/serve').serve
86
const fs = require('fs-extra')
97
const execa = require('execa')
108
const portfinder = require('portfinder')
119
const Application = require('spectron').Application
12-
// const {command} = require('yargs')
1310
const mockYargsParse = jest.fn()
1411
const mockYargsCommand = jest.fn(() => ({ parse: mockYargsParse }))
1512
jest.mock('yargs', () => ({ command: mockYargsCommand }))
@@ -34,6 +31,13 @@ jest.mock('spectron', () => ({
3431
client: { waitUntilWindowLoaded: mockWait }
3532
}))
3633
}))
34+
const chainWebpack = jest.fn()
35+
const serviceRun = jest.fn(
36+
() =>
37+
new Promise(resolve => {
38+
resolve({ url: 'serveUrl' })
39+
})
40+
)
3741
console.log = jest.fn()
3842

3943
beforeEach(() => {
@@ -54,7 +58,11 @@ const runCommand = async (command, options = {}, args = {}, rawArgs = []) => {
5458
registerCommand: jest.fn().mockImplementation((name, options, command) => {
5559
commands[name] = command
5660
}),
57-
resolve: jest.fn(path => 'projectPath/' + path)
61+
resolve: jest.fn(path => 'projectPath/' + path),
62+
chainWebpack,
63+
service: {
64+
run: serviceRun
65+
}
5866
}
5967
pluginIndex(api, options)
6068

@@ -110,8 +118,8 @@ describe('build:electron', () => {
110118
const mainConfig = webpack.mock.calls[0][0]
111119
// Main config output is correct
112120
expect(mainConfig.output.path).toBe('projectPath/output/bundled')
113-
// Render build output is correct
114-
expect(buildRenderer.mock.calls[0][0].dest).toBe('output/bundled')
121+
122+
expect(serviceRun.mock.calls[0][1].dest).toBe('output/bundled')
115123
// Electron-builder output is correct
116124
expect(builder.build.mock.calls[0][0].config.directories.output).toBe(
117125
'output'
@@ -145,12 +153,13 @@ describe('build:electron', () => {
145153
}
146154
}
147155
})
148-
const rendererConfig = buildRenderer.mock.calls[0][3].toConfig()
156+
const config = new Config()
157+
chainWebpack.mock.calls[0][0](config)
149158
// Custom node key is passed through
150-
expect(rendererConfig.node.test).toBe('expected')
159+
expect(config.toConfig().node.test).toBe('expected')
151160
})
152161

153-
test('Custom main process webpack config is used if provided', async () => {
162+
test('Custom Electron Builder config is used if provided', async () => {
154163
await runCommand('build:electron', {
155164
pluginOptions: {
156165
electronBuilder: {
@@ -304,9 +313,10 @@ describe('serve:electron', () => {
304313
}
305314
}
306315
})
307-
const rendererConfig = serve.mock.calls[0][3].toConfig()
316+
const config = new Config()
317+
chainWebpack.mock.calls[0][0](config)
308318
// Custom node key is passed through
309-
expect(rendererConfig.node.test).toBe('expected')
319+
expect(config.toConfig().node.test).toBe('expected')
310320
})
311321

312322
test('If --debug argument is passed, electron is not launched, main process is not minified, and source maps are enabled', async () => {

docs/guide/configuration.md

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ They can be placed under the `builderOptions` key in vue-cli-plugin-electron-bui
1414
// vue.config.js
1515

1616
module.exports = {
17-
pluginOptions: {
18-
electronBuilder: {
19-
builderOptions: {
20-
// options placed here will be merged with default configuration and passed to electron-builder
21-
}
22-
}
17+
pluginOptions: {
18+
electronBuilder: {
19+
builderOptions: {
20+
// options placed here will be merged with default configuration and passed to electron-builder
21+
}
2322
}
23+
}
2424
}
2525
```
2626

@@ -30,39 +30,37 @@ All CLI arguments passed to `build:electron` will be forwarded to electron-build
3030

3131
## Webpack configuration
3232

33-
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`. Use `chainWebpackRendererProcess` customize your app's webpack config for Electron builds only. 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.
33+
Your regular config is extended and used for bundling the renderer process (your app). To modify your webpack config for Electron builds only, use the `chainWebpackRendererProcess` function. To modify the webpack config for the [Electron main process](https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes) only, use the `chainWebpackMainProcess` function under VCP 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). These functions work similarly to the [`chainWebpack`](https://cli.vuejs.org/config/#chainwebpack) option provided by Vue CLI.
3434

3535
**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.**
3636

3737
```javascript
3838
// vue.config.js
3939

4040
module.exports = {
41-
configureWebpack: {
42-
// Configuration applied to all builds
43-
},
44-
pluginOptions: {
45-
electronBuilder: {
46-
chainWebpackMainProcess: config => {
47-
// Chain webpack config for electron main process only
48-
},
49-
chainWebpackRendererProcess: config => {
50-
// Chain webpack config for electron renderer process only
51-
// The following example will set IS_ELECTRON to true in your app
52-
config.plugin('define').tap(args => {
53-
args[0]['IS_ELECTRON'] = true
54-
return args
55-
})
56-
// If you do not return the config property, your app may break!
57-
return config
58-
},
59-
// Use this to change the entrypoint of your app's main process
60-
mainProcessFile: 'src/myBackgroundFile.js',
61-
// Provide an array of files that, when changed, will recompile the main process and restart Electron
62-
// Your main process file will be added by default
63-
mainProcessWatch: ['src/myFile1', 'src/myFile2']
64-
}
41+
configureWebpack: {
42+
// Configuration applied to all builds
43+
},
44+
pluginOptions: {
45+
electronBuilder: {
46+
chainWebpackMainProcess: config => {
47+
// Chain webpack config for electron main process only
48+
},
49+
chainWebpackRendererProcess: config => {
50+
// Chain webpack config for electron renderer process only
51+
// The following example will set IS_ELECTRON to true in your app
52+
config.plugin('define').tap(args => {
53+
args[0]['IS_ELECTRON'] = true
54+
return args
55+
})
56+
},
57+
// Use this to change the entrypoint of your app's main process
58+
mainProcessFile: 'src/myBackgroundFile.js',
59+
// Provide an array of files that, when changed, will recompile the main process and restart Electron
60+
// Your main process file will be added by default
61+
mainProcessWatch: ['src/myFile1', 'src/myFile2']
6562
}
63+
}
6664
}
6765
```
6866

@@ -76,11 +74,11 @@ If you don't want your files outputted into dist_electron, you can choose a cust
7674
// vue.config.js
7775

7876
module.exports = {
79-
pluginOptions: {
80-
electronBuilder: {
81-
outputDir: 'electron-builder-output-dir'
82-
}
77+
pluginOptions: {
78+
electronBuilder: {
79+
outputDir: 'electron-builder-output-dir'
8380
}
81+
}
8482
}
8583
```
8684

@@ -92,13 +90,13 @@ Typescript support is automatic and requires no configuration, just add the `@vu
9290
// vue.config.js
9391

9492
module.exports = {
95-
pluginOptions: {
96-
electronBuilder: {
97-
// option: default // description
98-
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).
99-
mainProcessTypeChecking: false // Manually enable type checking during webpck bundling for background file.
100-
}
93+
pluginOptions: {
94+
electronBuilder: {
95+
// option: default // description
96+
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).
97+
mainProcessTypeChecking: false // Manually enable type checking during webpck bundling for background file.
10198
}
99+
}
102100
}
103101
```
104102

docs/guide/guide.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ sidebarDepth: 2
1010

1111
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:
1212

13-
- 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.
14-
- In production, the `process.env.BASE_URL` is replaced with the path to your app's files.
13+
- 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.
14+
- In production, the `process.env.BASE_URL` is replaced with the path to your app's files.
1515

1616
**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.**
1717

@@ -61,6 +61,30 @@ console.log(fileContents)
6161
├── ...
6262
```
6363

64+
## Using Native Dependencies
65+
66+
To use native deps in Electron, you will need to find the path to the main file (`.node`) relative to `node_modules` for that dep. Once you have found it, set `vue.config.js` (replace `YOUR_DEP` with the name of the native dep and `YOUR_DEP_PATH` with the path to the main file) to:
67+
68+
```javascript
69+
module.exports = {
70+
pluginOptions: {
71+
electronBuilder: {
72+
chainWebpackRendererProcess: config => {
73+
config.module
74+
.rule('node-loader')
75+
.test(/\.node$/)
76+
.use('node')
77+
.loader('node-loader')
78+
config.resolve.alias.set('YOUR_DEP', 'YOUR_DEP_PATH')
79+
return config
80+
}
81+
}
82+
}
83+
}
84+
```
85+
86+
For example, if you were using SQlite3, `YOUR_DEP_PATH` would be `sqlite3/lib/binding/electron-v2.0-YOUR_PLATFORM_HERE-x64/node_sqlite3.node`
87+
6488
## How it works
6589

6690
### Build command

docs/guide/testingAndDebugging.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ vue-cli-plugin-electron-builder exports a `testWithSpectron` function. This func
3434
const { testWithSpectron } = require('vue-cli-plugin-electron-builder')
3535

3636
test('a window is created', async () => {
37-
const { stdout, url, stopServe, app } = await testWithSpectron()
38-
// stdout is the log of serve:electron
39-
console.log(`serve:electron returned: ${stdout}`)
40-
// url is the url for the dev server created with serve:electron
41-
console.log(`the dev server url is: ${url}`)
42-
// app is a spectron instance. It is attached to the dev server, launched, and waited for to load.
43-
expect(await app.client.getWindowCount()).toBe(1)
44-
// Before your tests end, make sure to stop the dev server and spectron
45-
await stopServe()
37+
const { stdout, url, stopServe, app } = await testWithSpectron()
38+
// stdout is the log of serve:electron
39+
console.log(`serve:electron returned: ${stdout}`)
40+
// url is the url for the dev server created with serve:electron
41+
console.log(`the dev server url is: ${url}`)
42+
// app is a spectron instance. It is attached to the dev server, launched, and waited for to load.
43+
expect(await app.client.getWindowCount()).toBe(1)
44+
// Before your tests end, make sure to stop the dev server and spectron
45+
await stopServe()
4646
})
4747
```
4848

0 commit comments

Comments
 (0)