Skip to content

Commit

Permalink
thread tracing through to resolve results
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon authored and mischnic committed Nov 5, 2024
1 parent ef71eef commit d90be1f
Show file tree
Hide file tree
Showing 34 changed files with 633 additions and 163 deletions.
5 changes: 4 additions & 1 deletion crates/next-core/src/next_build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use turbo_tasks::{RcStr, ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::resolve::{options::ImportMapping, ExternalType};
use turbopack_core::resolve::{options::ImportMapping, ExternalTraced, ExternalType};

use crate::next_import_map::get_next_package;

Expand All @@ -23,11 +23,14 @@ pub async fn get_postcss_package_mapping(

#[turbo_tasks::function]
pub async fn get_external_next_compiled_package_mapping(
project_path: ResolvedVc<FileSystemPath>,
package_name: Vc<RcStr>,
) -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some(format!("next/dist/compiled/{}", &*package_name.await?).into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.resolved_cell()])
.cell())
Expand Down
5 changes: 3 additions & 2 deletions crates/next-core/src/next_dynamic/dynamic_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ impl Transition for NextDynamicTransition {
module_asset_context,
Value::new(ReferenceType::Undefined),
)
.try_into_module()
.await?
{
ProcessResult::Module(client_module) => ProcessResult::Module(ResolvedVc::upcast(
Some(client_module) => ProcessResult::Module(ResolvedVc::upcast(
NextDynamicEntryModule::new(*client_module)
.to_resolved()
.await?,
)),
ProcessResult::Ignore => ProcessResult::Ignore,
None => ProcessResult::Ignore,
}
.cell())
}
Expand Down
69 changes: 55 additions & 14 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use turbopack_core::{
options::{ConditionValue, ImportMap, ImportMapping, ResolvedMap},
parse::Request,
pattern::Pattern,
resolve, AliasPattern, ExternalType, ResolveAliasMap, SubpathValue,
resolve, AliasPattern, ExternalTraced, ExternalType, ResolveAliasMap, SubpathValue,
},
source::Source,
};
Expand Down Expand Up @@ -239,7 +239,7 @@ pub async fn get_next_client_import_map(

/// Computes the Next-specific client import map.
#[turbo_tasks::function]
pub async fn get_next_build_import_map() -> Result<Vc<ImportMap>> {
pub async fn get_next_build_import_map(project_path: ResolvedVc<FileSystemPath>) -> Result<Vc<ImportMap>> {
let mut import_map = ImportMap::empty();

insert_package_alias(
Expand All @@ -248,15 +248,26 @@ pub async fn get_next_build_import_map() -> Result<Vc<ImportMap>> {
next_js_fs().root().to_resolved().await?,
);

let external = ImportMapping::External(None, ExternalType::CommonJs).resolved_cell();
let external = ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.resolved_cell();

import_map.insert_exact_alias("next", external);
import_map.insert_wildcard_alias("next/", external);
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs)
.resolved_cell(),
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.resolved_cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);

Expand Down Expand Up @@ -321,7 +332,14 @@ pub async fn get_next_server_import_map(

let ty = ty.into_value();

let external = ImportMapping::External(None, ExternalType::CommonJs).resolved_cell();
let external = ImportMapping::External(
None,
ExternalType::CommonJs,
// TODO(arlyon): wiring up in a follow up PR
ExternalTraced::Untraced,
None,
)
.resolved_cell();

import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty {
Expand All @@ -336,8 +354,13 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs)
.resolved_cell(),
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.resolved_cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);
// TODO: we should not bundle next/dist/build/utils in the pages renderer at all
Expand Down Expand Up @@ -562,12 +585,12 @@ async fn insert_next_server_special_aliases(
let external_cjs_if_node =
move |context_dir: ResolvedVc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(context_dir, request),
};
let external_esm_if_node =
move |context_dir: ResolvedVc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(context_dir, request),
};

import_map.insert_exact_alias(
Expand Down Expand Up @@ -1119,12 +1142,30 @@ fn request_to_import_mapping(

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_cjs_import_mapping(request: &str) -> ResolvedVc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::CommonJs).resolved_cell()
fn external_request_to_cjs_import_mapping(
context_dir: ResolvedVc<FileSystemPath>,
request: &str,
) -> ResolvedVc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::CommonJs,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.resolved_cell()
}

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_esm_import_mapping(request: &str) -> ResolvedVc<ImportMapping> {
ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).resolved_cell()
fn external_request_to_esm_import_mapping(
context_dir: ResolvedVc<FileSystemPath>,
request: &str,
) -> ResolvedVc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::EcmaScriptModule,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.resolved_cell()
}
34 changes: 20 additions & 14 deletions crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use turbopack_core::{
parse::Request,
pattern::Pattern,
plugin::{AfterResolvePlugin, AfterResolvePluginCondition},
resolve, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem,
ResolveResultOption,
resolve, ExternalTraced, ExternalType, FindContextFileResult, ResolveResult,
ResolveResultItem, ResolveResultOption,
},
source::Source,
};
Expand Down Expand Up @@ -375,10 +375,12 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
(FileType::CommonJs, false) => {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
ExternalType::CommonJs,
))
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: ExternalType::CommonJs,
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
Expand Down Expand Up @@ -412,25 +414,29 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
} else {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
if resolves_equal {
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: if resolves_equal {
ExternalType::CommonJs
} else {
ExternalType::EcmaScriptModule
},
))
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
}
(FileType::EcmaScriptModule, true) => {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request_str.into(),
ExternalType::EcmaScriptModule,
))
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: ExternalType::EcmaScriptModule,
// TODO(arlyon): wiring up in a follow up PR
traced: ExternalTraced::Untraced,
})
.cell(),
))
}
Expand Down
28 changes: 15 additions & 13 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use anyhow::Result;
use lazy_static::lazy_static;
use turbo_tasks::{RcStr, Value, Vc};
use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{glob::Glob, FileSystemPath};
use turbopack_core::{
diagnostics::DiagnosticExt,
Expand All @@ -15,7 +15,7 @@ use turbopack_core::{
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
},
ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
ExternalTraced, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
},
};

