Skip to content

Commit 23ff11b

Browse files
committed
feat(build/serve): add support for env vars in main process, closes #83
1 parent 83229ec commit 23ff11b

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

__tests__/commands.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ describe('build:electron', () => {
249249
expect(serviceRun).not.toBeCalled()
250250
expect(webpack).not.toBeCalled()
251251
})
252+
253+
test('Env vars prefixed with VUE_APP_ are available in main process config', async () => {
254+
process.env.VUE_APP_TEST = 'expected'
255+
await runCommand('serve:electron')
256+
const mainConfig = webpack.mock.calls[0][0]
257+
// Env var is set correctly
258+
expect(mainConfig.plugins[1].defaultValues.VUE_APP_TEST).toBe('expected')
259+
})
252260
})
253261

254262
describe('serve:electron', () => {
@@ -503,6 +511,14 @@ describe('Custom webpack chain', () => {
503511
'chainableConfig'
504512
)
505513
})
514+
515+
test('Env vars prefixed with VUE_APP_ are available in main process config', async () => {
516+
process.env.VUE_APP_TEST = 'expected'
517+
await runCommand('serve:electron')
518+
const mainConfig = webpack.mock.calls[0][0]
519+
// Env var is set correctly
520+
expect(mainConfig.plugins[1].defaultValues.VUE_APP_TEST).toBe('expected')
521+
})
506522
})
507523

508524
describe('testWithSpectron', async () => {

docs/guide/guide.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ console.log(fileContents)
9595
├── ...
9696
```
9797

98+
## Env Variables
99+
100+
Read [Vue ClI's documentation](https://cli.vuejs.org/guide/mode-and-env.html) to learn about using environment variables in your app. All env variables prefixed with `VUE_APP_` will be available in both the main and renderer processes.
101+
98102
## How it works
99103

100104
### Build Command

index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,19 @@ function bundleMain ({
342342
__static: JSON.stringify(api.resolve('./public'))
343343
}
344344
])
345-
config.plugin('env').use(webpack.EnvironmentPlugin, [
346-
{
347-
// Dev server url
348-
WEBPACK_DEV_SERVER_URL: server.url,
349-
// Path to node_modules (for externals in development)
350-
NODE_MODULES_PATH: api.resolve('./node_modules')
345+
const envVars = {
346+
// Dev server url
347+
WEBPACK_DEV_SERVER_URL: server.url,
348+
// Path to node_modules (for externals in development)
349+
NODE_MODULES_PATH: api.resolve('./node_modules')
350+
}
351+
// Add all env vars prefixed with VUE_APP_
352+
Object.keys(process.env).forEach(k => {
353+
if (/^VUE_APP_/.test(k)) {
354+
envVars[k] = process.env[k]
351355
}
352-
])
356+
})
357+
config.plugin('env').use(webpack.EnvironmentPlugin, [envVars])
353358
}
354359
if (args.debug) {
355360
// Enable source maps for debugging

0 commit comments

Comments
 (0)