Skip to content

Commit

Permalink
Ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Oct 23, 2024
1 parent a510db1 commit b80c46e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ impl Transition for NextEcmascriptClientReferenceTransition {
}

#[turbo_tasks::function]
async fn process(
async fn process_ignore_unknown(
self: Vc<Self>,
source: Vc<Box<dyn Source>>,
module_asset_context: Vc<ModuleAssetContext>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let part = match &*reference_type {
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportPart(
Expand Down Expand Up @@ -77,12 +78,13 @@ impl Transition for NextEcmascriptClientReferenceTransition {
} else {
source
};
let client_module = this.client_transition.process(
let client_module = this.client_transition.process_ignore_unknown(
client_source,
module_asset_context,
Value::new(ReferenceType::Entry(
EntryReferenceSubType::AppClientComponent,
)),
ignore_unknown,
);
let ProcessResult::Module(client_module) = *client_module.await? else {
return Ok(ProcessResult::Ignore.cell());
Expand Down
6 changes: 4 additions & 2 deletions crates/next-core/src/next_dynamic/dynamic_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ impl Transition for NextDynamicTransition {
}

#[turbo_tasks::function]
async fn process(
async fn process_ignore_unknown(
self: Vc<Self>,
source: Vc<Box<dyn Source>>,
module_asset_context: Vc<ModuleAssetContext>,
_reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let module_asset_context = self.process_context(module_asset_context);

let this = self.await?;

Ok(match *this
.client_transition
.process(
.process_ignore_unknown(
source,
module_asset_context,
Value::new(ReferenceType::Undefined),
ignore_unknown,
)
.await?
{
Expand Down
16 changes: 16 additions & 0 deletions turbopack/crates/turbopack-core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,29 @@ pub trait AssetContext {
reference_type: Value<ReferenceType>,
) -> Vc<ProcessResult>;

/// Process a source into a module.
fn process_ignore_unknown(
self: Vc<Self>,
asset: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Vc<ProcessResult>;

/// Process an [ResolveResult] into an [ModuleResolveResult].
fn process_resolve_result(
self: Vc<Self>,
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
) -> Vc<ModuleResolveResult>;

/// Process an [ResolveResult] into an [ModuleResolveResult].
fn process_resolve_result_ignore_unknown(
self: Vc<Self>,
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Vc<ModuleResolveResult>;

/// Gets a new AssetContext with the transition applied.
fn with_transition(self: Vc<Self>, transition: RcStr) -> Vc<Box<dyn AssetContext>>;

Expand Down
10 changes: 10 additions & 0 deletions turbopack/crates/turbopack-core/src/resolve/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ impl Pattern {
}
}

pub fn has_dynamic_parts(&self) -> bool {
match self {
Pattern::Constant(_) => false,
Pattern::Dynamic => true,
Pattern::Alternatives(list) | Pattern::Concatenation(list) => {
list.iter().any(|p| p.has_dynamic_parts())
}
}
}

pub fn constant_prefix(&self) -> &str {
// The normalized pattern is an Alternative of maximally merged
// Concatenations, so extracting the first/only Concatenation child
Expand Down
89 changes: 65 additions & 24 deletions turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ impl ModuleAssetContext {
self: Vc<Self>,
source: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let this = self.await?;
Ok(
Expand All @@ -452,9 +453,9 @@ impl ModuleAssetContext {
.get_by_rules(source, &reference_type)
.await?
{
transition.process(source, self, reference_type)
transition.process_ignore_unknown(source, self, reference_type, ignore_unknown)
} else {
self.process_default(source, reference_type)
self.process_default(source, reference_type, ignore_unknown)
},
)
}
Expand All @@ -465,8 +466,9 @@ impl ModuleAssetContext {
self: Vc<Self>,
source: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Vc<ProcessResult> {
process_default(self, source, reference_type, Vec::new())
process_default(self, source, reference_type, Vec::new(), ignore_unknown)
}
}

Expand All @@ -476,6 +478,7 @@ async fn process_default(
source: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
processed_rules: Vec<usize>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let span = tracing::info_span!(
"process module",
Expand All @@ -487,6 +490,7 @@ async fn process_default(
source,
reference_type,
processed_rules,
ignore_unknown,
)
.instrument(span)
.await
Expand All @@ -497,6 +501,7 @@ async fn process_default_internal(
source: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
processed_rules: Vec<usize>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let ident = source.ident().resolve().await?;
let path_ref = ident.path().await?;
Expand Down Expand Up @@ -553,10 +558,11 @@ async fn process_default_internal(
.get_by_rules(current_source, &reference_type)
.await?
{
return Ok(transition.process(
return Ok(transition.process_ignore_unknown(
current_source,
module_asset_context,
Value::new(reference_type),
ignore_unknown,
));
} else {
let mut processed_rules = processed_rules.clone();
Expand All @@ -566,6 +572,7 @@ async fn process_default_internal(
current_source,
Value::new(reference_type),
processed_rules,
ignore_unknown,
));
}
}
Expand Down Expand Up @@ -633,19 +640,20 @@ async fn process_default_internal(
let module_type = match current_module_type {
Some(module_type) => module_type,
None => {
ModuleIssue {
ident,
title: StyledString::Text("Unknown module type".into()).cell(),
description: StyledString::Text(
r"This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders".into(),
)
.cell(),
if !ignore_unknown {
ModuleIssue {
ident,
title: StyledString::Text("Unknown module type".into()).cell(),
description: StyledString::Text(
r"This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders".into(),
)
.cell(),
}
.cell()
.emit();
}
.cell()
.emit();

return Ok(ProcessResult::Ignore.cell());
}
}.cell();
Expand Down Expand Up @@ -729,7 +737,20 @@ impl AssetContext for ModuleAssetContext {
request,
resolve_options,
);
let mut result = self.process_resolve_result(result.resolve().await?, reference_type);
// if context_path.await?.path.contains("/node-gyp/") {
// println!(
// "resolve_asset: {} {} {:?} {:?}",
// context_path.to_string().await?,
// request.request_pattern().await?,
// &*reference_type,
// result.dbg().await?
// );
// }
let mut result = self.process_resolve_result_ignore_unknown(
result.resolve().await?,
reference_type,
request.request_pattern().await?.has_dynamic_parts(),
);

if *self.is_types_resolving_enabled().await? {
let types_result = type_resolve(
Expand All @@ -749,19 +770,29 @@ impl AssetContext for ModuleAssetContext {
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
) -> Result<Vc<ModuleResolveResult>> {
let this = self.await?;
Ok(self.process_resolve_result_ignore_unknown(result, reference_type, false))
}

#[turbo_tasks::function]
async fn process_resolve_result_ignore_unknown(
self: Vc<Self>,
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ModuleResolveResult>> {
let this = self.await?;
let result = result
.await?
.map_items_module(|item| {
let reference_type = reference_type.clone();
async move {
Ok(match item {
ResolveResultItem::Source(source) => {
match &*self.process(source, reference_type).await? {
ProcessResult::Module(module) => {
ModuleResolveResultItem::Module(*module)
}
match &*self
.process_ignore_unknown(source, reference_type, ignore_unknown)
.await?
{
ProcessResult::Module(m) => ModuleResolveResultItem::Module(*m),
ProcessResult::Ignore => ModuleResolveResultItem::Ignore,
}
}
Expand Down Expand Up @@ -817,12 +848,22 @@ impl AssetContext for ModuleAssetContext {
self: Vc<Self>,
asset: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
) -> Result<Vc<ProcessResult>> {
Ok(self.process_ignore_unknown(asset, reference_type, false))
}

#[turbo_tasks::function]
async fn process_ignore_unknown(
self: Vc<Self>,
asset: Vc<Box<dyn Source>>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let this = self.await?;
if let Some(transition) = this.transition {
Ok(transition.process(asset, self, reference_type))
Ok(transition.process_ignore_unknown(asset, self, reference_type, ignore_unknown))
} else {
Ok(self.process_with_transition_rules(asset, reference_type))
Ok(self.process_with_transition_rules(asset, reference_type, ignore_unknown))
}
}

Expand Down
13 changes: 12 additions & 1 deletion turbopack/crates/turbopack/src/transition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,21 @@ pub trait Transition {
asset: Vc<Box<dyn Source>>,
module_asset_context: Vc<ModuleAssetContext>,
reference_type: Value<ReferenceType>,
) -> Result<Vc<ProcessResult>> {
Ok(self.process_ignore_unknown(asset, module_asset_context, reference_type, false))
}

/// Apply modification on the processing of the asset
async fn process_ignore_unknown(
self: Vc<Self>,
asset: Vc<Box<dyn Source>>,
module_asset_context: Vc<ModuleAssetContext>,
reference_type: Value<ReferenceType>,
ignore_unknown: bool,
) -> Result<Vc<ProcessResult>> {
let asset = self.process_source(asset);
let module_asset_context = self.process_context(module_asset_context);
let m = module_asset_context.process_default(asset, reference_type);
let m = module_asset_context.process_default(asset, reference_type, ignore_unknown);
Ok(match *m.await? {
ProcessResult::Module(m) => {
ProcessResult::Module(self.process_module(m, module_asset_context))
Expand Down

0 comments on commit b80c46e

Please sign in to comment.