Skip to content

Commit 261b6d1

Browse files
committed
fix(env): preserve existing env vars so load in reverse order.
Fixes #1499
1 parent 5c8dd98 commit 261b6d1

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

packages/@vue/cli-service/__tests__/Service.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ beforeEach(() => {
2626
})
2727

2828
test('env loading', () => {
29-
fs.writeFileSync('/.env', `FOO=1\nBAR=2`)
30-
fs.writeFileSync('/.env.local', `FOO=3\nBAZ=4`)
29+
process.env.FOO = 0
30+
fs.writeFileSync('/.env.local', `FOO=1\nBAR=2`)
31+
fs.writeFileSync('/.env', `BAR=3\nBAZ=4`)
3132
createMockService()
32-
expect(process.env.FOO).toBe('3')
33+
expect(process.env.FOO).toBe('0')
3334
expect(process.env.BAR).toBe('2')
3435
expect(process.env.BAZ).toBe('4')
3536
})

packages/@vue/cli-service/lib/Service.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ module.exports = class Service {
5353
this.initialized = true
5454
this.mode = mode
5555

56-
// load base .env
57-
this.loadEnv()
5856
// load mode .env
5957
if (mode) {
6058
this.loadEnv(mode)
6159
}
60+
// load base .env
61+
this.loadEnv()
6262

6363
// load user config
6464
const userOptions = this.loadUserOptions()
@@ -106,8 +106,8 @@ module.exports = class Service {
106106
}
107107
}
108108

109-
load(basePath)
110109
load(localPath)
110+
load(basePath)
111111
}
112112

113113
resolvePlugins (inlinePlugins, useBuiltIn) {

packages/@vue/cli-service/lib/util/loadEnv.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const fs = require('fs')
33
module.exports = function loadEnv (path = '.env') {
44
const config = parse(fs.readFileSync(path, 'utf-8'))
55
Object.keys(config).forEach(key => {
6-
process.env[key] = config[key]
6+
if (typeof process.env[key] === 'undefined') {
7+
process.env[key] = config[key]
8+
}
79
})
810
return config
911
}

0 commit comments

Comments
 (0)