Skip to content

Commit 28a3c32

Browse files
committed
add support for custom electron-only webpack config, fixes #5
1 parent 7a768c2 commit 28a3c32

File tree

3 files changed

+91
-27
lines changed

3 files changed

+91
-27
lines changed

README.md

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,79 @@ A Vue Cli 3 plugin for Electron with no required configuration that uses [Electr
66

77
## Quick Start:
88

9-
Open a terminal in the directory of your app created with Vue-CLI 3
9+
Open a terminal in the directory of your app created with Vue-CLI 3.
10+
1011
Then, install and invoke the generator of vue-cli-plugin-electron-builder by running:
1112

12-
vue add electron-builder
13+
vue add electron-builder
1314

1415
That's It! Your ready to go!
1516

1617
### To start a development server:
1718

1819
If you use [Yarn](https://yarnpkg.com/en/) (strongly recommended):
1920

20-
yarn serve:electron
21+
yarn serve:electron
2122

2223
or if you use NPM:
2324

24-
npm run serve:electron
25+
npm run serve:electron
2526

2627
### To build your app:
2728

2829
With yarn:
2930

30-
yarn build:electron
31+
yarn build:electron
3132

3233
or with NPM:
3334

34-
npm run build:electron
35+
npm run build:electron
3536

3637
### Folder Structure:
3738

3839
```
39-
├── dist/ # where electron-webpack outputs compiled files (this will overwrite normal build files)
40-
│ └── ...
40+
├── dist/ # where electron-webpack outputs compiled files (this will overwrite normal build files)
41+
42+
│ └── ...
43+
4144
├── dist_electron/
42-
│ ├── [target platform]-unpacked # unpacked Electron app (main exe and supporting files)
43-
| ├── [application name] setup [version].[target binary (exe|dmg|rpm...)] # installer for Electron app
44-
│ └── ...
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+
4552
├── src/
46-
| ├─── main/
47-
| | └── [main|index].[js|ts] # Electron entry file (for Electron's main process)
48-
| ├── [main|index].[js|ts] # your app's entry file (for Electron's render process)
49-
│ └── ...
50-
├── electron-builder.[json|yml] # electron-builder configuration options (can also be placed in package.json under the "build" key)
51-
├── electron-webpack.[json|yml] # electron-webpack configuration options (can also be placed in package.json under the "electronWebpack" key)
52-
├── package.json # your app's package.json file
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+
5368
├── ...
5469
```
5570

5671
## Further Configuration:
5772

58-
Initial configuration is already set for you in your app's package.json, but it you want to modify it:
73+
Initial configuration is already set for you in your app's package.json, but if you want to modify it:
5974

6075
### CLI Options:
6176

6277
When building your app, any arguments will be passed to electron-builder. To pass an argument/arguments to electron-webpack, place them after --webpack.
6378

6479
**Example:**
6580

66-
yarn build:electron [electron-builder options] --webpack [electron-webpack options]
81+
yarn build:electron [electron-builder options] --webpack [electron-webpack options]
6782

6883
### Electron Builder:
6984

@@ -73,7 +88,6 @@ As per Electron Builder's documentation, they can be applied:
7388

7489
> * in the `package.json` file of your project using the `build` key on the top level:
7590
>
76-
>
7791
> ```
7892
> "build": {
7993
> "appId": "com.example.app"
@@ -82,7 +96,6 @@ As per Electron Builder's documentation, they can be applied:
8296
>
8397
> * 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))).
8498
>
85-
>
8699
> ```
87100
> appId: "com.example.app"
88101
> ```
@@ -97,7 +110,27 @@ To see avalible options, check out [Electron Webpack Configuration Options](http
97110
98111
As per Electron Webpack's documentation, they can be applied:
99112
100-
> Configurations can be applied in `package.json` at `electronWebpack` or in a separate `electron-webpack.(json|json5|yml)`.
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+
```
101134
102135
#### Adding TypeScript Support:
103136

generator/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module.exports = (api, opts) => {
1818
devDependencies,
1919
electronWebpack: {
2020
renderer: {
21-
sourceDirectory: 'src'
21+
sourceDirectory: 'src',
22+
webpackConfig: 'dist_electron/webpack.renderer.additions.js'
2223
}
2324
},
2425
build: {

index.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = api => {
1+
module.exports = (api, options) => {
22
api.registerCommand(
33
'build:electron',
44
{
@@ -12,7 +12,7 @@ module.exports = api => {
1212
},
1313
(args, rawArgs) => {
1414
api.setMode('production')
15-
15+
setWebpackOptions(api, options)
1616
const execa = require('execa')
1717
const electronWebpackPath =
1818
api.resolve('.') + '/node_modules/.bin/electron-webpack'
@@ -68,7 +68,7 @@ module.exports = api => {
6868
},
6969
() => {
7070
api.setMode('dev')
71-
71+
setWebpackOptions(api, options)
7272
const execa = require('execa')
7373
const electronWebpackPath =
7474
api.resolve('.') + '/node_modules/.bin/electron-webpack'
@@ -91,3 +91,33 @@ module.exports = api => {
9191
}
9292
)
9393
}
94+
function setWebpackOptions (api, options) {
95+
const fs = require('fs')
96+
let config
97+
if (
98+
options.pluginOptions &&
99+
options.pluginOptions.electronBuilder &&
100+
options.pluginOptions.electronBuilder.webpackConfig
101+
) {
102+
config = options.pluginOptions.electronBuilder.webpackConfig
103+
} else {
104+
config = {}
105+
}
106+
alias = config.resolve
107+
? config.resolve.alias
108+
? config.resolve.alias
109+
: {}
110+
: {}
111+
if (!config.resolve) config.resolve = {}
112+
config.resolve.alias = {
113+
...alias,
114+
...api.resolveWebpackConfig().resolve.alias
115+
}
116+
if (!fs.existsSync(api.resolve('.') + '/dist_electron')) {
117+
fs.mkdirSync(api.resolve('.') + '/dist_electron')
118+
}
119+
fs.writeFileSync(
120+
api.resolve('.') + '/dist_electron/webpack.renderer.additions.js',
121+
'module.exports=' + JSON.stringify(config)
122+
)
123+
}

0 commit comments

Comments
 (0)