1- use std:: { collections:: BTreeSet , str:: FromStr } ;
2-
31use anyhow:: { Context , Result , bail} ;
42use either:: Either ;
53use rustc_hash:: FxHashSet ;
@@ -667,21 +665,11 @@ impl TryFrom<ConfigConditionItem> for ConditionItem {
667665 }
668666}
669667
670- #[ derive(
671- Clone , Debug , PartialEq , Eq , Serialize , Deserialize , TraceRawVcs , NonLocalValue , OperationValue ,
672- ) ]
673- #[ serde( rename_all = "camelCase" , untagged) ]
674- pub enum RuleConfigItem {
675- Options ( RuleConfigItemOptions ) ,
676- LegacyConditional ( FxIndexMap < RcStr , RuleConfigItem > ) ,
677- LegacyBoolean ( bool ) ,
678- }
679-
680668#[ derive(
681669 Clone , Debug , PartialEq , Eq , Serialize , Deserialize , TraceRawVcs , NonLocalValue , OperationValue ,
682670) ]
683671#[ serde( rename_all = "camelCase" ) ]
684- pub struct RuleConfigItemOptions {
672+ pub struct RuleConfigItem {
685673 pub loaders : Vec < LoaderItem > ,
686674 #[ serde( default , alias = "as" ) ]
687675 pub rename_as : Option < RcStr > ,
@@ -1401,11 +1389,7 @@ impl NextConfig {
14011389 }
14021390
14031391 #[ turbo_tasks:: function]
1404- pub async fn webpack_rules (
1405- & self ,
1406- active_conditions : BTreeSet < WebpackLoaderBuiltinCondition > ,
1407- project_path : FileSystemPath ,
1408- ) -> Result < Vc < WebpackRules > > {
1392+ pub async fn webpack_rules ( & self , project_path : FileSystemPath ) -> Result < Vc < WebpackRules > > {
14091393 let Some ( turbo_rules) = self . turbopack . as_ref ( ) . and_then ( |t| t. rules . as_ref ( ) ) else {
14101394 return Ok ( Vc :: cell ( Vec :: new ( ) ) ) ;
14111395 } ;
@@ -1429,43 +1413,6 @@ impl NextConfig {
14291413 . collect ( ) ,
14301414 )
14311415 }
1432- enum FindRuleResult < ' a > {
1433- Found ( & ' a RuleConfigItemOptions ) ,
1434- NotFound ,
1435- Break ,
1436- }
1437- // This logic is needed for the `LegacyConditional`/`LegacyBoolean` configuration
1438- // syntax. This is technically public syntax, but was never documented and it is
1439- // unlikely that anyone is depending on it (outside of some Next.js internals).
1440- fn find_rule < ' a > (
1441- rule : & ' a RuleConfigItem ,
1442- active_conditions : & BTreeSet < WebpackLoaderBuiltinCondition > ,
1443- ) -> FindRuleResult < ' a > {
1444- match rule {
1445- RuleConfigItem :: Options ( rule) => FindRuleResult :: Found ( rule) ,
1446- RuleConfigItem :: LegacyConditional ( map) => {
1447- for ( condition, rule) in map. iter ( ) {
1448- let condition = WebpackLoaderBuiltinCondition :: from_str ( condition) ;
1449- if let Ok ( condition) = condition
1450- && ( condition == WebpackLoaderBuiltinCondition :: Default
1451- || active_conditions. contains ( & condition) )
1452- {
1453- match find_rule ( rule, active_conditions) {
1454- FindRuleResult :: Found ( rule) => {
1455- return FindRuleResult :: Found ( rule) ;
1456- }
1457- FindRuleResult :: Break => {
1458- return FindRuleResult :: Break ;
1459- }
1460- FindRuleResult :: NotFound => { }
1461- }
1462- }
1463- }
1464- FindRuleResult :: NotFound
1465- }
1466- RuleConfigItem :: LegacyBoolean ( _) => FindRuleResult :: Break ,
1467- }
1468- }
14691416 let config_file_path = || project_path. join ( & self . config_file_name ) ;
14701417 for item in & rule_collection. 0 {
14711418 match item {
@@ -1479,55 +1426,52 @@ impl NextConfig {
14791426 } ,
14801427 ) ) ;
14811428 }
1482- RuleConfigCollectionItem :: Full ( rule_config_item) => {
1483- if let FindRuleResult :: Found ( RuleConfigItemOptions {
1484- loaders,
1485- rename_as,
1486- condition,
1487- } ) = find_rule ( rule_config_item, & active_conditions)
1429+ RuleConfigCollectionItem :: Full ( RuleConfigItem {
1430+ loaders,
1431+ rename_as,
1432+ condition,
1433+ } ) => {
1434+ // If the extension contains a wildcard, and the rename_as does not,
1435+ // emit an issue to prevent users from encountering duplicate module
1436+ // names.
1437+ if glob. contains ( "*" )
1438+ && let Some ( rename_as) = rename_as. as_ref ( )
1439+ && !rename_as. contains ( "*" )
14881440 {
1489- // If the extension contains a wildcard, and the rename_as does not,
1490- // emit an issue to prevent users from encountering duplicate module
1491- // names.
1492- if glob. contains ( "*" )
1493- && let Some ( rename_as) = rename_as. as_ref ( )
1494- && !rename_as. contains ( "*" )
1495- {
1496- InvalidLoaderRuleRenameAsIssue {
1497- glob : glob. clone ( ) ,
1441+ InvalidLoaderRuleRenameAsIssue {
1442+ glob : glob. clone ( ) ,
1443+ config_file_path : config_file_path ( ) ?,
1444+ rename_as : rename_as. clone ( ) ,
1445+ }
1446+ . resolved_cell ( )
1447+ . emit ( ) ;
1448+ }
1449+
1450+ // convert from Next.js-specific condition type to internal Turbopack
1451+ // condition type
1452+ let condition = if let Some ( condition) = condition {
1453+ if let Ok ( cond) = ConditionItem :: try_from ( condition. clone ( ) ) {
1454+ Some ( cond)
1455+ } else {
1456+ InvalidLoaderRuleConditionIssue {
1457+ condition : condition. clone ( ) ,
14981458 config_file_path : config_file_path ( ) ?,
1499- rename_as : rename_as. clone ( ) ,
15001459 }
15011460 . resolved_cell ( )
15021461 . emit ( ) ;
1503- }
1504-
1505- // convert from Next.js-specific condition type to internal Turbopack
1506- // condition type
1507- let condition = if let Some ( condition) = condition {
1508- if let Ok ( cond) = ConditionItem :: try_from ( condition. clone ( ) ) {
1509- Some ( cond)
1510- } else {
1511- InvalidLoaderRuleConditionIssue {
1512- condition : condition. clone ( ) ,
1513- config_file_path : config_file_path ( ) ?,
1514- }
1515- . resolved_cell ( )
1516- . emit ( ) ;
1517- None
1518- }
1519- } else {
15201462 None
1521- } ;
1522- rules. push ( (
1523- glob. clone ( ) ,
1524- LoaderRuleItem {
1525- loaders : transform_loaders ( & mut loaders. iter ( ) ) ,
1526- rename_as : rename_as. clone ( ) ,
1527- condition,
1528- } ,
1529- ) ) ;
1530- }
1463+ }
1464+ } else {
1465+ None
1466+ } ;
1467+ rules. push ( (
1468+ glob. clone ( ) ,
1469+ LoaderRuleItem {
1470+ loaders : transform_loaders ( & mut loaders. iter ( ) ) ,
1471+ rename_as : rename_as. clone ( ) ,
1472+ condition,
1473+ } ,
1474+ ) ) ;
15311475 }
15321476 }
15331477 }
@@ -2001,11 +1945,11 @@ mod tests {
20011945 }
20021946 } ) ;
20031947
2004- let rule_config: RuleConfigItemOptions = serde_json:: from_value ( json_value) . unwrap ( ) ;
1948+ let rule_config: RuleConfigItem = serde_json:: from_value ( json_value) . unwrap ( ) ;
20051949
20061950 assert_eq ! (
20071951 rule_config,
2008- RuleConfigItemOptions {
1952+ RuleConfigItem {
20091953 loaders: vec![ ] ,
20101954 rename_as: Some ( rcstr!( "*.js" ) ) ,
20111955 condition: Some ( ConfigConditionItem :: All (
0 commit comments