From eb00be8fd432ea08b8e5811db32de856e5b8cca5 Mon Sep 17 00:00:00 2001 From: Dan Dombrowski Date: Thu, 3 Sep 2020 01:08:04 -0400 Subject: [PATCH] fix: Order of dotenv files loaded (#97) Syncing fix from here https://github.com/facebook/create-react-app/pull/9037 --- .releaserc | 1 + CHANGELOG.md | 4 ++-- README.md | 2 +- src/main.ts | 4 ++-- test/main.test.ts | 14 +++++++------- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.releaserc b/.releaserc index 25f2e85..3aca3af 100644 --- a/.releaserc +++ b/.releaserc @@ -11,6 +11,7 @@ { "preset": "conventionalcommits", "presetConfig": { + "header": "Changelog", "types": [ { "section": "Features", diff --git a/CHANGELOG.md b/CHANGELOG.md index ddc02f1..a029bbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +# Changelog + ## [2.0.0-beta.1](https://github.com/djdmbrwsk/dotenv-cra/compare/v1.0.0...v2.0.0-beta.1) (2020-09-03) @@ -9,8 +11,6 @@ * Drop support for Node 8 ([#96](https://github.com/djdmbrwsk/dotenv-cra/issues/96)) ([b13d42d](https://github.com/djdmbrwsk/dotenv-cra/commit/b13d42de64bdc948b50b51ca9fa52e769958aa44)) -# Changelog - ## 1.0.0 (2019-12-29) Initial release diff --git a/README.md b/README.md index 40d2f0c..a7449a1 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ PORT=3001 Files on the left have more priority than files on the right: -- `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env` +- `npm start`: `.env.development.local`, `.env.local`, `.env.development`, `.env` - `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing) [CRA Reference](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used) diff --git a/src/main.ts b/src/main.ts index 58bd25d..f14b43e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,9 +2,9 @@ import { existsSync } from 'fs'; import { basename, resolve } from 'path'; import { - config as dotenvConfig, DotenvConfigOptions, DotenvConfigOutput, + config as dotenvConfig, } from 'dotenv'; import dotenvExpand from 'dotenv-expand'; @@ -31,8 +31,8 @@ export function config(options?: DotenvCraOptions): DotenvConfigOutput { // https://github.com/facebook/create-react-app/blob/8b7b819b4b9e6ba457e011e92e33266690e26957/packages/react-scripts/config/env.js#L25-L34 const dotenvFiles = [ `${dotenvPath}.${env}.local`, - `${dotenvPath}.${env}`, process.env.NODE_ENV !== 'test' && `${dotenvPath}.local`, + `${dotenvPath}.${env}`, dotenvPath, ]; diff --git a/test/main.test.ts b/test/main.test.ts index 2c7b73e..f61a5a0 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -3,7 +3,7 @@ import { basename } from 'path'; import dotenv, { DotenvConfigOutput } from 'dotenv'; -import { config, DotenvCraOptions } from '../src/main'; +import { DotenvCraOptions, config } from '../src/main'; const originalNodeEnv = process.env.NODE_ENV; beforeEach(() => { @@ -80,10 +80,10 @@ test('should log files loaded when `debug` supplied in options', () => { '[dotenv-cra][DEBUG] loading `.env.development.local`', ); expect(consoleLogSpy.mock.calls[1][0]).toEqual( - '[dotenv-cra][DEBUG] loading `.env.development`', + '[dotenv-cra][DEBUG] loading `.env.local`', ); expect(consoleLogSpy.mock.calls[2][0]).toEqual( - '[dotenv-cra][DEBUG] loading `.env.local`', + '[dotenv-cra][DEBUG] loading `.env.development`', ); expect(consoleLogSpy.mock.calls[3][0]).toEqual( '[dotenv-cra][DEBUG] loading `.env`', @@ -99,8 +99,8 @@ test('should exclude .env.local when NODE_ENV set to test', () => { .mockImplementation(makeMockDotenvConfig()); config(); expect(dotenvConfigSpy).toBeCalledTimes(3); - let thirdCallPath = dotenvConfigSpy.mock.calls[2][0]?.path; - expect(thirdCallPath?.endsWith('.env.local')).toEqual(false); + let secondCallPath = dotenvConfigSpy.mock.calls[1][0]?.path; + expect(secondCallPath?.endsWith('.env.local')).toEqual(false); process.env.NODE_ENV = 'development'; dotenvConfigSpy.mockRestore(); @@ -109,8 +109,8 @@ test('should exclude .env.local when NODE_ENV set to test', () => { .mockImplementation(makeMockDotenvConfig()); config(); expect(dotenvConfigSpy).toBeCalledTimes(4); - thirdCallPath = dotenvConfigSpy.mock.calls[2][0]?.path; - expect(thirdCallPath?.endsWith('.env.local')).toEqual(true); + secondCallPath = dotenvConfigSpy.mock.calls[1][0]?.path; + expect(secondCallPath?.endsWith('.env.local')).toEqual(true); }); test('should return error objects', () => {