Skip to content

Commit

Permalink
Implement list of config options for Turbopack (#56188)
Browse files Browse the repository at this point in the history
This adds the define env for listed options below:

- `crossOrigin`
- `devIndicators`
- `output`
- `analyticsId`
- `optimizeFonts`
- `experimental.useDeploymentId`
- `experimental.useDeploymentIdServerActions`
- `experimental.deploymentId`
- `experimental.manualClientBasePath`
- `experimental.optimisticClientCache`
- `experimental.middlewarePrefetch`
- `experimental.optimizeCss`
- `experimental.nextScriptWorkers`
- `experimental.webVitalsAttribution`

For some there might still be missing pieces, for example `crossOrigin` needs something in Turbopack's script injection but most others will "just work" after this PR. 

Added the few ones that did not come directly from config as todos, this PR can be landed without those.
  • Loading branch information
timneutkens authored Sep 28, 2023
1 parent 59ce492 commit 5415811
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 23 deletions.
191 changes: 190 additions & 1 deletion packages/next-swc/crates/next-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ pub async fn env_for_js(

map.insert(
"__NEXT_I18N_SUPPORT".to_string(),
// How do I check if i18n exists in the config?
if next_config.i18n.is_none() {
"false".to_string()
} else {
Expand All @@ -161,6 +160,196 @@ pub async fn env_for_js(
},
);

map.insert(
"NEXT_MINIMAL".to_string(),
// Don't stringify undefined
"\"\"".to_string(),
);

map.insert(
"__NEXT_ACTIONS_DEPLOYMENT_ID".to_string(),
if next_config
.experimental
.use_deployment_id_server_actions
.unwrap_or(false)
{
"true".to_string()
} else {
"false".to_string()
},
);

map.insert(
"NEXT_DEPLOYMENT_ID".to_string(),
// Don't stringify undefined
if let Some(deployment_id) = next_config.experimental.deployment_id.as_ref() {
serde_json::to_string(deployment_id)?
} else {
"undefined".to_string()
},
);

map.insert(
"__NEXT_MANUAL_CLIENT_BASE_PATH".to_string(),
if next_config
.experimental
.manual_client_base_path
.unwrap_or(false)
{
"true".to_string()
} else {
"false".to_string()
},
);

map.insert(
"__NEXT_OPTIMISTIC_CLIENT_CACHE".to_string(),
if next_config
.experimental
.optimistic_client_cache
.unwrap_or(false)
{
"true".to_string()
} else {
"false".to_string()
},
);

map.insert(
"__NEXT_MIDDLEWARE_PREFETCH".to_string(),
// Don't stringify undefined
if let Some(middleware_prefetch) = next_config.experimental.middleware_prefetch.as_ref() {
serde_json::to_string(middleware_prefetch)?
} else {
"undefined".to_string()
},
);

// TODO: Implement crossOrigin in Turbopack script injection
map.insert(
"__NEXT_CROSS_ORIGIN".to_string(),
// Don't stringify undefined
if let Some(cross_origin) = next_config.cross_origin.as_ref() {
serde_json::to_string(cross_origin)?
} else {
"undefined".to_string()
},
);

map.insert(
"__NEXT_BUILD_INDICATOR".to_string(),
// Don't stringify undefined
match next_config.dev_indicators.as_ref() {
Some(dev_indicators) => match dev_indicators.build_activity.as_ref() {
Some(build_activity) => serde_json::to_string(build_activity)?,
None => "false".to_string(),
},
None => "false".to_string(),
},
);

map.insert(
"__NEXT_BUILD_INDICATOR_POSITION".to_string(),
// Don't stringify undefined
match next_config.dev_indicators.as_ref() {
Some(dev_indicators) => match dev_indicators.build_activity_position.as_ref() {
Some(build_activity_position) => serde_json::to_string(build_activity_position)?,
None => "undefined".to_string(),
},
None => "undefined".to_string(),
},
);

map.insert(
"__NEXT_OPTIMIZE_FONTS".to_string(),
if next_config.optimize_fonts.unwrap_or(true) {
"true".to_string()
} else {
"false".to_string()
},
);

map.insert(
"__NEXT_OPTIMIZE_CSS".to_string(),
// Don't stringify undefined
if let Some(optimize_css) = next_config.experimental.optimize_css.as_ref() {
serde_json::to_string(optimize_css)?
} else {
"false".to_string()
},
);

map.insert(
"__NEXT_SCRIPT_WORKERS".to_string(),
// TODO: This should be true in production mode
// if next_config
// .experimental
// .next_script_workers
// .unwrap_or(false)
// {
// "false".to_string()
// } else {
// "false".to_string()
// },
"false".to_string(),
);

map.insert(
"__NEXT_CONFIG_OUTPUT".to_string(),
// Don't stringify undefined
if let Some(output) = next_config.output.as_ref() {
serde_json::to_string(output)?
} else {
"undefined".to_string()
},
);

map.insert(
"__NEXT_ANALYTICS_ID".to_string(),
// Don't stringify undefined
if let Some(analytics_id) = next_config.analytics_id.as_ref() {
serde_json::to_string(analytics_id)?
} else {
"undefined".to_string()
},
);

map.insert(
"__NEXT_HAS_WEB_VITALS_ATTRIBUTION".to_string(),
if next_config.experimental.web_vitals_attribution.is_none() {
"false".to_string()
} else {
"true".to_string()
},
);

map.insert(
"__NEXT_WEB_VITALS_ATTRIBUTION".to_string(),
// Don't stringify undefined
if let Some(web_vitals_attribution) =
next_config.experimental.web_vitals_attribution.as_ref()
{
serde_json::to_string(web_vitals_attribution)?
} else {
"undefined".to_string()
},
);

// TODO: Implement
// map.insert(
// "__NEXT_FETCH_CACHE_KEY_PREFIX".to_string(),
// );

// TODO: Implement
// map.insert(
// "__NEXT_HAS_REWRITES".to_string(),
// );

// TODO: Implement for node server only?
// map.insert(
// "__NEXT_EXPERIMENTAL_REACT".to_string(),
// );

if !test_mode.is_empty() {
map.insert("__NEXT_TEST_MODE".to_string(), "true".to_string());
}
Expand Down
44 changes: 22 additions & 22 deletions packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub struct NextConfig {
pub skip_middleware_url_normalize: Option<bool>,
pub skip_trailing_slash_redirect: Option<bool>,
pub i18n: Option<I18NConfig>,
pub cross_origin: Option<String>,
pub dev_indicators: Option<DevIndicatorsConfig>,
pub output: Option<OutputType>,
pub analytics_id: Option<String>,

///
#[serde(rename = "_originalRedirects")]
Expand All @@ -99,15 +103,12 @@ pub struct NextConfig {
// Partially supported
pub compiler: Option<CompilerConfig>,

pub output: Option<OutputType>,
pub optimize_fonts: Option<bool>,

// unsupported
cross_origin: Option<String>,
amp: AmpConfig,
analytics_id: String,
clean_dist_dir: bool,
compress: bool,
dev_indicators: DevIndicatorsConfig,
eslint: EslintConfig,
exclude_default_moment_locales: bool,
// this can be a function in js land
Expand All @@ -117,7 +118,6 @@ pub struct NextConfig {
generate_etags: bool,
http_agent_options: HttpAgentConfig,
on_demand_entries: OnDemandEntriesConfig,
optimize_fonts: bool,
output_file_tracing: bool,
powered_by_header: bool,
production_browser_source_maps: bool,
Expand Down Expand Up @@ -146,7 +146,7 @@ struct EslintConfig {

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "kebab-case")]
enum BuildActivityPositions {
pub enum BuildActivityPositions {
#[default]
BottomRight,
BottomLeft,
Expand All @@ -156,9 +156,9 @@ enum BuildActivityPositions {

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
struct DevIndicatorsConfig {
build_activity: bool,
build_activity_position: BuildActivityPositions,
pub struct DevIndicatorsConfig {
pub build_activity: Option<bool>,
pub build_activity_position: Option<BuildActivityPositions>,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)]
Expand Down Expand Up @@ -431,6 +431,18 @@ pub struct ExperimentalConfig {
pub turbotrace: Option<serde_json::Value>,
pub external_middleware_rewrites_resolve: Option<bool>,
pub scroll_restoration: Option<bool>,
pub use_deployment_id: Option<bool>,
pub use_deployment_id_server_actions: Option<bool>,
pub deployment_id: Option<String>,
pub manual_client_base_path: Option<bool>,
pub optimistic_client_cache: Option<bool>,
pub middleware_prefetch: Option<MiddlewarePrefetchType>,
/// optimizeCss can be boolean or critters' option object
/// Use Record<string, unknown> as critters doesn't export its Option type
/// https://github.com/GoogleChromeLabs/critters/blob/a590c05f9197b656d2aeaae9369df2483c26b072/packages/critters/src/index.d.ts
pub optimize_css: Option<serde_json::Value>,
pub next_script_workers: Option<bool>,
pub web_vitals_attribution: Option<Vec<String>>,

// ---
// UNSUPPORTED
Expand All @@ -442,7 +454,6 @@ pub struct ExperimentalConfig {
case_sensitive_routes: Option<bool>,
cpus: Option<f64>,
cra_compat: Option<bool>,
deployment_id: Option<String>,
disable_optimized_loading: Option<bool>,
disable_postcss_preset_env: Option<bool>,
esm_externals: Option<serde_json::Value>,
Expand All @@ -460,17 +471,9 @@ pub struct ExperimentalConfig {
instrumentation_hook: Option<bool>,
large_page_data_bytes: Option<f64>,
logging: Option<serde_json::Value>,
manual_client_base_path: Option<bool>,
memory_based_workers_count: Option<bool>,
middleware_prefetch: Option<MiddlewarePrefetchType>,
next_script_workers: Option<bool>,
optimistic_client_cache: Option<bool>,
/// Optimize React APIs for server builds.
optimize_server_react: Option<bool>,
/// optimizeCss can be boolean or critters' option object
/// Use Record<string, unknown> as critters doesn't export its Option type
/// https://github.com/GoogleChromeLabs/critters/blob/a590c05f9197b656d2aeaae9369df2483c26b072/packages/critters/src/index.d.ts
optimize_css: Option<serde_json::Value>,
/// Automatically apply the "modularize_imports" optimization to imports of
/// the specified packages.
optimize_package_imports: Option<Vec<String>>,
Expand Down Expand Up @@ -502,9 +505,6 @@ pub struct ExperimentalConfig {
/// @see https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes
typed_routes: Option<bool>,
url_imports: Option<serde_json::Value>,
use_deployment_id: Option<bool>,
use_deployment_id_server_actions: Option<bool>,
web_vitals_attribution: Option<Vec<String>>,
/// This option is to enable running the Webpack build in a worker thread
/// (doesn't apply to Turbopack).
webpack_build_worker: Option<bool>,
Expand All @@ -520,7 +520,7 @@ enum SizeLimit {

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "kebab-case")]
enum MiddlewarePrefetchType {
pub enum MiddlewarePrefetchType {
Strict,
Flexible,
}
Expand Down

0 comments on commit 5415811

Please sign in to comment.