Skip to content

Commit

Permalink
fix(env): preserve existing env vars so load in reverse order. (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop authored and yyx990803 committed Jun 7, 2018
1 parent c1074be commit 7c1ef24
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/@vue/cli-service/__tests__/Service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ beforeEach(() => {
})

test('env loading', () => {
fs.writeFileSync('/.env', `FOO=1\nBAR=2`)
fs.writeFileSync('/.env.local', `FOO=3\nBAZ=4`)
process.env.FOO = 0
fs.writeFileSync('/.env.local', `FOO=1\nBAR=2`)
fs.writeFileSync('/.env', `BAR=3\nBAZ=4`)
createMockService()
expect(process.env.FOO).toBe('3')
expect(process.env.FOO).toBe('0')
expect(process.env.BAR).toBe('2')
expect(process.env.BAZ).toBe('4')
})
Expand Down
6 changes: 3 additions & 3 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ module.exports = class Service {
this.initialized = true
this.mode = mode

// load base .env
this.loadEnv()
// load mode .env
if (mode) {
this.loadEnv(mode)
}
// load base .env
this.loadEnv()

// load user config
const userOptions = this.loadUserOptions()
Expand Down Expand Up @@ -106,8 +106,8 @@ module.exports = class Service {
}
}

load(basePath)
load(localPath)
load(basePath)
}

resolvePlugins (inlinePlugins, useBuiltIn) {
Expand Down
4 changes: 3 additions & 1 deletion packages/@vue/cli-service/lib/util/loadEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const fs = require('fs')
module.exports = function loadEnv (path = '.env') {
const config = parse(fs.readFileSync(path, 'utf-8'))
Object.keys(config).forEach(key => {
process.env[key] = config[key]
if (typeof process.env[key] === 'undefined') {
process.env[key] = config[key]
}
})
return config
}
Expand Down

0 comments on commit 7c1ef24

Please sign in to comment.