Skip to content

Commit d547da1

Browse files
committed
feat(webpackConfig): disable nodeIntegration by default
1 parent 67c55a0 commit d547da1

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

docs/guide/configuration.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ module.exports = {
7373
}
7474
```
7575

76+
## Node Integration
77+
78+
As of v2.0, electron `nodeIntegration` is disabled by default. This blocks all node APIs such as `require`. This reduces [security risks](https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content), and is a recommended best practice by the electron team. If you need to use native modules such as `fs` or `sqlite` in the renderer process, you can enable `nodeIntegration` in `vue.config.js`:
79+
80+
```js
81+
module.exports = {
82+
pluginOptions: {
83+
electronBuilder: {
84+
nodeIntegration: true
85+
}
86+
}
87+
}
88+
```
89+
7690
## Changing the Output Directory
7791

7892
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. If you are using `v1.0.0-rc.4` or later, you can use the `--dest` argument to change the output dir as well.

docs/guide/guide.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ sidebarDepth: 2
88

99
[[toc]]
1010

11-
## Native Modules <Badge text="1.0.0-rc.1+" type="info"/>
11+
## Native Modules
1212

13-
Native modules are supported and should work without any configuration. If you get errors, first make sure VCP-Electron-Builder's version is set to `1.0.0-rc.1` or greater. If it still fails, re-invoke the generator with `vue invoke electron-builder`. The generator will automatically detect missing code (such as native module support) and add it, without interfering with the rest. If you have done both these things, you may need to set the native dependency as an [webpack external](https://webpack.js.org/configuration/externals/). It should get found automatically, but it might not. To do this, use the `externals` option:
13+
Native modules are supported and should work without any configuration, assuming [nodeIntegration is enabled](./configuration.md#node-integration). If you get errors, you may need to set the native dependency as an [webpack external](https://webpack.js.org/configuration/externals/). It should get found automatically, but it might not. To do this, use the `externals` option:
1414

1515
```javascript
1616
// vue.config.js
@@ -31,7 +31,7 @@ module.exports = {
3131

3232
- You can prefix an item in the `externals` array with `!` to prevent it being automatically marked as an external. (`!not-external`)
3333

34-
- If you do not use native dependencies in your code, you can remove the `postinstall` script from your `package.json`. Native modules may not work, but dependency install times will be faster.
34+
- If you do not use native dependencies in your code, you can remove the `postinstall` and `postuninstall` scripts from your `package.json`. Native modules may not work, but dependency install times will be faster.
3535

3636
- Using a database such as MySQL or MongoDB requires extra configuration. See [Issue #76 (comment)](https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/76#issuecomment-420060179) for more info.
3737

generator/templates/base/src/background.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ let win
1818
function createWindow () {
1919
// Create the browser window.
2020
win = new BrowserWindow({ width: 800, height: 600, webPreferences: {
21-
nodeIntegration: true
21+
// Use pluginOptions.nodeIntegration, leave this alone
22+
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/configuration.html#node-integration for more info
23+
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION
2224
} })
2325

2426
if (process.env.WEBPACK_DEV_SERVER_URL) {

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ function bundleMain ({
561561
envVars[k] = process.env[k]
562562
}
563563
})
564+
// Enable/disable nodeIntegration
565+
envVars.ELECTRON_NODE_INTEGRATION = pluginOptions.nodeIntegration || false
564566
config.plugin('env').use(webpack.EnvironmentPlugin, [envVars])
565567

566568
if (args.debug) {

lib/webpackConfig.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const fs = require('fs')
22
const { DefinePlugin } = require('webpack')
33

4-
async function chainWebpack (api, pluginOptions, config) {
4+
async function chainWebpack(api, pluginOptions, config) {
55
const rendererProcessChain =
66
pluginOptions.chainWebpackRendererProcess || (config => config)
77
const realDirname = 'require("electron").remote.app.getAppPath()'
88
if (process.env.IS_ELECTRON) {
9-
// Add externals
10-
config.externals(getExternals(api, pluginOptions))
11-
// Modify webpack config to work properly with electron
12-
config
13-
.target('electron-renderer')
14-
.node.set('__dirname', false)
15-
.set('__filename', false)
9+
if (pluginOptions.nodeIntegration) {
10+
// Add externals
11+
config.externals(getExternals(api, pluginOptions))
12+
// Modify webpack config to work properly with electron
13+
config.target('electron-renderer')
14+
}
15+
config.node.set('__dirname', false).set('__filename', false)
1616
// Set IS_ELECTRON
1717
if (config.plugins.has('define')) {
1818
config.plugin('define').tap(args => {
@@ -76,7 +76,7 @@ async function chainWebpack (api, pluginOptions, config) {
7676
process.env.VUE_APP_NODE_MODULES_PATH = false
7777
}
7878
// Find all the dependencies without a `main` property or with a `binary` property or set by user and add them as webpack externals
79-
function getExternals (api, pluginOptions) {
79+
function getExternals(api, pluginOptions) {
8080
const nodeModulesPath = pluginOptions.nodeModulesPath || './node_modules'
8181
let nodeModulesPaths = []
8282
if (Array.isArray(nodeModulesPath)) {

0 commit comments

Comments
 (0)