Unverified Commit afa97721 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: set env in tracing context (#75254)

Part of PACK-4164

~~Set `NODE_ENV` in tracing context~~ For that, see https://github.com/vercel/next.js/pull/75254#issuecomment-2627686346
Set `TURBOPACK` in tracing context

This get rids of some constant build overhead (because we only analyze one instance of react-dom for NFT now, instead of all of the edge/server/client onces).
Otherwise no measureable perf/memory impact
parent 0985b231
...@@ -8,8 +8,8 @@ use turbo_tasks_fs::FileSystemPath; ...@@ -8,8 +8,8 @@ use turbo_tasks_fs::FileSystemPath;
use turbopack::{ use turbopack::{
css::chunk::CssChunkType, css::chunk::CssChunkType,
module_options::{ module_options::{
CssOptionsContext, EcmascriptOptionsContext, JsxTransformOptions, ModuleOptionsContext, CssOptionsContext, EcmascriptOptionsContext, ExternalsTracingOptions, JsxTransformOptions,
ModuleRule, TypeofWindow, TypescriptTransformOptions, ModuleOptionsContext, ModuleRule, TypeofWindow, TypescriptTransformOptions,
}, },
resolve_options_context::ResolveOptionsContext, resolve_options_context::ResolveOptionsContext,
transition::Transition, transition::Transition,
...@@ -19,6 +19,7 @@ use turbopack_core::{ ...@@ -19,6 +19,7 @@ use turbopack_core::{
ChunkingConfig, MangleType, MinifyType, SourceMapsType, ChunkingConfig, MangleType, MinifyType, SourceMapsType,
module_id_strategies::ModuleIdStrategy, module_id_strategies::ModuleIdStrategy,
}, },
compile_time_defines,
compile_time_info::{CompileTimeDefines, CompileTimeInfo, FreeVarReferences}, compile_time_info::{CompileTimeDefines, CompileTimeInfo, FreeVarReferences},
environment::{ environment::{
Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion, RuntimeVersions, Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion, RuntimeVersions,
...@@ -387,6 +388,49 @@ pub async fn get_server_compile_time_info( ...@@ -387,6 +388,49 @@ pub async fn get_server_compile_time_info(
.await .await
} }
#[turbo_tasks::function]
pub async fn get_tracing_compile_time_info() -> Result<Vc<CompileTimeInfo>> {
CompileTimeInfo::builder(
Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment::default().resolved_cell(),
))
.to_resolved()
.await?,
)
/*
We'd really like to set `process.env.NODE_ENV = "production"` here, but with that,
`react/cjs/react.development.js` won't be copied anymore (as expected).
However if you `import` react from native ESM: `import {createContext} from 'react';`, it fails with
```
import {createContext} from 'react';
^^^^^^^^^^^^^
SyntaxError: Named export 'createContext' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
```
This is because Node's import-cjs-from-esm feature can correctly find all named exports in
```
// `react/index.js`
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.js');
} else {
module.exports = require('./cjs/react.development.js');
}
```
if both files exist (which is what's happening so far).
If `react.development.js` doesn't exist, then it bails with that error message.
Also just removing that second branch works fine, but a `require` to a non-existent file fails.
*/
.defines(
compile_time_defines!(
process.env.TURBOPACK = true,
// process.env.NODE_ENV = "production",
)
.resolved_cell(),
)
.cell()
.await
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_server_module_options_context( pub async fn get_server_module_options_context(
project_path: FileSystemPath, project_path: FileSystemPath,
...@@ -544,7 +588,13 @@ pub async fn get_server_module_options_context( ...@@ -544,7 +588,13 @@ pub async fn get_server_module_options_context(
tree_shaking_mode: tree_shaking_mode_for_user_code, tree_shaking_mode: tree_shaking_mode_for_user_code,
side_effect_free_packages: next_config.optimize_package_imports().owned().await?, side_effect_free_packages: next_config.optimize_package_imports().owned().await?,
enable_externals_tracing: if next_mode.is_production() { enable_externals_tracing: if next_mode.is_production() {
Some(project_path) Some(
ExternalsTracingOptions {
tracing_root: project_path,
compile_time_info: get_tracing_compile_time_info().to_resolved().await?,
}
.resolved_cell(),
)
} else { } else {
None None
}, },
......
...@@ -34,7 +34,6 @@ use turbopack_core::{ ...@@ -34,7 +34,6 @@ use turbopack_core::{
chunk::SourceMapsType, chunk::SourceMapsType,
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
context::{AssetContext, ProcessResult}, context::{AssetContext, ProcessResult},
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment},
ident::Layer, ident::Layer,
issue::{IssueExt, IssueSource, StyledString, module::ModuleIssue}, issue::{IssueExt, IssueSource, StyledString, module::ModuleIssue},
module::Module, module::Module,
...@@ -643,15 +642,12 @@ async fn process_default_internal( ...@@ -643,15 +642,12 @@ async fn process_default_internal(
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleAssetContext>> { async fn externals_tracing_module_context(
let env = Environment::new(ExecutionEnvironment::NodeJsLambda( ty: ExternalType,
NodeJsEnvironment::default().resolved_cell(), compile_time_info: Vc<CompileTimeInfo>,
)) ) -> Result<Vc<ModuleAssetContext>> {
.to_resolved()
.await?;
let resolve_options = ResolveOptionsContext { let resolve_options = ResolveOptionsContext {
emulate_environment: Some(env), emulate_environment: Some(compile_time_info.await?.environment),
loose_errors: true, loose_errors: true,
custom_conditions: match ty { custom_conditions: match ty {
ExternalType::CommonJs => vec![rcstr!("require")], ExternalType::CommonJs => vec![rcstr!("require")],
...@@ -663,7 +659,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA ...@@ -663,7 +659,7 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
Ok(ModuleAssetContext::new_without_replace_externals( Ok(ModuleAssetContext::new_without_replace_externals(
Default::default(), Default::default(),
CompileTimeInfo::builder(env).cell().await?, compile_time_info,
// Keep these options more or less in sync with // Keep these options more or less in sync with
// turbopack/crates/turbopack/tests/node-file-trace.rs to ensure that the NFT unit tests // turbopack/crates/turbopack/tests/node-file-trace.rs to ensure that the NFT unit tests
// are actually representative of what Turbopack does. // are actually representative of what Turbopack does.
...@@ -784,7 +780,7 @@ impl AssetContext for ModuleAssetContext { ...@@ -784,7 +780,7 @@ impl AssetContext for ModuleAssetContext {
ResolveResultItem::External { name, ty, traced } => { ResolveResultItem::External { name, ty, traced } => {
let replacement = if replace_externals { let replacement = if replace_externals {
let tracing_mode = if traced == ExternalTraced::Traced let tracing_mode = if traced == ExternalTraced::Traced
&& let Some(tracing_root) = &self && let Some(options) = &self
.module_options_context() .module_options_context()
.await? .await?
.enable_externals_tracing .enable_externals_tracing
...@@ -793,13 +789,17 @@ impl AssetContext for ModuleAssetContext { ...@@ -793,13 +789,17 @@ impl AssetContext for ModuleAssetContext {
// request will later be resolved relative to tracing_root // request will later be resolved relative to tracing_root
// anyway. // anyway.
let options = options.await?;
CachedExternalTracingMode::Traced { CachedExternalTracingMode::Traced {
externals_context: ResolvedVc::upcast( externals_context: ResolvedVc::upcast(
externals_tracing_module_context(ty) externals_tracing_module_context(
ty,
*options.compile_time_info,
)
.to_resolved() .to_resolved()
.await?, .await?,
), ),
root_origin: tracing_root.join("_")?, root_origin: options.tracing_root.join("_")?,
} }
} else { } else {
CachedExternalTracingMode::Untraced CachedExternalTracingMode::Untraced
......
...@@ -6,8 +6,8 @@ use turbo_rcstr::RcStr; ...@@ -6,8 +6,8 @@ use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, ValueDefault, Vc, trace::TraceRawVcs}; use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, ValueDefault, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
chunk::SourceMapsType, condition::ContextCondition, environment::Environment, chunk::SourceMapsType, compile_time_info::CompileTimeInfo, condition::ContextCondition,
resolve::options::ImportMapping, environment::Environment, resolve::options::ImportMapping,
}; };
use turbopack_ecmascript::{TreeShakingMode, references::esm::UrlRewriteBehavior}; use turbopack_ecmascript::{TreeShakingMode, references::esm::UrlRewriteBehavior};
pub use turbopack_mdx::MdxTransformOptions; pub use turbopack_mdx::MdxTransformOptions;
...@@ -121,7 +121,6 @@ impl ValueDefault for TypescriptTransformOptions { ...@@ -121,7 +121,6 @@ impl ValueDefault for TypescriptTransformOptions {
} }
} }
// [TODO]: should enabled_react_refresh belong to this options?
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
pub struct JsxTransformOptions { pub struct JsxTransformOptions {
...@@ -131,6 +130,14 @@ pub struct JsxTransformOptions { ...@@ -131,6 +130,14 @@ pub struct JsxTransformOptions {
pub runtime: Option<RcStr>, pub runtime: Option<RcStr>,
} }
#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
pub struct ExternalsTracingOptions {
/// The directory from which the bundled files will require the externals at runtime.
pub tracing_root: FileSystemPath,
pub compile_time_info: ResolvedVc<CompileTimeInfo>,
}
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, Default)] #[derive(Clone, Default)]
#[serde(default)] #[serde(default)]
...@@ -152,10 +159,7 @@ pub struct ModuleOptionsContext { ...@@ -152,10 +159,7 @@ pub struct ModuleOptionsContext {
/// Generate (non-emitted) output assets for static assets and externals, to facilitate /// Generate (non-emitted) output assets for static assets and externals, to facilitate
/// generating a list of all non-bundled files that will be required at runtime. /// generating a list of all non-bundled files that will be required at runtime.
/// pub enable_externals_tracing: Option<ResolvedVc<ExternalsTracingOptions>>,
/// The filepath is the directory from which the bundled files will require the externals at
/// runtime.
pub enable_externals_tracing: Option<FileSystemPath>,
/// If true, it stores the last successful parse result in state and keeps using it when /// If true, it stores the last successful parse result in state and keeps using it when
/// parsing fails. This is useful to keep the module graph structure intact when syntax errors /// parsing fails. This is useful to keep the module graph structure intact when syntax errors
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment