Skip to content

Commit

Permalink
Expose module_options and resolve_options in turbotrace (vercel/turbo…
Browse files Browse the repository at this point in the history
…repo#3839)

This pr allows passing `ModuleOptionsContext` and
`ResolveOptionsContext` in fn start, so next.js can pass enableJsx:
true, enableTypes: true while tracing entries, and `enableJsx: false`,
`enableTypes: false` while tracing compiled files.
  • Loading branch information
Brooooooklyn committed Mar 2, 2023
1 parent ac7eec3 commit f8055a0
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 73 deletions.
190 changes: 118 additions & 72 deletions crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use turbo_tasks::{
NothingVc, TaskId, TransientInstance, TransientValue, TurboTasks, TurboTasksBackendApi, Value,
};
use turbo_tasks_fs::{
glob::GlobVc, DirectoryEntry, DiskFileSystemVc, FileSystem, FileSystemVc, ReadGlobResultVc,
glob::GlobVc, DirectoryEntry, DiskFileSystemVc, FileSystem, FileSystemPathVc, FileSystemVc,
ReadGlobResultVc,
};
use turbo_tasks_memory::{
stats::{ReferenceType, Stats},
Expand Down Expand Up @@ -128,11 +129,6 @@ pub struct CommonArgs {
#[cfg_attr(feature = "node-api", serde(default))]
exact: bool,

/// Whether to enable mdx parsing while tracing dependencies
#[cfg_attr(feature = "cli", clap(long))]
#[cfg_attr(feature = "node-api", serde(default))]
enable_mdx: bool,

/// Enable experimental garbage collection with the provided memory limit in
/// MB.
#[cfg_attr(feature = "cli", clap(long))]
Expand Down Expand Up @@ -241,60 +237,20 @@ async fn add_glob_results(
async fn input_to_modules<'a>(
fs: FileSystemVc,
input: Vec<String>,
process_cwd: Option<String>,
exact: bool,
enable_mdx: bool,
process_cwd: Option<String>,
context: String,
module_options: TransientInstance<ModuleOptionsContext>,
resolve_options: TransientInstance<ResolveOptionsContext>,
) -> Result<AssetsVc> {
let root = fs.root();
let env = EnvironmentVc::new(
Value::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
cwd: OptionStringVc::cell(process_cwd),
..Default::default()
}
.into(),
)),
Value::new(EnvironmentIntention::Api),
);
let compile_time_info = CompileTimeInfo {
environment: env,
defines: CompileTimeDefinesVc::empty(),
}
.cell();
let glob_mappings = vec![
(
root,
GlobVc::new("**/*/next/dist/server/next.js"),
ImportMapping::Ignore.into(),
),
(
root,
GlobVc::new("**/*/next/dist/bin/next"),
ImportMapping::Ignore.into(),
),
];
let context: AssetContextVc = ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
compile_time_info,
ModuleOptionsContext {
enable_types: true,
enable_mdx,
..Default::default()
}
.cell(),
ResolveOptionsContext {
emulate_environment: Some(env),
resolved_map: Some(
ResolvedMap {
by_glob: glob_mappings,
}
.cell(),
),
..Default::default()
}
.cell(),
)
.into();
let process_cwd = process_cwd
.clone()
.map(|p| p.trim_start_matches(&context).to_owned());

let context: AssetContextVc =
create_module_asset(root, process_cwd, module_options, resolve_options).into();

