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: match next.js env file loading behavior in bin scripts & importConfig, clean up installed packages & mismatching package versions #6601

Merged
merged 29 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
184fbb2
fix: ensure .env.local files are automatically loaded in bin scripts …
AlessioGr Jun 3, 2024
36ed705
use next's algorithm for loading .env files
AlessioGr Jun 3, 2024
d5ca63a
fix commonjs bs
AlessioGr Jun 3, 2024
ac80b8c
chore: upgrade findUp package
AlessioGr Jun 3, 2024
14bd4c9
fix
AlessioGr Jun 3, 2024
e456884
remove useless webpack devDependencies from payload
AlessioGr Jun 3, 2024
7d6a0da
get rid of nodemon
AlessioGr Jun 3, 2024
428feec
remove useless devdeps
AlessioGr Jun 3, 2024
fdecf1c
remove incorrect @types/probe-image-size dependency (not devDependency)
AlessioGr Jun 3, 2024
b22ec5c
add missing @types/uuid package
AlessioGr Jun 3, 2024
480b26a
matching @types/react-datepicker version
AlessioGr Jun 3, 2024
7c431d9
huge devDependency cleanup
AlessioGr Jun 3, 2024
c7124cd
cleanup next package
AlessioGr Jun 3, 2024
fcd9eeb
matching @types/uuid versions
AlessioGr Jun 3, 2024
516db1c
matching uuid versions
AlessioGr Jun 3, 2024
5bdc765
add back @types/nodemailer
AlessioGr Jun 3, 2024
fc3c75e
upgrade scheduler and use-context-selector
AlessioGr Jun 3, 2024
8240f96
fix incorrect date-fns imports, previously hidden by an ancient peerd…
AlessioGr Jun 3, 2024
7422864
add missing sass package and upgrade other sass loader packages
AlessioGr Jun 3, 2024
91e6434
pnpm-lock
AlessioGr Jun 3, 2024
4469607
further cleanup. move devdeps out of root package.json
AlessioGr Jun 3, 2024
db9f0ec
fix jest esm support
AlessioGr Jun 3, 2024
cee5702
fix incorrect __dirname usage
AlessioGr Jun 3, 2024
3e3c32f
remove another __dirname
AlessioGr Jun 3, 2024
d12c216
fix types
AlessioGr Jun 3, 2024
7eb8fc5
fix unit tests jest
AlessioGr Jun 3, 2024
220bb7c
remove useless package again
AlessioGr Jun 3, 2024
c27a334
fix sass dependency issue
AlessioGr Jun 3, 2024
e162fe6
attempt to unflake lexical e2e test again
AlessioGr Jun 3, 2024
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"concat-stream": "^2.0.0",
"copyfiles": "2.4.1",
"cross-env": "7.0.3",
"dotenv": "8.6.0",
"dotenv": "16.4.5",
"drizzle-kit": "0.20.14-1f2c838",
"drizzle-orm": "0.29.4",
"escape-html": "^1.0.3",
Expand Down
6 changes: 2 additions & 4 deletions packages/payload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"pretest": "pnpm build"
},
"dependencies": {
"@next/env": "^15.0.0-rc.0",
"@payloadcms/translations": "workspace:*",
"@swc-node/core": "^1.13.0",
"@swc-node/sourcemap-support": "^0.5.0",
Expand All @@ -92,9 +93,8 @@
"console-table-printer": "2.11.2",
"dataloader": "2.2.2",
"deepmerge": "4.3.1",
"dotenv": "8.6.0",
"file-type": "16.5.4",
"find-up": "4.1.0",
"find-up": "7.0.0",
"get-tsconfig": "^4.7.2",
"http-status": "1.6.2",
"image-size": "^1.1.1",
Expand Down Expand Up @@ -141,11 +141,9 @@
"confusing-browser-globals": "1.0.11",
"copyfiles": "2.4.1",
"cross-env": "7.0.3",
"file-loader": "6.2.0",
"form-data": "3.0.1",
"get-port": "5.1.1",
"graphql-http": "^1.22.0",
"mini-css-extract-plugin": "1.6.2",
"nodemon": "3.0.3",
"object.assign": "4.1.4",
"object.entries": "1.1.6",
Expand Down
35 changes: 19 additions & 16 deletions packages/payload/src/bin/loadEnv.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import dotenv from 'dotenv'
import findUp from 'find-up'
import fs from 'fs'
import path from 'path'
import nextEnvImport from '@next/env'
const { loadEnvConfig } = nextEnvImport
import { findUpStop, findUpSync } from 'find-up'

/**
* Try to find user's .env and load it
* Try to find user's env files and load it. Uses the same algorithm next.js uses to parse env files, meaning this also supports .env.local, .env.development, .env.production, etc.
*/
export function loadEnv() {
const envPath = findUp.sync('.env')
export function loadEnv(path?: string) {
if (path?.length) {
loadEnvConfig(path, true)
return
}

const { loadedEnvFiles } = loadEnvConfig(process.cwd(), true) // assuming this won't run in production

if (envPath) {
dotenv.config({ path: envPath })
} else {
const cwdPath = path.resolve(process.cwd(), '.env')
if (fs.existsSync(cwdPath)) {
dotenv.config({
path: cwdPath,
})
}
if (!loadedEnvFiles?.length) {
// use findUp to find the env file. So, run loadEnvConfig for every directory upwards
findUpSync((dir) => {
const { loadedEnvFiles } = loadEnvConfig(dir, true)
if (loadedEnvFiles?.length) {
return findUpStop
}
})
}
}
14 changes: 7 additions & 7 deletions packages/payload/src/config/find.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import findUp from 'find-up'
import { findUpSync, pathExistsSync } from 'find-up'
import fs from 'fs'
import path from 'path'

Expand All @@ -13,7 +13,7 @@ const getTSConfigPaths = (): {
rootPath?: string
srcPath?: string
} => {
const tsConfigPath = findUp.sync('tsconfig.json')
const tsConfigPath = findUpSync('tsconfig.json')

if (!tsConfigPath) {
return {
Expand Down Expand Up @@ -79,17 +79,17 @@ export const findConfig = (): string => {
for (const searchPath of searchPaths) {
if (!searchPath) continue

const configPath = findUp.sync(
const configPath = findUpSync(
(dir) => {
const tsPath = path.join(dir, 'payload.config.ts')
const hasTS = findUp.sync.exists(tsPath)
const hasTS = pathExistsSync(tsPath)

if (hasTS) {
return tsPath
}

const jsPath = path.join(dir, 'payload.config.js')
const hasJS = findUp.sync.exists(jsPath)
const hasJS = pathExistsSync(jsPath)

if (hasJS) {
return jsPath
Expand All @@ -108,13 +108,13 @@ export const findConfig = (): string => {
// If no config file is found in the directories defined by tsconfig.json,
// try searching in the 'src' and 'dist' directory as a last resort, as they are most commonly used
if (process.env.NODE_ENV === 'production') {
const distConfigPath = findUp.sync(['payload.config.js', 'payload.config.ts'], {
const distConfigPath = findUpSync(['payload.config.js', 'payload.config.ts'], {
cwd: path.resolve(process.cwd(), 'dist'),
})

if (distConfigPath) return distConfigPath
} else {
const srcConfigPath = findUp.sync(['payload.config.js', 'payload.config.ts'], {
const srcConfigPath = findUpSync(['payload.config.js', 'payload.config.ts'], {
cwd: path.resolve(process.cwd(), 'src'),
})

Expand Down
3 changes: 3 additions & 0 deletions packages/payload/src/utilities/importWithoutClientFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from 'path'

import type { SanitizedConfig } from '../config/types.js'

import { loadEnv } from '../bin/loadEnv.js'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

Expand All @@ -20,6 +22,7 @@ export const importWithoutClientFiles = async <T = unknown>(filePath: string) =>
* Resolve and load Payload config from either a relative or absolute path
*/
export const importConfig = async (configPath: string) => {
loadEnv() // loadConfig would usually be run outside of next. This means they will not get next's automatic env loading here. In order to not force them to install dotenv and set it up manually, we can load the env for them here
const isAbsolutePath = path.isAbsolute(configPath)
if (isAbsolutePath) {
const config = await importWithoutClientFiles<{ default: Promise<SanitizedConfig> }>(configPath)
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/utilities/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync } from 'child_process'
import ciInfo from 'ci-info'
import ConfImport from 'conf'
import { randomBytes } from 'crypto'
import findUp from 'find-up'
import { findUp } from 'find-up'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-sentry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@types/react-dom": "npm:[email protected]",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"dotenv": "^8.2.0",
"jest": "^29.5.0",
"nodemon": "3.0.3",
"payload": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/translations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/react": "npm:[email protected]",
"@types/react-dom": "npm:[email protected]",
"date-fns": "3.3.1",
"dotenv": "8.6.0",
"dotenv": "16.4.5",
"prettier": "^3.0.3",
"typescript": "5.4.5"
},
Expand Down
Loading
Loading