Expand Down Expand Up @@ -204,14 +204,14 @@ pub(crate) fn get_invalid_styled_jsx_resolve_plugin(

#[turbo_tasks::value]
pub(crate) struct NextExternalResolvePlugin {
root: Vc<FileSystemPath>,
project_path: ResolvedVc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl NextExternalResolvePlugin {
#[turbo_tasks::function]
pub fn new(root: Vc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { root }.cell()
pub fn new(project_path: ResolvedVc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { project_path }.cell()
}
}

Expand All @@ -220,7 +220,7 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(
self.root.root(),
self.project_path.root(),
Glob::new("**/next/dist/**/*.{external,runtime.dev,runtime.prod}.js".into()),
)
}
Expand All @@ -233,18 +233,20 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
_reference_type: Value<ReferenceType>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
let raw_fs_path = &*fs_path.await?;
let path = raw_fs_path.path.to_string();
let path = fs_path.await?.path.to_string();
// Find the starting index of 'next/dist' and slice from that point. It should
// always be found since the glob pattern above is specific enough.
let starting_index = path.find("next/dist").unwrap();
let specifier = &path[starting_index..];
// Replace '/esm/' with '/' to match the CJS version of the file.
let modified_path = path[starting_index..].replace("/esm/", "/");
let specifier: RcStr = specifier.replace("/esm/", "/").into();

Ok(Vc::cell(Some(
ResolveResult::primary(ResolveResultItem::External(
modified_path.into(),
ExternalType::CommonJs,
))
ResolveResult::primary(ResolveResultItem::External {
name: specifier.clone(),
ty: ExternalType::CommonJs,
traced: ExternalTraced::Traced(self.project_path),
})
.into(),
)))
}
Expand Down
15 changes: 11 additions & 4 deletions crates/next-core/src/next_shared/webpack_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use anyhow::Result;
use turbo_tasks::{RcStr, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::WebpackLoadersOptions;
use turbopack_core::resolve::options::ImportMapping;
use turbopack_core::resolve::{options::ImportMapping, ExternalTraced, ExternalType};

use self::{babel::maybe_add_babel_loader, sass::maybe_add_sass_loader};
use crate::{next_build::get_external_next_compiled_package_mapping, next_config::NextConfig};
use crate::next_config::NextConfig;

pub(crate) mod babel;
pub(crate) mod sass;
Expand Down Expand Up @@ -33,6 +33,13 @@ pub async fn webpack_loader_options(
}

#[turbo_tasks::function]
fn loader_runner_package_mapping() -> Vc<ImportMapping> {
get_external_next_compiled_package_mapping(Vc::cell("loader-runner".into()))
async fn loader_runner_package_mapping() -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some("next/dist/compiled/loader-runner".into()),
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell()])
.cell())
}
14 changes: 0 additions & 14 deletions packages/next/src/build/swc/generated-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export class ExternalObject<T> {
[K: symbol]: T
}
}
export interface TransformOutput {
code: string
map?: string
output?: string
}
export function mdxCompile(
value: string,
option: Buffer,
Expand Down Expand Up @@ -370,15 +365,6 @@ export interface NapiRewrite {
has?: Array<NapiRouteHas>
missing?: Array<NapiRouteHas>
}
export function createTurboTasks(
outputPath: string,
persistentCaching: boolean,
memoryLimit?: number | undefined | null
): ExternalObject<NextTurboTasks>
export function runTurboTracing(
options: Buffer,
turboTasks: ExternalObject<NextTurboTasks>
): Promise<Array<string>>
export function getTargetTriple(): string
export function initHeapProfiler(): ExternalObject<RefCell>
export function teardownHeapProfiler(
Expand Down
Loading

0 comments on commit d90be1f

Please sign in to comment.