Skip to content

Commit

Permalink
Configure React development flag, inherit NODE_ENV from execution con…
Browse files Browse the repository at this point in the history
…text (#4972)

### Description

This PR allows Turbopack users to configure the `development` flag from
the SWC React transform. This is used in
vercel/next.js#49852.

This PR also makes the Node.js evaluation asset context inherit its
NODE_ENV from the execution context.

### Testing Instructions

N/A
  • Loading branch information
alexkirsz committed May 17, 2023
1 parent 059b631 commit 4d39f01
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum EcmascriptInputTransform {
Plugin(TransformPluginVc),
PresetEnv(EnvironmentVc),
React {
#[serde(default)]
development: bool,
#[serde(default)]
refresh: bool,
// swc.jsc.transform.react.importSource
Expand Down Expand Up @@ -143,6 +145,7 @@ impl EcmascriptInputTransform {
} = ctx;
match self {
EcmascriptInputTransform::React {
development,
refresh,
import_source,
runtime,
Expand All @@ -165,7 +168,7 @@ impl EcmascriptInputTransform {

let config = Options {
runtime: Some(runtime),
development: Some(true),
development: Some(*development),
import_source: import_source.await?.clone_value(),
refresh: if *refresh {
Some(swc_core::ecma::transforms::react::RefreshOptions {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-tests/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ async fn run_test(resource: &str) -> Result<FileSystemPathVc> {
compile_time_info,
ModuleOptionsContext {
enable_jsx: Some(JsxTransformOptionsVc::cell(JsxTransformOptions {
development: true,
..Default::default()
})),
enable_emotion: Some(EmotionTransformConfig::cell(EmotionTransformConfig {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_json = { workspace = true }
tokio = { workspace = true }

turbo-tasks = { workspace = true }
turbo-tasks-env = { workspace = true }
turbo-tasks-fs = { workspace = true }
turbopack-core = { workspace = true }
turbopack-css = { workspace = true }
Expand Down
17 changes: 12 additions & 5 deletions crates/turbopack/src/evaluate_context.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use anyhow::Result;
use turbo_tasks::Value;
use turbo_tasks_fs::{FileSystem, FileSystemPathVc};
use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::FileSystem;
use turbopack_core::{
compile_time_defines,
compile_time_info::CompileTimeInfo,
context::AssetContextVc,
environment::{EnvironmentIntention, EnvironmentVc, ExecutionEnvironment, NodeJsEnvironment},
resolve::options::{ImportMap, ImportMapVc, ImportMapping},
};
use turbopack_node::execution_context::ExecutionContextVc;

use crate::{
module_options::ModuleOptionsContext, resolve_options_context::ResolveOptionsContext,
Expand All @@ -26,7 +28,7 @@ pub fn node_build_environment() -> EnvironmentVc {

#[turbo_tasks::function]
pub async fn node_evaluate_asset_context(
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
import_map: Option<ImportMapVc>,
transitions: Option<TransitionsByNameVc>,
) -> Result<AssetContextVc> {
Expand All @@ -44,13 +46,18 @@ pub async fn node_evaluate_asset_context(
.cell(),
);
let import_map = import_map.cell();
let node_env = if let Some(node_env) = &*execution_context.env().read("NODE_ENV").await? {
node_env.clone()
} else {
"development".to_string()
};
Ok(ModuleAssetContextVc::new(
transitions.unwrap_or_else(|| TransitionsByNameVc::cell(Default::default())),
CompileTimeInfo::builder(node_build_environment())
.defines(
compile_time_defines!(
process.turbopack = true,
process.env.NODE_ENV = "development",
process.env.NODE_ENV = node_env.clone(),
)
.cell(),
)
Expand All @@ -62,10 +69,10 @@ pub async fn node_evaluate_asset_context(
.cell(),
ResolveOptionsContext {
enable_typescript: true,
enable_node_modules: Some(project_path.root().resolve().await?),
enable_node_modules: Some(execution_context.project_path().root().resolve().await?),
enable_node_externals: true,
enable_node_native_modules: true,
custom_conditions: vec!["development".to_string(), "node".to_string()],
custom_conditions: vec![node_env, "node".to_string()],
import_map: Some(import_map),
..Default::default()
}
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl ModuleOptionsVc {
let jsx = enable_jsx.await?;

transforms.push(EcmascriptInputTransform::React {
development: jsx.development,
refresh: jsx.react_refresh,
import_source: OptionStringVc::cell(jsx.import_source.clone()),
runtime: OptionStringVc::cell(jsx.runtime.clone()),
Expand Down Expand Up @@ -257,7 +258,7 @@ impl ModuleOptionsVc {
Some(ModuleRuleEffect::SourceTransforms(
SourceTransformsVc::cell(vec![PostCssTransformVc::new(
node_evaluate_asset_context(
execution_context.project_path(),
execution_context,
Some(import_map),
None,
),
Expand Down Expand Up @@ -472,7 +473,7 @@ impl ModuleOptionsVc {
ModuleRuleEffect::SourceTransforms(SourceTransformsVc::cell(vec![
WebpackLoadersVc::new(
node_evaluate_asset_context(
execution_context.project_path(),
execution_context,
Some(import_map),
None,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl WebpackLoadersOptions {
#[turbo_tasks::value(shared)]
#[derive(Default, Clone, Debug)]
pub struct JsxTransformOptions {
pub development: bool,
pub react_refresh: bool,
pub import_source: Option<String>,
pub runtime: Option<String>,
Expand Down

0 comments on commit 4d39f01

Please sign in to comment.