Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(env): preserve existing env vars so load in reverse order. #1503

Merged
merged 1 commit into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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