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

read env vars from next.config.js #3128

Merged
merged 1 commit into from
Jan 17, 2023
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
10 changes: 6 additions & 4 deletions crates/next-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ pub async fn env_for_js(
} else {
env
};
let env = EmbeddableProcessEnvVc::new(env).into();
let image_config = next_config.image_config().await?;

let next_config = next_config.await?;
let env =
EmbeddableProcessEnvVc::new(CustomProcessEnvVc::new(env, next_config.env()).into()).into();

let image_config = next_config.image_config().await?;
let mut map = indexmap! {
// We need to overload the __NEXT_IMAGE_OPTS to override the default remotePatterns field.
// This allows us to support loading from remote hostnames until we properly support reading
// the next.config.js file.
"__NEXT_IMAGE_OPTS".to_string() => serde_json::to_string(&image_config).unwrap(),
"__NEXT_IMAGE_OPTS".to_string() => serde_json::to_string(&image_config)?,
};

let next_config = next_config.await?;

if next_config.react_strict_mode.unwrap_or(false) {
map.insert("__NEXT_STRICT_MODE".to_string(), "true".to_string());
}
Expand Down
10 changes: 7 additions & 3 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
Expand All @@ -8,6 +6,7 @@ use turbo_tasks::{
trace::TraceRawVcs,
Value,
};
use turbo_tasks_env::EnvMapVc;
use turbopack::{
evaluate_context::node_evaluate_asset_context,
module_options::{
Expand Down Expand Up @@ -44,7 +43,7 @@ pub struct NextConfig {
pub typescript: Option<TypeScriptConfig>,
pub react_strict_mode: Option<bool>,
pub experimental: Option<ExperimentalConfig>,
pub env: Option<HashMap<String, String>>,
pub env: IndexMap<String, String>,
pub compiler: Option<CompilerConfig>,
pub images: ImageConfig,
pub transpile_packages: Option<Vec<String>>,
Expand Down Expand Up @@ -206,6 +205,11 @@ impl NextConfigVc {
))
}

#[turbo_tasks::function]
pub async fn env(self) -> Result<EnvMapVc> {
Ok(EnvMapVc::cell(self.await?.env.clone()))
}

#[turbo_tasks::function]
pub async fn image_config(self) -> Result<ImageConfigVc> {
Ok(self.await?.images.clone().cell())
Expand Down