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

Expose dotenv loading under separate package #17152

Merged
merged 6 commits into from
Sep 25, 2020
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ packages/next-codemod/transforms/__testfixtures__/**/*
packages/next-codemod/transforms/__tests__/**/*
packages/next-codemod/**/*.js
packages/next-codemod/**/*.d.ts
packages/next-env/**/*.d.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ packages/next-codemod/transforms/__testfixtures__/**/*
packages/next-codemod/transforms/__tests__/**/*
packages/next-codemod/**/*.js
packages/next-codemod/**/*.d.ts
packages/next-env/**/*.d.ts
1 change: 1 addition & 0 deletions packages/next-env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
types
3 changes: 3 additions & 0 deletions packages/next-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@next/env`

Next.js' util for loading dotenv files in with the proper priorities
32 changes: 19 additions & 13 deletions packages/next/lib/load-env-config.ts → packages/next-env/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import path from 'path'
import * as log from '../build/output/log'
import dotenvExpand from 'next/dist/compiled/dotenv-expand'
import dotenv, { DotenvConfigOutput } from 'next/dist/compiled/dotenv'
/* eslint-disable import/no-extraneous-dependencies */
import * as fs from 'fs'
import * as path from 'path'
import * as dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'

export type Env = { [key: string]: string }
export type LoadedEnvFiles = Array<{
Expand All @@ -13,14 +13,19 @@ export type LoadedEnvFiles = Array<{
let combinedEnv: Env | undefined = undefined
let cachedLoadedEnvFiles: LoadedEnvFiles = []

export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {
type Log = {
info: (...args: any[]) => void
error: (...args: any[]) => void
}

export function processEnv(
loadedEnvFiles: LoadedEnvFiles,
dir?: string,
log: Log = console
) {
// don't reload env if we already have since this breaks escaped
// environment values e.g. \$ENV_FILE_KEY
if (
combinedEnv ||
process.env.__NEXT_PROCESSED_ENV ||
!loadedEnvFiles.length
) {
if (process.env.__NEXT_PROCESSED_ENV || loadedEnvFiles.length === 0) {
return process.env as Env
}
// flag that we processed the environment values in case a serverless
Expand All @@ -32,7 +37,7 @@ export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {

for (const envFile of loadedEnvFiles) {
try {
let result: DotenvConfigOutput = {}
let result: dotenv.DotenvConfigOutput = {}
result.parsed = dotenv.parse(envFile.contents)

result = dotenvExpand(result)
Expand Down Expand Up @@ -62,7 +67,8 @@ export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {

export function loadEnvConfig(
dir: string,
dev?: boolean
dev?: boolean,
log: Log = console
): {
combinedEnv: Env
loadedEnvFiles: LoadedEnvFiles
Expand Down
37 changes: 37 additions & 0 deletions packages/next-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@next/env",
"version": "9.5.4-canary.21",
"keywords": [
"react",
"next",
"next.js",
"dotenv"
],
"description": "Next.js dotenv file loading",
"repository": {
"type": "git",
"url": "https://github.com/vercel/next.js",
"directory": "packages/next-env"
},
"author": "Next.js Team <support@vercel.com>",
"license": "MIT",
"main": "dist/index.js",
"types": "types/index.d.ts",
"files": [
"dist",
"types"
],
"scripts": {
"build": "ncc build ./index.ts -w -o dist/",
"prerelease": "rimraf ./dist/",
"types": "tsc index.ts --declaration --emitDeclarationOnly --declarationDir types --esModuleInterop",
"release": "ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
"prepublish": "yarn release && yarn types"
},
"devDependencies": {
"@types/dotenv": "8.2.0",
"@zeit/ncc": "^0.20.4",
"dotenv": "8.2.0",
"dotenv-expand": "5.1.0"
}
}
10 changes: 10 additions & 0 deletions packages/next-env/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node",
"strict": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": false
}
}
2 changes: 1 addition & 1 deletion packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path'
import { warn } from './output/log'
import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { LoadedEnvFiles } from '../lib/load-env-config'
import { LoadedEnvFiles } from '@next/env'

type PagesMapping = {
[page: string]: string
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import loadCustomRoutes, {
Redirect,
RouteType,
} from '../lib/load-custom-routes'
import { loadEnvConfig } from '../lib/load-env-config'
import { loadEnvConfig } from '@next/env'
import { recursiveDelete } from '../lib/recursive-delete'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import {
Expand Down Expand Up @@ -112,7 +112,7 @@ export default async function build(
}

// attempt to load global env values so they are available in next.config.js
const { loadedEnvFiles } = loadEnvConfig(dir)
const { loadedEnvFiles } = loadEnvConfig(dir, false, Log)

const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const { target } = config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const nextServerlessLoader: loader.Loader = function () {
`
: ''
const envLoading = `
const { processEnv } = require('next/dist/lib/load-env-config')
const { processEnv } = require('@next/env')
processEnv(${Buffer.from(loadedEnvFiles, 'base64').toString()})
`

Expand Down
2 changes: 1 addition & 1 deletion packages/next/compiled/amphtml-validator/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/compiled/arg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/next/compiled/async-retry/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading