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

feat(env): add .local variants to defaultEnvFiles (#1604) #4871

Merged
merged 4 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/netlify-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Netlify Dev is meant to work with zero config for the majority of users, by usin
jwtSecret = "secret" # The secret used to verify tokens for JWT based redirects
jwtRolePath = "app_metadata.authorization.roles" # Object path we should look for role values for JWT based redirects
autoLaunch = true # a Boolean value that determines if Netlify Dev launches the local server address in your browser
envFiles = [".env.development", ".env"] # The env files to use, ordered by priority (left - highest, right - lowest)
envFiles = [".env.development.local", ".env.local", ".env.development", ".env"] # The env files to use, ordered by priority (left - highest, right - lowest)
# to start an https server instead of an http one, configure a certificate and key files
[dev.https]
certFile = "cert.pem" # path to the certificate file
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dot-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
}

// in the user configuration, the order is highest to lowest
const defaultEnvFiles = ['.env.development', '.env']
const defaultEnvFiles = ['.env.development.local', '.env.local', '.env.development', '.env']

const tryLoadDotEnvFiles = async ({ projectDir, dotenvFiles = defaultEnvFiles }) => {
const results = await Promise.all(
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/utils/dot-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,35 @@ test('should read env vars from .env.development file', async (t) => {
})
})

test('should read from both .env.development and .env', async (t) => {
test('should read env vars from .env.local file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env.local',
env: { TEST: 'FROM_LOCAL_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env.local', env: { TEST: 'FROM_LOCAL_ENV' } }])
})
})

test('should read env vars from .env.development.local file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env.development.local',
env: { TEST: 'FROM_LOCAL_DEVELOPMENT_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env.development.local', env: { TEST: 'FROM_LOCAL_DEVELOPMENT_ENV' } }])
})
})

test('should read env vars from all four .env[.development][.local] files', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder
Expand All @@ -54,12 +82,25 @@ test('should read from both .env.development and .env', async (t) => {
path: '.env.development',
env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' },
})
.withEnvFile({
path: '.env.local',
env: { ONE: 'FROM_LOCAL_ENV', FOUR: 'FROM_LOCAL_ENV' },
})
.withEnvFile({
path: '.env.development.local',
env: { ONE: 'FROM_LOCAL_DEVELOPMENT_ENV', FIVE: 'FROM_LOCAL_DEVELOPMENT_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [
{ file: '.env', env: { ONE: 'FROM_ENV', TWO: 'FROM_ENV' } },
{ file: '.env.development', env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' } },
{ file: '.env.local', env: { ONE: 'FROM_LOCAL_ENV', FOUR: 'FROM_LOCAL_ENV' } },
{
file: '.env.development.local',
env: { ONE: 'FROM_LOCAL_DEVELOPMENT_ENV', FIVE: 'FROM_LOCAL_DEVELOPMENT_ENV' },
},
])
})
})
Expand Down