let mut list = Vec::new();
for input in input.iter() {
if exact {
Expand Down Expand Up @@ -353,6 +309,8 @@ fn process_input(dir: &Path, context: &str, input: &[String]) -> Result<Vec<Stri
pub async fn start(
args: Arc<Args>,
turbo_tasks: Option<&Arc<TurboTasks<MemoryBackend>>>,
module_options: Option<ModuleOptionsContext>,
resolve_options: Option<ResolveOptionsContext>,
) -> Result<Vec<String>> {
register();
let &CommonArgs {
Expand Down Expand Up @@ -437,6 +395,8 @@ pub async fn start(
println!("graph.html written");
}
},
module_options,
resolve_options,
)
.await
}
Expand All @@ -445,6 +405,8 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
args: Arc<Args>,
create_tt: impl Fn() -> Arc<TurboTasks<B>>,
final_finish: impl FnOnce(Arc<TurboTasks<B>>, TaskId, Duration) -> F,
module_options: Option<ModuleOptionsContext>,
resolve_options: Option<ResolveOptionsContext>,
) -> Result<Vec<String>> {
let &CommonArgs {
watch,
Expand Down Expand Up @@ -497,8 +459,15 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
let dir = dir.clone();
let args = args.clone();
let sender = sender.clone();
let module_options = TransientInstance::new(module_options.clone().unwrap_or_default());
let resolve_options = TransientInstance::new(resolve_options.clone().unwrap_or_default());
Box::pin(async move {
let output = main_operation(TransientValue::new(dir.clone()), args.clone().into());
let output = main_operation(
TransientValue::new(dir.clone()),
args.clone().into(),
module_options,
resolve_options,
);

let source = TransientValue::new(output.into());
let issues = IssueVc::peek_issues_with_path(output)
Expand Down Expand Up @@ -538,28 +507,36 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
async fn main_operation(
current_dir: TransientValue<PathBuf>,
args: TransientInstance<Args>,
module_options: TransientInstance<ModuleOptionsContext>,
resolve_options: TransientInstance<ResolveOptionsContext>,
) -> Result<StringsVc> {
let dir = current_dir.into_value();
let args = &*args;
let &CommonArgs {
ref input,
watch,
exact,
enable_mdx,
ref context_directory,
ref process_cwd,
..
} = args.common();
let context = process_context(&dir, context_directory.as_ref()).unwrap();
let process_cwd = process_cwd
.clone()
.map(|p| p.trim_start_matches(&context).to_owned());
let fs = create_fs("context directory", &context, watch).await?;

match *args {
Args::Print { common: _ } => {
let input = process_input(&dir, &context, input).unwrap();
let mut result = BTreeSet::new();
let fs = create_fs("context directory", &context, watch).await?;
let modules = input_to_modules(fs, input, process_cwd, exact, enable_mdx).await?;
let modules = input_to_modules(
fs,
input,
exact,
process_cwd.clone(),
context,
module_options,
resolve_options,
)
.await?;
for module in modules.iter() {
let set = all_assets(*module);
IssueVc::attach_context(
Expand All @@ -578,12 +555,19 @@ async fn main_operation(
}
Args::Annotate { common: _ } => {
let input = process_input(&dir, &context, input).unwrap();
let fs = create_fs("context directory", &context, watch).await?;
let mut output_nft_assets = Vec::new();
let mut emits = Vec::new();
for module in input_to_modules(fs, input, process_cwd, exact, enable_mdx)
.await?
.iter()
for module in input_to_modules(
fs,
input,
exact,
process_cwd.clone(),
context,
module_options,
resolve_options,
)
.await?
.iter()
{
let nft_asset = NftJsonAssetVc::new(*module);
let path = nft_asset.ident().path().await?.path.clone();
Expand All @@ -602,14 +586,21 @@ async fn main_operation(
} => {
let output = process_context(&dir, Some(output_directory)).unwrap();
let input = process_input(&dir, &context, input).unwrap();
let fs = create_fs("context directory", &context, watch).await?;
let out_fs = create_fs("output directory", &output, watch).await?;
let input_dir = fs.root();
let output_dir = out_fs.root();
let mut emits = Vec::new();
for module in input_to_modules(fs, input, process_cwd, exact, enable_mdx)
.await?
.iter()
for module in input_to_modules(
fs,
input,
exact,
process_cwd.clone(),
context,
module_options,
resolve_options,
)
.await?
.iter()
{
let rebased = RebasedAssetVc::new(*module, input_dir, output_dir).into();
emits.push(emit_with_completion(rebased, output_dir));
Expand All @@ -624,6 +615,61 @@ async fn main_operation(
Ok(StringsVc::cell(Vec::new()))
}

#[turbo_tasks::function]
async fn create_module_asset(
root: FileSystemPathVc,
process_cwd: Option<String>,
module_options: TransientInstance<ModuleOptionsContext>,
resolve_options: TransientInstance<ResolveOptionsContext>,
) -> Result<ModuleAssetContextVc> {
let env = EnvironmentVc::new(
Value::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment {
cwd: OptionStringVc::cell(process_cwd.clone()),
..Default::default()
}
.into(),
)),
Value::new(EnvironmentIntention::Api),
);
let compile_time_info = CompileTimeInfo {
environment: env,
defines: CompileTimeDefinesVc::empty(),
}
.cell();
let glob_mappings = vec![
(
root,
GlobVc::new("**/*/next/dist/server/next.js"),
ImportMapping::Ignore.into(),
),
(
root,
GlobVc::new("**/*/next/dist/bin/next"),
ImportMapping::Ignore.into(),
),
];
let mut resolve_options = ResolveOptionsContext::clone(&*resolve_options);
if resolve_options.emulate_environment.is_none() {
resolve_options.emulate_environment = Some(env);
}
if resolve_options.resolved_map.is_none() {
resolve_options.resolved_map = Some(
ResolvedMap {
by_glob: glob_mappings,
}
.cell(),
);
}

Ok(ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
compile_time_info,
ModuleOptionsContext::clone(&*module_options).cell(),
resolve_options.cell(),
))
}

fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
Expand Down
2 changes: 1 addition & 1 deletion crates/node-file-trace/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> Result<()> {
console_subscriber::init();
let args = Arc::new(Args::parse());
let should_print = matches!(&*args, Args::Print { .. });
let result = start(args, None).await?;
let result = start(args, None, None, None).await?;
if should_print {
for file in result.iter() {
println!("{}", file);
Expand Down
17 changes: 17 additions & 0 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,42 @@ impl WebpackLoadersOptions {
#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
pub struct ModuleOptionsContext {
#[serde(default)]
pub enable_jsx: bool,
#[serde(default)]
pub enable_emotion: bool,
#[serde(default)]
pub enable_react_refresh: bool,
#[serde(default)]
pub enable_styled_components: bool,
#[serde(default)]
pub enable_styled_jsx: bool,
#[serde(default)]
pub enable_postcss_transform: Option<PostCssTransformOptions>,
#[serde(default)]
pub enable_webpack_loaders: Option<WebpackLoadersOptions>,
#[serde(default)]
pub enable_types: bool,
#[serde(default)]
pub enable_typescript_transform: bool,
#[serde(default)]
pub enable_mdx: bool,
#[serde(default)]
pub preset_env_versions: Option<EnvironmentVc>,
#[serde(default)]
pub custom_ecmascript_app_transforms: Vec<EcmascriptInputTransform>,
#[serde(default)]
pub custom_ecmascript_transforms: Vec<EcmascriptInputTransform>,
#[serde(default)]
/// Custom rules to be applied after all default rules.
pub custom_rules: Vec<ModuleRule>,
#[serde(default)]
pub execution_context: Option<ExecutionContextVc>,
#[serde(default)]
/// A list of rules to use a different module option context for certain
/// context paths. The first matching is used.
pub rules: Vec<(ContextCondition, ModuleOptionsContextVc)>,
#[serde(default)]
pub placeholder_for_future_extensions: (),
}

Expand Down
Loading

0 comments on commit f8055a0

Please sign in to comment.