Skip to content

Commit 8b60108

Browse files
committed
Turbopack: introduce ReferenceTypeCondition
1 parent 5a22623 commit 8b60108

File tree

8 files changed

+132
-96
lines changed

8 files changed

+132
-96
lines changed

crates/next-api/src/app.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use turbopack_core::{
6363
},
6464
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
6565
reference::all_assets_from_entries,
66-
reference_type::{CommonJsReferenceSubType, CssReferenceSubType, ReferenceType},
66+
reference_type::{CommonJsReferenceSubType, CssReferenceSubType, ReferenceTypeCondition},
6767
resolve::{ResolveErrorMode, origin::PlainResolveOrigin, parse::Request, pattern::Pattern},
6868
source::Source,
6969
source_map::SourceMapAsset,
@@ -388,9 +388,9 @@ impl AppProject {
388388
// CSS Module to the actual CSS
389389
TransitionRule::new(
390390
RuleCondition::all(vec![
391-
RuleCondition::ReferenceType(ReferenceType::Css(
391+
RuleCondition::ReferenceType(ReferenceTypeCondition::Css(Some(
392392
CssReferenceSubType::Inner,
393-
)),
393+
))),
394394
module_styles_rule_condition(),
395395
]),
396396
self.css_client_reference_transition().to_resolved().await?,
@@ -399,9 +399,9 @@ impl AppProject {
399399
// the list of CSS module classes.
400400
TransitionRule::new(
401401
RuleCondition::all(vec![
402-
RuleCondition::ReferenceType(ReferenceType::Css(
402+
RuleCondition::ReferenceType(ReferenceTypeCondition::Css(Some(
403403
CssReferenceSubType::Analyze,
404-
)),
404+
))),
405405
module_styles_rule_condition(),
406406
]),
407407
ResolvedVc::upcast(self.client_transition().to_resolved().await?),
@@ -664,9 +664,9 @@ impl AppProject {
664664
// Change context, this is used to determine the list of CSS module classes.
665665
TransitionRule::new(
666666
RuleCondition::all(vec![
667-
RuleCondition::ReferenceType(ReferenceType::Css(
667+
RuleCondition::ReferenceType(ReferenceTypeCondition::Css(Some(
668668
CssReferenceSubType::Analyze,
669-
)),
669+
))),
670670
module_styles_rule_condition(),
671671
]),
672672
ResolvedVc::upcast(self.client_transition().to_resolved().await?),
@@ -736,9 +736,9 @@ impl AppProject {
736736
// Change context, this is used to determine the list of CSS module classes.
737737
TransitionRule::new(
738738
RuleCondition::all(vec![
739-
RuleCondition::ReferenceType(ReferenceType::Css(
739+
RuleCondition::ReferenceType(ReferenceTypeCondition::Css(Some(
740740
CssReferenceSubType::Analyze,
741-
)),
741+
))),
742742
module_styles_rule_condition(),
743743
]),
744744
ResolvedVc::upcast(self.client_transition().to_resolved().await?),

crates/next-core/src/next_server/resolve.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use turbo_tasks_fs::{
99
};
1010
use turbopack_core::{
1111
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
12-
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
12+
reference_type::{ReferenceType, ReferenceTypeCondition},
1313
resolve::{
1414
ExternalTraced, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem,
1515
ResolveResultOption, find_context_file,
@@ -155,8 +155,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
155155
};
156156

157157
let is_esm = self.import_externals
158-
&& ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::Undefined)
159-
.includes(&reference_type);
158+
&& ReferenceTypeCondition::EcmaScriptModules(None).includes(&reference_type);
160159

161160
#[derive(Debug, Copy, Clone)]
162161
enum FileType {

crates/next-core/src/next_server/transforms.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use turbo_rcstr::RcStr;
44
use turbo_tasks::{ResolvedVc, Vc};
55
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, RuleCondition};
66
use turbopack_core::reference_type::{
7-
CssReferenceSubType, EntryReferenceSubType, ReferenceType, UrlReferenceSubType,
7+
CssReferenceSubType, EntryReferenceSubType, ReferenceTypeCondition,
88
};
99

1010
use crate::{
@@ -65,7 +65,9 @@ pub async fn get_next_server_transforms_rules(
6565
// the class names object.
6666
ModuleRule::new(
6767
RuleCondition::all(vec![
68-
RuleCondition::ReferenceType(ReferenceType::Css(CssReferenceSubType::Inner)),
68+
RuleCondition::ReferenceType(ReferenceTypeCondition::Css(Some(
69+
CssReferenceSubType::Inner,
70+
))),
6971
module_styles_rule_condition(),
7072
]),
7173
vec![ModuleRuleEffect::Ignore],
@@ -90,8 +92,8 @@ pub async fn get_next_server_transforms_rules(
9092
pages_dir.clone(),
9193
ExportFilter::StripDefaultExport,
9294
mdx_rs,
93-
vec![RuleCondition::ReferenceType(ReferenceType::Entry(
94-
EntryReferenceSubType::PageData,
95+
vec![RuleCondition::ReferenceType(ReferenceTypeCondition::Entry(
96+
Some(EntryReferenceSubType::PageData),
9597
))],
9698
&page_extensions,
9799
)?);
@@ -209,8 +211,8 @@ pub async fn get_next_server_transforms_rules(
209211
// (i.e. for pages), while still allowing `new URL(..., import.meta.url)`
210212
rules.push(ModuleRule::new(
211213
RuleCondition::all(vec![
212-
RuleCondition::not(RuleCondition::ReferenceType(ReferenceType::Url(
213-
UrlReferenceSubType::Undefined,
214+
RuleCondition::not(RuleCondition::ReferenceType(ReferenceTypeCondition::Url(
215+
None,
214216
))),
215217
RuleCondition::any(vec![
216218
RuleCondition::ResourcePathEndsWith(".apng".to_string()),

crates/next-core/src/next_shared/transforms/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use server_actions::get_server_actions_transform_rule;
3434
use turbo_tasks::ResolvedVc;
3535
use turbo_tasks_fs::FileSystemPath;
3636
use turbopack::module_options::{ModuleRule, ModuleRuleEffect, ModuleType, RuleCondition};
37-
use turbopack_core::reference_type::{ReferenceType, UrlReferenceSubType};
37+
use turbopack_core::reference_type::ReferenceTypeCondition;
3838
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform};
3939

4040
use crate::next_image::{StructuredImageModuleType, module::BlurPlaceholderMode};
@@ -44,8 +44,8 @@ pub async fn get_next_image_rule() -> Result<ModuleRule> {
4444
RuleCondition::All(vec![
4545
// avoid urlAssetReference to be affected by this rule, since urlAssetReference
4646
// requires raw module to have its paths in the export
47-
RuleCondition::not(RuleCondition::ReferenceType(ReferenceType::Url(
48-
UrlReferenceSubType::Undefined,
47+
RuleCondition::not(RuleCondition::ReferenceType(ReferenceTypeCondition::Url(
48+
None,
4949
))),
5050
RuleCondition::any(vec![
5151
RuleCondition::ResourcePathEndsWith(".jpg".to_string()),
@@ -108,8 +108,8 @@ fn match_js_extension(enable_mdx_rs: bool) -> RuleCondition {
108108
/// condition for custom ecma specific transforms.
109109
pub(crate) fn module_rule_match_js_no_url(enable_mdx_rs: bool) -> RuleCondition {
110110
RuleCondition::all(vec![
111-
RuleCondition::not(RuleCondition::ReferenceType(ReferenceType::Url(
112-
UrlReferenceSubType::Undefined,
111+
RuleCondition::not(RuleCondition::ReferenceType(ReferenceTypeCondition::Url(
112+
None,
113113
))),
114114
match_js_extension(enable_mdx_rs),
115115
])

turbopack/crates/turbopack-core/src/reference_type.rs

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ pub enum WorkerReferenceSubType {
269269
ServiceWorker,
270270
NodeWorker,
271271
Custom(u8),
272-
Undefined,
273272
}
274273

275274
// TODO(sokra) this was next.js specific values. We want to solve this in a
@@ -351,53 +350,6 @@ impl Display for ReferenceType {
351350
}
352351

353352
impl ReferenceType {
354-
pub fn includes(&self, other: &Self) -> bool {
355-
if self == other {
356-
return true;
357-
}
358-
match self {
359-
ReferenceType::CommonJs(sub_type) => {
360-
matches!(other, ReferenceType::CommonJs(_))
361-
&& matches!(sub_type, CommonJsReferenceSubType::Undefined)
362-
}
363-
ReferenceType::EcmaScriptModules(sub_type) => {
364-
matches!(other, ReferenceType::EcmaScriptModules(_))
365-
&& matches!(sub_type, EcmaScriptModulesReferenceSubType::Undefined)
366-
}
367-
ReferenceType::Css(CssReferenceSubType::AtImport(_)) => {
368-
// For condition matching, treat any AtImport pair as identical.
369-
matches!(other, ReferenceType::Css(CssReferenceSubType::AtImport(_)))
370-
}
371-
ReferenceType::Css(sub_type) => {
372-
matches!(other, ReferenceType::Css(_))
373-
&& matches!(sub_type, CssReferenceSubType::Undefined)
374-
}
375-
ReferenceType::Url(sub_type) => {
376-
matches!(other, ReferenceType::Url(_))
377-
&& matches!(sub_type, UrlReferenceSubType::Undefined)
378-
}
379-
ReferenceType::TypeScript(sub_type) => {
380-
matches!(other, ReferenceType::TypeScript(_))
381-
&& matches!(sub_type, TypeScriptReferenceSubType::Undefined)
382-
}
383-
ReferenceType::Worker(sub_type) => {
384-
matches!(other, ReferenceType::Worker(_))
385-
&& matches!(sub_type, WorkerReferenceSubType::Undefined)
386-
}
387-
ReferenceType::Entry(sub_type) => {
388-
matches!(other, ReferenceType::Entry(_))
389-
&& matches!(sub_type, EntryReferenceSubType::Undefined)
390-
}
391-
ReferenceType::Runtime => matches!(other, ReferenceType::Runtime),
392-
ReferenceType::Internal(_) => matches!(other, ReferenceType::Internal(_)),
393-
ReferenceType::Loader => matches!(other, ReferenceType::Loader),
394-
ReferenceType::Custom(_) => {
395-
todo!()
396-
}
397-
ReferenceType::Undefined => true,
398-
}
399-
}
400-
401353
/// Returns true if this reference type is internal. This will be used in
402354
/// combination with [`ModuleRuleCondition::Internal`] to determine if a
403355
/// rule should be applied to an internal asset/reference.
@@ -408,3 +360,74 @@ impl ReferenceType {
408360
)
409361
}
410362
}
363+
364+
#[derive(
365+
PartialEq, Eq, TraceRawVcs, NonLocalValue, Debug, Clone, Hash, TaskInput, Encode, Decode,
366+
)]
367+
pub enum ReferenceTypeCondition {
368+
CommonJs(Option<CommonJsReferenceSubType>),
369+
EcmaScriptModules(Option<EcmaScriptModulesReferenceSubType>),
370+
Css(Option<CssReferenceSubType>),
371+
Url(Option<UrlReferenceSubType>),
372+
TypeScript(Option<TypeScriptReferenceSubType>),
373+
Worker(Option<WorkerReferenceSubType>),
374+
Entry(Option<EntryReferenceSubType>),
375+
Runtime,
376+
Internal,
377+
Loader,
378+
Custom(u8),
379+
}
380+
381+
macro_rules! match_condition_includes {
382+
(
383+
$self:expr, $other:expr,
384+
optional: [$($opt:ident),* $(,)?],
385+
unit: [$($unit:ident),* $(,)?],
386+
value: [$($val:ident),* $(,)?]
387+
$(,)?
388+
) => {
389+
match $self {
390+
$(
391+
ReferenceTypeCondition::$opt(sub_type) => {
392+
if let ReferenceType::$opt(other_sub_type) = $other {
393+
return sub_type.as_ref().is_none_or(|s| s == other_sub_type);
394+
}
395+
}
396+
)*
397+
$(
398+
ReferenceTypeCondition::$unit => {
399+
return matches!($other, ReferenceType::$unit { .. });
400+
}
401+
)*
402+
$(
403+
ReferenceTypeCondition::$val(v) => {
404+
if let ReferenceType::$val(ov) = $other {
405+
return v == ov;
406+
}
407+
}
408+
)*
409+
}
410+
};
411+
}
412+
413+
impl ReferenceTypeCondition {
414+
pub fn includes(&self, other: &ReferenceType) -> bool {
415+
if matches!(
416+
self,
417+
ReferenceTypeCondition::Css(Some(CssReferenceSubType::AtImport(_)))
418+
) && matches!(other, ReferenceType::Css(CssReferenceSubType::AtImport(_)))
419+
{
420+
// For condition matching, treat any AtImport pair as identical.
421+
return true;
422+
}
423+
424+
match_condition_includes!(
425+
self, other,
426+
optional: [CommonJs, EcmaScriptModules, Css, Url, TypeScript, Worker, Entry],
427+
unit: [Runtime, Loader, Internal],
428+
value: [Custom],
429+
);
430+
431+
false
432+
}
433+
}

turbopack/crates/turbopack-tests/tests/snapshot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use turbopack_core::{
5050
chunk_group_info::{ChunkGroup, ChunkGroupEntry},
5151
},
5252
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
53-
reference_type::{EntryReferenceSubType, ReferenceType, UrlReferenceSubType},
53+
reference_type::{EntryReferenceSubType, ReferenceType, ReferenceTypeCondition},
5454
};
5555
use turbopack_ecmascript::{
5656
AnalyzeMode, EcmascriptInputTransform, TreeShakingMode, chunk::EcmascriptChunkType,
@@ -347,8 +347,8 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
347347
.await?;
348348

349349
let conditions = RuleCondition::All(vec![
350-
RuleCondition::not(RuleCondition::ReferenceType(ReferenceType::Url(
351-
UrlReferenceSubType::Undefined,
350+
RuleCondition::not(RuleCondition::ReferenceType(ReferenceTypeCondition::Url(
351+
None,
352352
))),
353353
RuleCondition::any(vec![
354354
RuleCondition::ResourcePathEndsWith(".js".into()),

0 commit comments

Comments
 (0)